我最终将SocketInformation传递给新的AppDomain,并在那里创建一个新的套接字对象(在新的AppDomain中)。
在我的“沙盒包装对象,”我有这样的功能:
internal class Sandbox
{
private AppDomain _AppDomain;
private WebApplicationProxy _Proxy;
public Sandbox(string assemblyFile)
{
_AppDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString());
_Proxy = (WebApplicationProxy)_AppDomain.CreateInstanceAndUnwrap(
Assembly.GetExecutingAssembly().FullName,
"WebServer.WebApplication.Proxy");
_Proxy.Initialize(assemblyFile);
}
public void SendResponse(Socket client, HttpRequest request)
{
SocketInformation clientInfo = client.DuplicateAndClose(Process.GetCurrentProcess().Id);
_Proxy.GetResponse(clientInfo, request);
}
}
,并在新的AppDomain“恢复”的插座,我还没有确定到底发生了什么幕后......这里的验证码:
internal class Proxy : MarshalByRefObject
{
private AppController _AppController;
public void Initialize(string assemblyFile)
{
Assembly asm = Assembly.LoadFile(assemblyFile);
var q = from t in asm.GetTypes()
where t.GetInterfaces().Contains(typeof(AppController))
&& !t.IsAbstract && t.IsClass
select t;
foreach (Type t in q)
{
_AppController = (AppController)Activator.CreateInstance(t);
}
}
public void SendResponse(SocketInformation clientInfo, HttpRequest req)
{
Socket client = new Socket(clientInfo);
LinkedList<byte[]> toSend = _AppController.GetResponse(client, req);
foreach (byte[] bytes in toSend)
client.Send(bytes);
client.Close();
}
}
我不知道的时候DuplicateAndClose
被调用,或创建新域新的Socket对象发生了什么细节,但它运作良好至今。
编组套接字而不是数据。不知道该怎么做,SocketInformation看起来很诱人。 – 2011-06-12 15:06:48
这工作:)我曾想过,但认为没有办法序列化套接字(技术上仍然是真的)。我只是使用Socket.DuplicateAndClose获取SocketInformation(它是可序列化的)并使用它在新的AppDomain中创建套接字。谢谢。 – toby 2011-06-12 20:55:43
帮我们一个忙,写一个答案。 AndClose让我感到困惑。 – 2011-06-12 21:20:12