gpt4 book ai didi

c# - IIS 中托管的 WCF 服务在不同线程中使用时返回 "Safe handle has been closed"

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:57 28 4
gpt4 key购买 nike

我在使用 winform 中的 wcf 代理时遇到了一些问题。这个 wcf 服务托管在 IIS 上,公开了 2 个方法,当我在单线程 winform 中使用它时(一次一个调用),它可以正常工作。当我在不同的线程中使用相同的代理(相同的实例或 2 个单独的实例,这无关紧要)时,问题出现了,我确实收到错误:“安全句柄已关闭”。

我真正不明白的是:如果我启动一些线程,它们都调用相同的 wcf 方法,它工作正常。只有当我开始调用其中一个 wcf 方法,开始另一个调用第二个 wcf 方法并且第二次调用在第一次调用完成之前完成时,才会出现问题。澄清一下:

This will work:
- start threadA that calls wcf MethodA
- start threadB that calls wcf MethodA
- MethodA in threadA finishes <-- No errors
- MethodA in threadB finishes <-- No errors

This won't work:
- start threadA that calls wcf MethodA
- start threadB that calls wcf MethodB
- MethodB in threadB finishes <-- No errors
- MethodA in threadA finishes <-- Error "Safe handle has been closed"

This won't work either, methods order doesn't matter, only the fact that I mix methods it seems:
- start threadA that calls wcf MethodB
- start threadB that calls wcf MethodA
- MethodA in threadB finishes <-- No errors
- MethodB in threadA finishes <-- Error "Safe handle has been closed"

这是我收到的堆栈:

Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at MyProxy.MyInterface.MyMethod(String myParam)

谁有解释?

最佳答案

通常,所有代理类都派生自 System.ServiceModel.ClientBase。根据 MSDN 文档,ClientBase 的实例方法不是线程安全的。

http://msdn.microsoft.com/en-us/library/ms576141.aspx

此外,查看错误,您的服务中可能存在一些公共(public)资源。请检查 MethodA 和 MethodB 是否正在访问公共(public)资源。调试服务会有帮助

关于c# - IIS 中托管的 WCF 服务在不同线程中使用时返回 "Safe handle has been closed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15114069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com