How do I load and run assemblies dynamically 
Author Message
 How do I load and run assemblies dynamically

Hi,

I'm writing a windows service which ties into BITS to download and install
current versions of an application on a local machine, but would appreciate
some assistance or suggestions.  The service code is very simple, but what I
want to be able to do is encapsulate the download/install code in a DLL that
I can update as well using the same app.  I'm envisioning the following in
pseudocode:

Thread wakes up
Thread reads configuration file to discover the latest version of the
updater utility
Thread loads the latest updater assembly and executes the proper function
with its parameters from the configuration file
Thread unloads the assembly and goes back to sleep.

I've tried loading the application domain so it wasn't domain-neutral, but
that isn't straightforward for me.  Here's the code I have so far that
encapsulates what I want, but the problem is, when I unload the appdom
variable, the assembly stays in memory in the CurrentDomain:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

    ' Set up the variables.

    Dim assem As System.Reflection.Assembly

    Dim obj As Object

    Dim versionType As Type

    Dim updateMethod As MethodInfo

    Dim args(0) As Object

    Dim appdom As AppDomain = AppDomain.CreateDomain("VersionUpdater")

    assem = appdom.CurrentDomain.Load( _

            loadFile("C:\DotNet\VersionUpdater\bin\VersionUpdater.exe"), _

            loadFile("C:\DotNet\VersionUpdater\bin\VersionUpdater.pdb"))

    versionType = assem.GetType("VersionUpdater.VersionUpdater")

    updateMethod = versionType.GetMethod("VersionCheck")

    obj = Activator.CreateInstance(versionType)

    args(0) = CBool(0)

    updateMethod.Invoke(obj, args)

    AppDomain.Unload(appdom)

End Sub

Any Ideas would be more than welcome!



Tue, 14 Jun 2005 06:13:05 GMT  
 How do I load and run assemblies dynamically
Have you looked at Jamie Cool's application updater component?

http://www.gotdotnet.com/team/windowsforms/

--
Scott Swigart
www.3leaf.com
Early Adopter Weblog: http://radio.weblogs.com/0117167/


Quote:
> Hi,

> I'm writing a windows service which ties into BITS to download and install
> current versions of an application on a local machine, but would
appreciate
> some assistance or suggestions.  The service code is very simple, but what
I
> want to be able to do is encapsulate the download/install code in a DLL
that
> I can update as well using the same app.  I'm envisioning the following in
> pseudocode:

> Thread wakes up
> Thread reads configuration file to discover the latest version of the
> updater utility
> Thread loads the latest updater assembly and executes the proper function
> with its parameters from the configuration file
> Thread unloads the assembly and goes back to sleep.

> I've tried loading the application domain so it wasn't domain-neutral, but
> that isn't straightforward for me.  Here's the code I have so far that
> encapsulates what I want, but the problem is, when I unload the appdom
> variable, the assembly stays in memory in the CurrentDomain:

> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click

>     ' Set up the variables.

>     Dim assem As System.Reflection.Assembly

>     Dim obj As Object

>     Dim versionType As Type

>     Dim updateMethod As MethodInfo

>     Dim args(0) As Object

>     Dim appdom As AppDomain = AppDomain.CreateDomain("VersionUpdater")

>     assem = appdom.CurrentDomain.Load( _

>             loadFile("C:\DotNet\VersionUpdater\bin\VersionUpdater.exe"), _

>             loadFile("C:\DotNet\VersionUpdater\bin\VersionUpdater.pdb"))

>     versionType = assem.GetType("VersionUpdater.VersionUpdater")

>     updateMethod = versionType.GetMethod("VersionCheck")

>     obj = Activator.CreateInstance(versionType)

>     args(0) = CBool(0)

>     updateMethod.Invoke(obj, args)

>     AppDomain.Unload(appdom)

> End Sub

> Any Ideas would be more than welcome!



Tue, 14 Jun 2005 06:28:39 GMT  
 How do I load and run assemblies dynamically
Yes, and it has been very helpful.  But the problem I have is that I'm
running a windows service that needs to manage updates to multiple
components, including itself.  I'm hoping for an answer to this specific
question.

Thanks, Jim


Quote:
> Have you looked at Jamie Cool's application updater component?

> http://www.gotdotnet.com/team/windowsforms/

> --
> Scott Swigart
> www.3leaf.com
> Early Adopter Weblog: http://radio.weblogs.com/0117167/



> > Hi,

> > I'm writing a windows service which ties into BITS to download and
install
> > current versions of an application on a local machine, but would
> appreciate
> > some assistance or suggestions.  The service code is very simple, but
what
> I
> > want to be able to do is encapsulate the download/install code in a DLL
> that
> > I can update as well using the same app.  I'm envisioning the following
in
> > pseudocode:

> > Thread wakes up
> > Thread reads configuration file to discover the latest version of the
> > updater utility
> > Thread loads the latest updater assembly and executes the proper
function
> > with its parameters from the configuration file
> > Thread unloads the assembly and goes back to sleep.

> > I've tried loading the application domain so it wasn't domain-neutral,
but
> > that isn't straightforward for me.  Here's the code I have so far that
> > encapsulates what I want, but the problem is, when I unload the appdom
> > variable, the assembly stays in memory in the CurrentDomain:

> > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles Button1.Click

> >     ' Set up the variables.

> >     Dim assem As System.Reflection.Assembly

> >     Dim obj As Object

> >     Dim versionType As Type

> >     Dim updateMethod As MethodInfo

> >     Dim args(0) As Object

> >     Dim appdom As AppDomain = AppDomain.CreateDomain("VersionUpdater")

> >     assem = appdom.CurrentDomain.Load( _

> >             loadFile("C:\DotNet\VersionUpdater\bin\VersionUpdater.exe"),
_

> >             loadFile("C:\DotNet\VersionUpdater\bin\VersionUpdater.pdb"))

> >     versionType = assem.GetType("VersionUpdater.VersionUpdater")

> >     updateMethod = versionType.GetMethod("VersionCheck")

> >     obj = Activator.CreateInstance(versionType)

> >     args(0) = CBool(0)

> >     updateMethod.Invoke(obj, args)

> >     AppDomain.Unload(appdom)

> > End Sub

> > Any Ideas would be more than welcome!



Wed, 15 Jun 2005 00:13:49 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Dynamically loading an assembly?

2. How to load Dll dynamically at Run Time?

3. Accessing files in excel dynamically------(Can It be done?)---

4. Load Assembly /CreateInstance

5. Load Assembly

6. One or more of the types in the assembly unable to load

7. Loading control from assembly

8. Trying to Dunamically Load Assembly via Reflection

9. Loading an assembly at runtime

10. Types in the assembly failed to load

11. Can not load file or assembly

12. could not load file or assembly

 

 
Powered by phpBB® Forum Software