By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,165 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,165 IT Pros & Developers. It's quick & easy.

Dynamically Load Assembly (Windows Service)

P: n/a
I'm attempting to dynamically load an assembly from a windows service.
However, i'm having problems when the service runs... he is the line of
code that causes the error:

dim loAssembly as Assembly = Assembly.LoadFrom("SomeDLL.dll")

I have a test windows Form that i use when developing services... this
way i can run the service in debug mode. If i run as a windows form,
the code executes fine and the assembly loads. However, when i compile
and run as a windows service, the above line of code does not execute.

I'm guessing that while a windows service, the app is running in a
different context -- but i'm not sure. Any help would be appreciated.

Thanks!

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

dercon wrote:
and run as a windows service, the above line of code does not

execute.

What does this mean? Are you getting an exception? Can you post any
exceptions generated?

A little more information is needed.

Does your service run under an account that has sufficient priveleges
to load the assembly?

Nov 21 '05 #2

P: n/a
I fixed the initial issue... turns out that since the application is
running as a service, its base directory is the /windows/system32
directory so it was not finding the dll. I simply passed in the full
path to the dll and i was able to load the assembly.

However, a new issue has surfaced... i can load the assembly but i
can't create a class from the dll...

dim moDLL as SomeNamespace.SomeClass
loAssembly = Assembly.LoadFrom("C:\sswork\MyDLL.dll)
'The following line fails to create.
moDLL = loAssembly.CreateInstance("SomeNamespace.SomeClass ")

I'm getting the following error when i attempt to create my class
instance:
System.InvalidCastException: Specified cast is not valid.

However, if i start the service and attach it to the development
environment, if i manually create instance (using the command window)
the code works fine.

Any ideas?

Nov 21 '05 #3

P: n/a
Off the top of my head, it appears you are not casting the result of
the CreateInstance call to your class type. CreateInstance returns
Object so you would have to cast the object to your class:

moDLL = DirectCast(loAssembly.CreateInstance("SomeClass"), SomeClass)

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.