Dynamically Load Assembly (Windows Service)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dercon

    #1

    Dynamically Load Assembly (Windows Service)

    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.LoadFr om("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!

  • Chris Dunaway

    #2
    Re: Dynamically Load Assembly (Windows Service)


    dercon wrote:[color=blue]
    > and run as a windows service, the above line of code does not[/color]
    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?

    Comment

    • dercon

      #3
      Re: Dynamically Load Assembly (Windows Service)

      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.S omeClass
      loAssembly = Assembly.LoadFr om("C:\sswork\M yDLL.dll)
      'The following line fails to create.
      moDLL = loAssembly.Crea teInstance("Som eNamespace.Some Class")

      I'm getting the following error when i attempt to create my class
      instance:
      System.InvalidC astException: 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?

      Comment

      • Chris Dunaway

        #4
        Re: Dynamically Load Assembly (Windows Service)

        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(loAs sembly.CreateIn stance("SomeCla ss"),SomeClas s)

        Comment

        Working...