0

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?

1 Answer 1

0

There are many causes for the apparent failure in the above scenario.

  1. AppDomian.Load expects Full Name of the Assembly

    "EventsPublisher, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    
  2. 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.

using System;
using System.Reflection;

public class Worker : MarshalByRefObject
{
    public void PrintDomain() 
    { 
        Console.WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain.CurrentDomain.FriendlyName); 
    }
}

class Example
{
    public static void Main()
    {
        // Create an ordinary instance in the current AppDomain
        Worker localWorker = new Worker();
        localWorker.PrintDomain();

        // Create a new application domain, create an instance
        // of Worker in the application domain, and execute code
        // there.
        AppDomain ad = AppDomain.CreateDomain("New domain");
        Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
            typeof(Worker).Assembly.FullName,
            "Worker");
        remoteWorker.PrintDomain();
    }
}

/* This code produces output similar to the following:

Object is executing in AppDomain "source.exe"
Object is executing in AppDomain "New domain"
 */

Check out below example to load assembly

using System;
using System.Reflection;

class Test {

   static void Main() {
      InstantiateINT32(true);      // OK!
   }

   static void InstantiateINT32(bool ignoreCase) {
      try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         object instance = currentDomain.CreateInstanceAndUnwrap(
            "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "SYSTEM.INT32",
            ignoreCase,
            BindingFlags.Default,
            null,
            null,
            null,
            null,
            null
         );
         Console.WriteLine(instance.GetType());
      } catch (TypeLoadException e) {
         Console.WriteLine(e.Message);
      }
   }
}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.