当收到对我的服务的HTTP响应时,WCF错误

本文关键字:响应 WCF 错误 HTTP 我的 服务 | 更新日期: 2023-09-27 18:01:43

嗨,伙计们,我使用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)

当收到对我的服务的HTTP响应时,WCF错误

出现这种错误的情况有很多。

首先

检查您是否提供了正确的[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>

我想这会对你有帮助。

我能解决这个问题。

如果你正在使用EF"代码优先"的方法,并且你想使用WCF访问你的数据,你应该考虑不启用延迟加载,使用poco类中的"virtual"关键字,因为WCF序列化不能序列化动态代理数据。