c# - 接收对我的服务的 HTTP 响应时出现 WCF 错误

标签 c# .net wcf web-services wcf-binding

大家好,我使用 WCF 创建了一个服务,但是当我测试我的服务时,有些服务成功完成,为什么有些服务给我以下错误。关于如何解决这些的任何想法?谢谢

An error occurred while receiving the HTTP response to http://localhost:8733/PortOperation/Operator_Service/ws. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

Server stack trace: 
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.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 IOperator_Service.GetAllUser(String ConnectionString)
at Operator_ServiceClient.GetAllUser(String ConnectionString)

Inner Exception:
The underlying connection was closed: An unexpected error occurred on a receive.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

Inner Exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

Inner Exception:
An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

最佳答案

出现这种错误的场景有很多。

首先

检查您是否提供了正确的 [DataContract] 和 [DataMember]?如果未提供,则会发生此类错误。您必须在响应中传递的类上方编写 [DataContract],并在进入客户端响应的类成员上方编写 [DataMember]。

例如。

[DataContract]
class Program
{
  [DataMember] 
  public string Example{get;set}
}

第二

如果传递了一些数据类型的最小值,请检查响应。这意味着某个时间数据成员在它采用最小值时未被初始化。 例如int 的 minValue 是 '-2147483648',所以有时它无法序列化并抛出错误。

如果您想跟踪此类错误,请在您的服务器端 web.config 中添加以下代码

<system.diagnostics>
 <sources>
  <source name="System.ServiceModel" switchValue="Information, ActivityTracing"    propagateActivity="true">
   <listeners>
     <add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener"      initializeData= "D:\Traces.svclog"/>
   </listeners>
  </source>
 </sources>
</system.diagnostics>

我想这对你有帮助。

关于c# - 接收对我的服务的 HTTP 响应时出现 WCF 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15167995/

相关文章:

c# - 如何让双击编辑在我的 ListView 中的一行上工作?

c# - 如何使用 C# 对 Picasa 网络相册数据 API 进行 OAuth2 身份验证?

c# - 如何将泛型 List<T> 转换为基于接口(interface)的 List<T>

wcf - 如何将参数传递给 ServiceHost

WCF 服务设计示例

c# - 构建字符串时如何保持上一行的缩进

c# - 如何使用 DateTime.Today 与 TextBox.Text 的比较验证器?

c# - 使用 protobuf-net 序列化基类属性

c# - 使用 TFS SDK 查询源代码管理权限时的性能问题

c# - 帮助解决简单的 restful post 问题