【问题标题】:WCF Test Client Error Failed to invoke the service ( while Sending Entities Through WCF)WCF 测试客户端错误调用服务失败(通过 WCF 发送实体时)
【发布时间】:2016-03-27 14:07:55
【问题描述】:

我有一个服务,除了返回list<Module> 的方法之外,所有方法都可以正常工作

我的模块类(一个实体)如下

[DataContract(IsReference = true)] //prevents Infinite loop
[KnownType(typeof(SubModule))]
public class ModuleMst 
{
    public ModuleMst ()
    { }

    [Key]
    [DataMember]
    public int moduleid { get; set; }

    [StringLength(100)]
    [DataMember]
    public string modulename { get; set; }

    [StringLength(1)]
    [DataMember]
    public string status { get; set; }

    [DataMember]
    public string crby { get; set; }

    [DataMember]
    public DateTime crdt { get; set; }

    [DataMember]
    public string modby { get; set; }

    [DataMember]
    public DateTime moddt { get; set; }

    //[DataMember]
    public virtual List<SubModule> SubModules { get; set; }

}

异常 WcfTestClient 显示为

An error occurred while receiving the HTTP response to http://localhost:8888/ArchService/. 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.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 IArchService.GetAllModules()
   at ArchServiceClient.GetAllModules()

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)

配置是

  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="LargeSettingsWS" maxBufferPoolSize="524288" maxReceivedMessageSize="6553600">
          <readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None" />
        </binding>
      </wsHttpBinding>
      <netTcpBinding>
        <binding name="LargeSettingsTcp" maxBufferSize="524288" maxBufferPoolSize="524288" maxReceivedMessageSize="524288">
          <readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport" >
            <transport clientCredentialType="Windows" />
            <message clientCredentialType="Windows" />
          </security>
        </binding>
      </netTcpBinding>
      <netNamedPipeBinding>
        <binding name="LargeSettingsPipe" maxBufferSize="524288" maxBufferPoolSize="524288" maxReceivedMessageSize="524288">
          <readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport" />
        </binding>
      </netNamedPipeBinding>
    </bindings>
    <services>
      <service name="AppService.ArchService">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8808/ArchService" />
            <add baseAddress="net.pipe://localhost/ArchService/" />
            <add baseAddress="http://localhost:8888/ArchService/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="LargeSettingsWS" contract="AppService.IArchService">

          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="LargeSettingsTcp" name="netTcpEndPoint" contract="AppService.IArchService" listenUriMode="Explicit">
          <identity>
            <certificateReference storeName="My" storeLocation="LocalMachine" x509FindType="FindBySubjectDistinguishedName" />
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="NetSvc" binding="netNamedPipeBinding" bindingConfiguration="LargeSettingsPipe" name="netPipeEndPoint" contract="AppService.IArchService" />

        <endpoint address="mex" binding="mexTcpBinding" name="MetadatEndpointTcp" contract="IMetadataExchange" endpointConfiguration="" />
        <endpoint address="mex" binding="mexNamedPipeBinding" name="MetadatEndpointNetPipe" contract="IMetadataExchange" />
        <endpoint address="mex" binding="mexHttpBinding" name="MetadatEndpointHttpDual" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <dataContractSerializer maxItemsInObjectGraph="2147483646" />

          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />

          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

异常不是很有帮助。

此外,所有绑定都使用此特定方法失败。

我错过了什么?

编辑

似乎任何尝试返回 List(其中 T 可以是任何实体)的方法都会失败,并出现错误 An existing connection was forcibly closed by the remote host

编辑

客户端配置如下

<bindings>
  <netNamedPipeBinding>
    <binding name="netPipeEndPoint" maxBufferPoolSize="524288" maxReceivedMessageSize="6553600" >
      <readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="Transport" />
    </binding>
  </netNamedPipeBinding>
  <netTcpBinding>
    <binding name="netTcpEndPoint" maxBufferPoolSize="524288" maxReceivedMessageSize="6553600">
      <readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="Transport" />
    </binding>
  </netTcpBinding>

  <wsHttpBinding>
    <binding name="WSHttpBinding_IArchService" maxBufferPoolSize="524288" maxReceivedMessageSize="6553600">
      <readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None" />
    </binding>
  </wsHttpBinding>
</bindings>




<client>
  <endpoint address="http://localhost:8888/ArchService/" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IArchService" contract="ServiceReference.IArchService" name="WSHttpBinding_IArchService">
    <identity>
      <dns value="localhost" />
    </identity>
  </endpoint>
  <endpoint address="net.tcp://localhost:8808/ArchService" binding="netTcpBinding" bindingConfiguration="netTcpEndPoint" contract="ServiceReference.IArchService" name="netTcpEndPoint">
    <identity>
      <dns value="localhost" />
    </identity>
  </endpoint>
  <endpoint address="net.pipe://localhost/ArchService/NetSvc" binding="netNamedPipeBinding" bindingConfiguration="netPipeEndPoint" contract="ServiceReference.IArchService" name="netPipeEndPoint">
    <identity>
      <userPrincipalName value="SYS1\deb" />
    </identity>
  </endpoint>
