我有一个使用 WCF 与远程服务器通信的应用程序。我的日志中经常出现 ObjectDisposedException
,而且相对可预见,但我不明白为什么。看起来 WCF 对象本身已被释放。到目前为止,我已经看到这些对象类型被处置并抛出异常:
System.ServiceModel.Channels.HttpChannelFactory'1+HttpRequestChannel[System.ServiceModel.Channels.IRequestChannel]
System.ServiceModel.Channels.ServiceChannel
我收集了 WCF 跟踪信息,以这种方式失败的每个调用都遵循相同的模式:
- 跟踪传输,来自
- 事件边界,开始
- 消息日志跟踪
- 抛出异常
- 跟踪传输,至
我的代码中没有任何内容明确处理服务客户端。事实上,我只是在传递服务契约接口(interface)本身,它们没有实现 IDisposable
,因此,例如,using
block 不太可能是罪魁祸首。
正如我之前所说,我可以重现这些错误是相对可预测的;但是,我无法通过特定的服务契约接口(interface)使其按预期发生,更不用说对该接口(interface)的特定方法调用了。它移动了一点。有时第一次调用失败,有时多次调用失败。
我已经排除了网络连接问题。我能做些什么来隔离这些异常的来源吗?即,我的 CommunicationObject
派生对象被处置的核心原因,或者是我的异常日志记录中的一个特例,将它们视为良性的我唯一的选择?
更新:
我正在使用 channel 工厂来创建我的服务契约接口(interface)的实例。我不会缓存/重用那些 channel 工厂。我还有一些 IMessageInspector
实现,它们通过 IEndpointBehavior
连接起来并通过 channelFactory.Endpoint.Behaviors.Add(...)
附加。我正在以编程方式执行所有操作,而不是配置驱动,因此有很多代码在使用,因此没有发布。
堆栈跟踪示例:
System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen()
System.ServiceModel.Channels.RequestChannel.BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, Object state)
System.ServiceModel.Dispatcher.RequestChannelBinder.BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, Object state)
System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.StartSend(Boolean completedSynchronously)
System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.FinishEnsureOpen(IAsyncResult result, Boolean completedSynchronously)
System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.StartEnsureOpen(Boolean completedSynchronously)
System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.FinishEnsureInteractiveInit(IAsyncResult result, Boolean completedSynchronously)
System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.StartEnsureInteractiveInit()
System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.Begin()
System.ServiceModel.Channels.ServiceChannel.BeginCall(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, TimeSpan timeout, AsyncCallback callback, Object asyncState)
System.ServiceModel.Channels.ServiceChannelProxy.InvokeBeginService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
<truncated, my application-level code is at this level>
我搜索过网络,反编译过 WCF 内部结构,调试过,收集过跟踪日志。我已经完成作业了。正如您在编辑历史中看到的那样,每次有人要求时我都会添加详细信息。如果您有任何贡献要添加,包括“你不应该那样做”,请务必加入。
最佳答案
所以我发现了问题。一个非托管组件正在爆炸并在内存中涂鸦。 CLR 能够(在某种程度上)减轻大屠杀,但 WCF 异常是一个转移注意力的问题。
关于c# - 什么在处置我的 WCF 对象,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33760888/