9

In one of my application, which is related to system diagnostics, the related DLL is to be loaded and unloaded dynamically in C#. After some search I found that a separate DLL cannot be loaded dynamically its the complete AppDomain. So I have to create an AppDomain and use that DLL to be loaded unloaded dynamically. But I could not find anywhere how can I use that in code. I can not show the app code since it is against company rules.

Can somebody tell me some application code to use it. I want to load and unload the dll dynamically using appdomain and call a specific method in that dll, the dll does not have any entry point.

Thanks for answers. Ashutosh

Ashutosh
  • 397
  • 1
  • 7
  • 20

4 Answers4

14

How to: Load Assemblies into an Application Domain

public static void Main()


    {
        // Use the file name to load the assembly into the current
        // application domain.
        Assembly a = Assembly.Load("example");
        // Get the type to use.
        Type myType = a.GetType("Example");
        // Get the method to call.
        MethodInfo myMethod = myType.GetMethod("MethodA");
        // Create an instance.
        object obj = Activator.CreateInstance(myType);
        // Execute the method.
        myMethod.Invoke(obj, null);
    }

As for how to unload it, you have to unload the AppDomain itself, see this

AppDomain Temporary = AppDomain.CreateDomain("Temporary");
try
{
  Gateway Proxy = 
    (Gateway) Temporary.CreateInstanceAndUnwrap("Shim", "Shim.Gateway");

  Match M = Proxy.LoadAndMatch("Plugin.dll", 
    "Though the tough cough and hiccough, plough them through");  
}
finally
{
  AppDomain.Unload(Temporary);
}
Illuminati
  • 4,539
  • 2
  • 35
  • 55
  • 2
    Do i have to make a separate Main entry point which calls the DLL in separate AppDomain to load and unload the dll using load and unload of AppDomain...? – Ashutosh Jul 05 '11 at 05:49
  • 1
    There is nothing called separate main entry point. If your application is a Winforms applciation there is already a entry point. If your application is a web application there is Application_Start method in the global.asax. – Illuminati Jul 05 '11 at 05:52
  • re: first comment to answer: he needs a reference on .NET remoting, the example doesn't explain nor make it clear that remoting is being used for activation/RPC of component in secondary appdomain. – Shaun Wilson Nov 08 '16 at 08:10
1

It's difficult to understand your question, but I will try to make some suggestions.

There is no reason you cannot dynamically Load a dll directly into your application w/o a separate App Domain, the trick is that you cannot unload it. This is only important if you may load multiple versions of the same dll (i.e. you want the ability to update this diagnostic component to a new version without halting the execution of your application). If that is what you are trying to do, then I suggest this CodeProject article.

Paul Wheeler
  • 18,988
  • 3
  • 28
  • 41
  • 1
    The C# Dll is a diagnostics, which internally calls some win32 dll's which loads device drivers for the hardware under test. After test completes it needs to be reset and closing application releases all the memory and driver from memory which resets the hardware. Now i don't want to close application but unload the C# dll which will eventually reset the hardware by unloading related win32 dll and drivers, which requires AppDomain. Im not much sure how can i use it...??? – Ashutosh Jul 05 '11 at 05:40
  • 1
    AppDomains would certainly solve your Win32 memory issues. Although in most cases you should be able to free the vast majority of the memory used by your Win32 API by making the proper cleanup/delete calls. But if unloading an AppDomain is what you want to do then there are ample examples posted in the answers above. If you still have a question you'll have to be more specific. – Paul Wheeler Jul 05 '11 at 20:56
1

Actually you can dynamically load assemblies into your app domain and run code from it, the issue is that you cannot then unload the assembly. You can however load additional app domains (and assemblies into them) and unload the app domain when you are done.

As its name suggests though, you then have a new application domain, and you can't just simply call its code and use its types you need to marshal your calls and data across the domain boundaries. If you search you will find lots of examples on how to do this.

Something to consider though, is that this is a common pattern, and there are ready made solutions for it, the framework itself has a whole addin namespace that is dedicated to this type of plug-in behavior, it might be worth your while in having a close look at that first. There is an article here that shows how to use it.

Tim Jarvis
  • 18,465
  • 9
  • 55
  • 92
0

Thanks guys, here is link where i found answer to my quetion:

The MSDN forum description for load and unload of assemblies dynamically

The other dll can be dynamically loaded and unloaded using another class which does load assembly and and call methods in that assembly... AppDomain.CreateInstanceAndUnwrap generally wants input as assemblies from current project or generally current namespace. to solve that i need Assembly.LoadFrom(); to be used in some other class and create AppDomain and create instance of this class using AppDomain object as given in link.

Thanks for ur replies guys.

Ashutosh
  • 397
  • 1
  • 7
  • 20