I'm just trying to figure out AppDomain and loading DLLs from different locations into a newly created AppDomain and am running into difficulties. I know I will be using different methods but I'm still trying to figure out this. Here's the backstory of the example.
I have a dll called MyReadableDLL.dll located in c:\DLLTest. Here is the contents:
namespace MyReadableDLL
{
public class ReadThis : System.MarshalByRefObject
{
public string RetString()
{
return "You read this from 'Read This!'";
}
}
}
I have created a DomainBuilder in a separate project and here is the contents:
class DomainBuilder
{
AppDomainSetup domaininfo = new AppDomainSetup();
AppDomain appDomain;
public DomainBuilder()
{
domaininfo.ApplicationBase = @"c:\DLLtest";
appDomain = AppDomain.CreateDomain("MyTestDomain", null, domaininfo);
var currentdomain = AppDomain.CurrentDomain.FriendlyName;
var temporarydomain = appDomain.FriendlyName;
var appbase = appDomain.SetupInformation.ApplicationBase;
Assembly assembly = appDomain.Load("MyReadableDLL");
}
}
I'm not understanding why it fails when I it gets the the appDomain.Load line. From what I've read about AddDomainSetup, the AppliationBase sets up the default directory for checking for EXE/DLL files and there is only the MyReadableDLL file in that folder. I've seen many solution which state to use CreateInstanceFromandUnwrap and i'm certain I'll go in that direction. In the meantime, i'm trying to find a working example of 1) AddDomain creation and then 2) AppDomain.Load into the newly created domain. That's it. What am I missing?
There are many causes for the apparent failure in the above scenario.
AppDomian.Load expects Full Name of the Assembly
Ensure you run the program as Administrator to get over security exceptions
Please find the below example of understanding AppDomain, where it shows the simplest way to execute code in another application domain.
Check out below example to load assembly
I have written a blog covering the same introduction. Feel free to check out my blog as well for more details on the same. Sending Events using AppDomain