public static MServer CreateServer(String domain, bool createNewAppDomain)
{
if (domain == null)
{
throw new ArgumentNullException("domain");
}
if (domains.Contains(domain))
{
throw new DomainAlreadyExistsException(domain);
}
String typeName = ConfigurationSettings.AppSettings[CustomServerConfigurationKey];
Type serverType = null;
if (typeName != null && typeName != String.Empty)
{
// TODO: Allow custom servers..
}
else
{
serverType = typeof(MDefaultServer);
}
if (createNewAppDomain)
{
// Lets create a seperated AppDomain for this server
AppDomain currentDomain = AppDomain.CurrentDomain;
String baseDir = new FileInfo(currentDomain.BaseDirectory).FullName;
String configFile = String.Format(
"{0}/{1}.config",
baseDir, domain);
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = domain;
setup.ApplicationBase = currentDomain.SetupInformation.ApplicationBase;
setup.PrivateBinPath = currentDomain.SetupInformation.PrivateBinPath;
setup.ConfigurationFile = configFile;
// setup.ShadowCopyFiles = "false";
// setup.ShadowCopyDirectories = appBase;
Evidence baseEvidence = currentDomain.Evidence;
Evidence evidence = new Evidence(baseEvidence);
AppDomain newDomain = AppDomain.CreateDomain(
domain, evidence, setup);
object remoteInstance = newDomain.CreateInstanceAndUnwrap(
serverType.Assembly.FullName, serverType.FullName);
// Register the domain
domains.Add(domain, new DomainInfo( domain, remoteInstance as MServer, newDomain) );
// As this already method "unwraps" the target object, its safe
// to return it - in an "wrapped" object we should invoke the
// class's constructor
return (MServer) remoteInstance;
}
else
{
object localInstance = Activator.CreateInstance(serverType);
// Register the domain
domains.Add(domain, new DomainInfo( domain, localInstance as MServer ) );
return (MServer) localInstance;
}
}