book
Article ID: KB0087203
calendar_today
Updated On:
Description
Resolution:
GridServer Services can be implemented to run inside of user-created AppDomains. To do this, create a wrapper around your Service implementation, and specify the wrapper class as the implementing class in the Service Type Registry entry for your Service. Add a method to the wrapper class that creates the new AppDomain and instantiates your Service implementation, and register this method as an init method in the Service Type Registry entry for your Service.
The code snippet below demonstrates the use of such a wrapper and shows how to set up the new AppDomain so that classes that are instatiated inside of it inherit the proper permissions:
namespace Example
{
[Serializable]
public class ServiceImplementation
{
public void Init(object data)
{
System.Diagnostics.Trace.WriteLine("Service.Init() called.");
}
public object Service(object data)
{
System.Diagnostics.Trace.WriteLine("Service.Service() called.");
return data;
}
}
public class ServiceWrapper
{
private ServiceImplementation service;
public object Service(object data)
{
return service.Service(data);
}
public void Init(object data)
{
AppDomainSetup ads = new AppDomainSetup();
ads.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
ads.ApplicationName = "NodeServiceDomain";
Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);
evidence = new Evidence(evidence);
evidence.AddHost(new Zone(System.Security.SecurityZone.MyComputer));
AppDomain appDomain = AppDomain.CreateDomain("ServiceDomain",evidence,ads);
service = (ServiceImplementation) appDomain.CreateInstanceAndUnwrap(
Assembly.GetExecutingAssembly().FullName, typeof(ServiceImplementation).FullName);
service.Init(data);
}
}
}
In the Service Type Registry entry for the example above, the "class" field should be set to "Example.ServiceWrapper" and the "initMethod" field should be set to "Init."
The ServiceImplementation class should derive from System.MarshalByORefObject so that execution takes place in a remote appdomain.
Issue/Introduction
Running .NET services in AppDomains