Dynamic Load Assembly

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: BrianS (BrianS_at_discussions.microsoft.com)
Date: 01/06/05


Date: Thu, 6 Jan 2005 09:49:02 -0800

I have Web Service (A) that dynamically loads a private assembly (B). I do
NOT reference B in A. ( I do reference A in B, but that should not matter?).
Both these Assemblies reside in the A's /Bin.

I dynamically load B and Invoke a Method:

'Create evidence for the new appdomain.
Dim adevidence As System.Security.Policy.Evidence =
AppDomain.CurrentDomain.Evidence

' Create the new application domain.
Dim domain As AppDomain = AppDomain.CreateDomain("MyDomain",
adevidence,AppDomain.CurrentDomain.SetupInformation)
Dim myAssembly As [Assembly] = domain.Load(strAssemblyName)
Dim sysType As Type = myAssembly.GetType(strClassName)

obj = Activator.CreateInstance(sysType, New Object())

' Invoke the method
Dim intBindings As Integer = BindingFlags.DeclaredOnly Or
BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or
BindingFlags.InvokeMethod
returnObj= obj.GetType.InvokeMember(strMethod, intBindings, Nothing, obj,
New Object())

' Unload the application domain.
AppDomain.Unload(domain)

It was my understanding to load the assembly into a new AppDomain so I can
explicity Unload the AppDomain to free resources.

However, when I excecute my Web Service (A) and I am on the Service Page
(where all methods are listed) I see that B has already been loaded in the
aspnet_wp.exe process using Process Explorer?

Why would this be? Is it because, using the Load context, since assemblies
reside in the same path, .Net automactically loads them at startup?

I tried commenting out the code above, but it still loads the assembly at
startup.



Relevant Pages

  • Re: Is the GAC dynamic?
    ... The GAC registers an assembly, and I believe is loaded when your application ... If more than one application loads it simultaneously, ... "Multiple copies of assemblies with the same name but different version ... > automatically reflected by the GAC, or do I have to manually unload all ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Again: referencing other generated "InMemory" assembly
    ... it only loads the assemblies a single time ... ... loaded from when generated main assembly loads. ... Initializing runtime sequence file Lib1 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Assembly.LoadFrom, .NET 2.0
    ... You could also try deleting all local assemblies to remove all possible ... service that handled the download details and that solved the problem. ... The whole deployment is based on a bootstrapper that loads our ... I put an HTTP sniffer on the server, and when a client with an existing ...
    (microsoft.public.dotnet.framework)
  • unload a dynamically created assembly
    ... I'm using the ICodeCompiler.CompileAssemblyFromSource method to compile a ... loads the assembly. ... than one of these assemblies in memory. ... AppDomain doesn't seem to have any provision to load an assembly that was ...
    (microsoft.public.dotnet.general)
  • Re: Dynamic creation of object problem
    ... ASP.NET makes a shadow copy of the assemblies it loads, ... assemblies in the bin directory are never locked by the worker ... There is no way to unload an assembly from an app domain. ... >loads assemblies, looking for objects that implement a particular interface. ...
    (microsoft.public.dotnet.framework.aspnet)