</client>

编辑 3

我的其他相关实体类导致错误。我不知道为什么。这是他们

[Table("SubModuleMst", Schema = "setp")]
[DataContract(IsReference = true)] //prevents Infinite loop
[KnownType(typeof(ModuleMst))]
[KnownType(typeof(List<PageMst>))]

public class SubModule
{
    public SubModule()
    { }

    #region SubModule

    [Key]
    [DataMember]
    public int submoduleid { get; set; }
     [DataMember]
    public int moduleid { get; set; }
    [DataMember]
    [StringLength(100)]
    public string submodulename { get; set; }
    [DataMember]
    public int orderno { get; set; }
    [DataMember]
    [StringLength(1)]
    public string status { get; set; }
    [DataMember]
    public string crby { get; set; }
    [DataMember]
    public DateTime crdt { get; set; }
    [DataMember]
    public string modby { get; set; }
    [DataMember]
    public DateTime moddt { get; set; }
    #endregion

    [DataMember]
    public virtual ModuleMst Module { get; set; }
    [DataMember]
    public virtual List<PageMst> Pages { get; set; }
}

[Table("PageMst", Schema = "setp")]
[DataContract(IsReference = true)] //prevents Infinite loop
[KnownType(typeof(SubModule))]
public class PageMst
{
    public PageMst()
    { }

    #region PageMst

    [Key]
    [DataMember]
    public int pageid { get; set; }
    [DataMember]
    [Required]
    [ForeignKey("SubModule")]
    public int submoduleid { get; set; }
    [DataMember]
    [StringLength(1000)]
    [Required]
    public string pagename { get; set; }
    [DataMember]
    [StringLength(1000)]
    public string pageurl { get; set; }
    [DataMember]
    public int orderno { get; set; }
    [DataMember]
    [StringLength(1)]
    public string menuitm { get; set; }
    [DataMember]
    public int rptparentid { get; set; }
    [DataMember]
    [StringLength(1)]
    public string alertflag { get; set; }
    [DataMember]
    [StringLength(1)]
    public string status { get; set; }
    [DataMember]
    public string crby { get; set; }
    [DataMember]
    public DateTime crdt { get; set; }
    [DataMember]
    public string modby { get; set; }
    [DataMember]
    public DateTime moddt { get; set; }
    [DataMember]
    [StringLength(200)]
    public string paramids { get; set; }
    [DataMember]
    [StringLength(500)]
    public string repurl { get; set; }
    #endregion

    [DataMember]
    public virtual SubModule SubModule { get; set; }
}

编辑 4

现在我确认错误是由于 EF 创建的代理不能序列化 根据这篇 MSDN 文章 https://msdn.microsoft.com/en-us/library/ee705457(v=vs.100).aspx 还有根据this SO Article

【问题讨论】:

  • WCF/SOAP 对列表没有问题。我希望它归结为 DataContract 的特定部分。如果可能,我建议附加到服务以查看服务端异常详细信息。
  • @Glen Thomas 有趣的是我可以返回相同类型的对象。问题仅发生在所述类型的列表中!
  • 如果你能得到异常细节我们可以更快地解决这个问题
  • @GlenThomas 目前我正在使用 WCFTestClient 进行测试并发布了异常详细信息。我不知道在这种情况下如何附加到服务。能详细点吗?
  • 如果服务在本地机器的 IIS 中运行,您可以附加到应用程序池进程 (w3wp.exe)。在 Visual Studio 中,调试->附加到进程。然后,当抛出异常时,您应该会看到服务端异常。

标签: c# entity-framework wcf


【解决方案1】:

由于没有人回答我的问题,所以我在这里发布我所做的工作,希望它能帮助像我这样的其他人。

原来POCO代理类型不能被Windows Communication Foundation(WCF)as per this MSDN Article直接序列化或反序列化。

这是因为 EF 在 POCO 周围创建了一个 Wrapper 用于 Entity Tracking,它与 POCO 类不同,因此 WCF 无法序列化它,因为它只能序列化已知类型。

因此,为了使实体 SerializableWCF 需要完成以下操作之一。

  1. DBContext 配置中禁用代理创建。 (See this)
  2. 使用.AsNoTracking() (See this SO Post)
  3. 使用属性来装饰您的服务方法,这些方法返回使用 ProxyDataContractResolver 的实体(请参阅上面的 MSDN 文章中的演练)

我使用了第三种方法,我的服务最终返回 POCO 实体。演练文章中的代码是不言自明的,因此不在此处发布。

最后,我认为 DTO 是考虑 EF 的最佳方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多