public static void RunAtIsolatedDomain(Evidence securityInfo, AppDomainSetup info, Delegate action, params object[] args) { if (securityInfo == null) { throw new ArgumentNullException("securityInfo"); } if (info == null) { throw new ArgumentNullException("info"); } if (action == null) { throw new ArgumentNullException("action"); } if (!action.CanCrossDomain()) { throw new ArgumentException(Resources.GetString("AppDomainMixin_RunAtIsolatedXXX_ActionCanNotCrossDomain"), "action"); } var domain = default(AppDomain); try { domain = AppDomain.CreateDomain("Domain " + action.Method.ToString(), securityInfo, info); var type = typeof(MarshalByRefRunner); var runner = (MarshalByRefRunner)domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName); runner.Action = action; runner.Run(args); } catch (SerializationException e) { throw new ArgumentException(Resources.GetString("AppDomainMixin_RunAtIsolatedXXX_ActionParameterCanNotCrossDomain"), e); } finally { try { if (domain != null) { AppDomain.Unload(domain); } } catch { } } }
static void RunAtIsolatedDomain(Evidence securityInfo, AppDomainSetup info, Delegate action, params object[] args) { if (action == null) { throw new ArgumentNullException("action"); } if (!action.CanCrossDomain()) { throw new ArgumentException("The parameter must be domain crossable. Please check the following points: " + "Doesn't lambda expression capture external variable? " + "Or is a static method specified? " + "Or is an instance method of serializable/marshalizable specified?", "action"); } var domain = default(AppDomain); try { domain = AppDomain.CreateDomain("Domain " + action.Method.ToString(), securityInfo, info); var type = typeof(MarshalByRefRunner); var runner = (MarshalByRefRunner)domain.CreateInstanceAndUnwrap( type.Assembly.FullName, type.FullName); runner.Action = action; runner.Run(args); } catch (SerializationException e) { throw new ArgumentException("The parameter must be domain crossable. " + "Please confirm that the type inherits MarshalByRefObject, " + "or it is applied SerializableAttribute.", e); } finally { try { if (domain != null) { AppDomain.Unload(domain); } } catch { } } }
public static void RunAtIsolatedProcess(AppDomainSetup info, Delegate action, params object[] args) { if (info == null) { throw new ArgumentNullException("info"); } if (action == null) { throw new ArgumentNullException("action"); } if (!action.CanCrossDomain()) { throw new ArgumentException(Resources.GetString("AppDomainMixin_RunAtIsolatedXXX_ActionCanNotCrossDomain"), "action"); } // The reason of using GUID as follows is that IPC channel cannot close immediately. So we generate new port name each time. var portName = "localhost_nanonym_mixins_system_appdomainmixin_runatisolatedprocess" + Guid.NewGuid().ToString("N"); var properties = new Hashtable(); properties["name"] = "ipc server " + Guid.NewGuid().ToString("N"); properties["portName"] = portName; var serverSinkProvider = new BinaryServerFormatterSinkProvider() { TypeFilterLevel = TypeFilterLevel.Full }; var serverChannel = new IpcServerChannel(properties, serverSinkProvider); ChannelServices.RegisterChannel(serverChannel, false); try { RunAtIsolatedProcessCore(info.ApplicationBase, portName, action, args); } finally { ChannelServices.UnregisterChannel(serverChannel); } }