BeginAuthenticateAsClient 在新的 AppDomain 中不起作用
我正在尝试在新的 AppDomain
中创建一个 SslStream
对象。但是,在流上运行 BeginAuthenticateAsClient
方法会导致抛出异常。
internal class SslWrapper : MarshalByRefObject
{
public void CreateStream(Stream innerStream)
{
var ioStream = new SslStream(innerStream,
false,
ValidateRemoteCertificate,
SelectLocalCertificate);
ioStream.BeginAuthenticateAsClient("Target",
GetCertificates(),
SslProtocols.Tls,
false,
new AsyncCallback(CallbackMethod),
null);
}
private void CallbackMethod(IAsyncResult result)
{
// TODO: handle callback...
}
}
例外情况指出:
Assembly System 中的类型 System.Net.AsyncProtocolRequest、Version=2.0.0.0、Culture=neutral、PublicKeyToken=b77a5c561934e089 未标记为可序列化。
编辑:调用BeginAuthenticateAsClient()
后抛出异常。调用了 SelectLocalCertificate()
回调方法,但未调用 ValidateRemoteCertificate()
。
新的 AppDomain
和 SslWrapper
创建如下:
_appDomain = AppDomain.CreateDomain(name, null, new AppDomainSetup { PrivateBinPath = pathToDll });
_wrapper = (SslWrapper)_appDomain.CreateInstanceFromAndUnwrap(pathToDll, typeof(SslWrapper).FullName);
_wrapper.CreateStream(innerStream);
使用同步 AuthenticateAsClient()
可以成功工作,就像在默认情况下运行异步代码一样应用程序域
。
我假设该异常与 AsyncCallback 方法有关。 但是,此方法不应跨越 AppDomain 边界,那么为什么我会收到异常?更重要的是,有可能避免吗?
我使用的是.NET 2,WinXP 64位。
编辑:这也是 .NET 4 中的问题。
I am trying to create an SslStream
object in a new AppDomain
. However, running the BeginAuthenticateAsClient
method on the stream causes an Exception to be thrown.
internal class SslWrapper : MarshalByRefObject
{
public void CreateStream(Stream innerStream)
{
var ioStream = new SslStream(innerStream,
false,
ValidateRemoteCertificate,
SelectLocalCertificate);
ioStream.BeginAuthenticateAsClient("Target",
GetCertificates(),
SslProtocols.Tls,
false,
new AsyncCallback(CallbackMethod),
null);
}
private void CallbackMethod(IAsyncResult result)
{
// TODO: handle callback...
}
}
The exception states:
The type System.Net.AsyncProtocolRequest in Assembly System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is not marked as serializable.
Edit: The exception is thrown after calling BeginAuthenticateAsClient()
. The SelectLocalCertificate()
callback method is called, but ValidateRemoteCertificate()
is not.
The new AppDomain
and SslWrapper
are created as follows:
_appDomain = AppDomain.CreateDomain(name, null, new AppDomainSetup { PrivateBinPath = pathToDll });
_wrapper = (SslWrapper)_appDomain.CreateInstanceFromAndUnwrap(pathToDll, typeof(SslWrapper).FullName);
_wrapper.CreateStream(innerStream);
Using the synchronous AuthenticateAsClient()
instead works successfully, as does running the asynchronous code in the default AppDomain
.
I assume the exception is related to the AsyncCallback method.
However, this method should not be crossing the AppDomain boundary, so why might I be getting the exception? More importantly, is it possible to avoid it?
I am using .NET 2, WinXP 64bit.
Edit: This is also a problem in .NET 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题应该出在您的 innerStream 对象中,它必须是可序列化的或 MarshalByRefObject 才能工作(即能够作为参数跨 AppDomain 边界传递)
the problem should be in your innerStream object, it has to be serializable or MarshalByRefObject in order to work(i.e. be able to be passed as parameter across AppDomain boundary)