c# - 从 WCF 客户端内部调试 NullReference 异常

标签 c# .net wcf exception nullreferenceexception

我们有一个 WCF 服务和大致如下的客户端代码:

bool success = false;
IClientChannel proxy = null;
try
{
  var client = channelFactory.CreateChannel(new EndpointAddress(url));
  proxy = client as IClientChannel;
  proxy.Open();
  client.MyWcfMethod(a, b, c);                                
  proxy.Close();
  success = true;
}
finally
{
  if (!success)
    proxy.Abort();
}

它似乎工作正常,但在我们的 beta 环境中,大约 70000 次调用中就会抛出一次 NullReferenceException。我们仔细记录异常和周围的所有数据,看起来它不以任何方式依赖于数据。如果我在异常发生后立即重复调用(稍等片刻直到成功),它就会顺利进行。

异常出现得如此罕见,以至于我无法重现、跟踪或调试它。似乎在调用时立即抛出异常,没有调用服务器代码。

这是堆栈跟踪:

Object reference not set to an instance of an object. Server stack trace: 
at System.Xml.Linq.ElementWriter.WriteElement(XElement e) 
at System.Xml.Linq.XElement.WriteTo(XmlWriter writer) 
at System.Xml.Linq.XElement.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter writer) 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteIXmlSerializable(XmlWriterDelegator xmlWriter, Object obj, XmlSerializableWriter xmlSerializableWriter) 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteIXmlSerializable(XmlWriterDelegator xmlWriter, Object obj) 
at System.Runtime.Serialization.XmlDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) 
at WriteCallContextToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract ) 
at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) 
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) 
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) 
at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph) 
at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectContentHandleExceptions(XmlWriterDelegator writer, Object graph) 
at System.Runtime.Serialization.DataContractSerializer.WriteObjectContent(XmlDictionaryWriter writer, Object graph) 
at System.ServiceModel.Channels.XmlObjectSerializerHeader.OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) 
at System.ServiceModel.Channels.MessageHeader.WriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) 
at System.ServiceModel.Channels.MessageHeaders.WriteHeaderContents(Int32 headerIndex, XmlDictionaryWriter writer) 
at System.ServiceModel.Channels.Message.WriteMessagePreamble(XmlDictionaryWriter writer) at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer) at System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota) 
at System.ServiceModel.Channels.BinaryMessageEncoderFactory.BinaryMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset) 
at System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(Message message) 
at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout) 
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout) 
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) 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 IMyService.MyWcfMethod(int a, string b, double c) 
at MyClientClass.Process() 

关于在哪里搜索答案有什么想法吗?

最佳答案

这听起来可能是线程问题 - 也许 WCF 客户端代码正在为多个调用重用相同的序列化程序实例,并且它正在破坏内容。

您可以尝试将对 client.MyWcfMethod() 的调用放在 lock block 中吗?

例如:

private static readonly object _wcfSync = new object();
...

  lock (_wcfSync)
  {
    client.MyWcfMethod(a, b, c);
  }

关于c# - 从 WCF 客户端内部调试 NullReference 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18639216/

相关文章:

c# - HttpPost 未执行

wcf - Silverlight客户端身份不总是传递到服务器吗?

c# - WCF 用户名身份验证 : Can I get the Username in a custom ServiceAuthorizationManager?

c# - OleDb 异常 : Command contains unrecognized phrase/keyword

C# 模拟具体类。如何?

c# - 将距离添加到 GPS 坐标

c# - 无法在运行时找到静态资源,即使设计师可以看到它

.net - 在TextBox中更改选定的Text BackColor

.net - 在实现接口(interface)但使用基类的成员实现的 .NET 中创建 DynamicType

c# - 仅使用服务的 WCF 证书身份验证(无客户端证书)