Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an application domain and run my application in it?

Tags:

I need to create a custom application domain to work around a bug in the .NET runtime's default behavior. None of the sample code I've seen online is helpful since I don't know where to place it, or what it needs to replace within my Main() method.

like image 712
Dan Is Fiddling By Firelight Avatar asked Apr 15 '10 19:04

Dan Is Fiddling By Firelight


People also ask

What is an example of an application domain?

6.3. Application domains include banks, insurance companies, or hospitals. In this book, equipment management for a small software company is our main example. Internet applications have become increasingly important, especially for the home and entertainment domains.

What is the purpose of using an application domain?

Application domains provide an isolation boundary for security, reliability, and versioning, and for unloading assemblies. Application domains are typically created by runtime hosts, which are responsible for bootstrapping the common language runtime before an application is run.

How can you create an application domain in asp net?

You create a new application domain using one of the overloaded CreateDomain methods in the System. AppDomain class. You can give the application domain a name and reference it by that name.

How many application domain can be used in single process?

Process A runs managed code with one application domain while Process B runs managed code has three application domains. Note that Process C which runs unmanaged code has no application domain. Code and data are safely isolated using the boundary provided by the AppDomain.


1 Answers

It should probably be noted that creating AppDomains just to get around something that can be fixed with a constant string is probably the wrong way to do it. If you are trying to do the same thing as the link you noted, you could just do this:

var configFile = Assembly.GetExecutingAssembly().Location + ".config"; if (!File.Exists(configFile))     throw new Exception("do your worst!"); 

Recursive Entry Point :o)

static void Main(string[] args) {     if (AppDomain.CurrentDomain.IsDefaultAppDomain())     {         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);          var currentAssembly = Assembly.GetExecutingAssembly();         var otherDomain = AppDomain.CreateDomain("other domain");         var ret = otherDomain.ExecuteAssemblyByName(currentAssembly.FullName, args);          Environment.ExitCode = ret;         return;     }      Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);     Console.WriteLine("Hello"); } 

Quick sample using a nonstatic secondary entry point and MarshalByRefObject...

class Program {     static AppDomain otherDomain;      static void Main(string[] args)     {         otherDomain = AppDomain.CreateDomain("other domain");          var otherType = typeof(OtherProgram);         var obj = otherDomain.CreateInstanceAndUnwrap(                                  otherType.Assembly.FullName,                                  otherType.FullName) as OtherProgram;          args = new[] { "hello", "world" };         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);         obj.Main(args);     } }  public class OtherProgram : MarshalByRefObject {     public void Main(string[] args)     {         Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);         foreach (var item in args)             Console.WriteLine(item);     } } 
like image 105
Matthew Whited Avatar answered Oct 29 '22 04:10

Matthew Whited