WCF合同不匹配问题

16 .net c# asp.net wcf

我有一个客户端控制台应用程序与WCF服务通信,我收到以下错误:"服务器没有提供有意义的答复;这可能是由合同不匹配,过早的会话关闭或内部服务器错误引起的."

我认为这是因为合同不匹配但我无法弄清楚原因.该服务本身运行良好,两个部分一起工作,直到我添加了模拟代码.

任何人都可以看到有什么问题?

这是客户端,全部用代码完成:

NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

EndpointAddress endPoint = new EndpointAddress(new Uri("net.tcp://serverName:9990/TestService1"));
ChannelFactory<IService1> channel = new ChannelFactory<IService1>(binding, endPoint);
channel.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
IService1 service = channel.CreateChannel();
Run Code Online (Sandbox Code Playgroud)

这是WCF服务的配置文件:

<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="MyBinding">
          <security mode="Message">
            <transport clientCredentialType="Windows"/>
            <message clientCredentialType="Windows" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCFTest.ConsoleHost2.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"  />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceAuthorization impersonateCallerForAllOperations="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="WCFTest.ConsoleHost2.Service1Behavior"
          name="WCFTest.ConsoleHost2.Service1">
        <endpoint address="" binding="wsHttpBinding" contract="WCFTest.ConsoleHost2.IService1">
          <identity>
            <dns value="" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint binding="netTcpBinding" bindingConfiguration="MyBinding"
            contract="WCFTest.ConsoleHost2.IService1" />
        <host>
          <baseAddresses>
            <add baseAddress="http://serverName:9999/TestService1/" />
            <add baseAddress="net.tcp://serverName:9990/TestService1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

Sim*_*ver 32

其他可能的原因:

  • 尝试在没有默认构造函数的情况下序列化对象.
  • 试图序列化其他一些非序列化对象(例如Exception).要防止项目被序列化[IgnoreDataMember],请将属性应用于字段或属性.
  • 检查枚举字段以确保它们设置为有效值(或可以为空).在某些情况下,您可能需要向枚举添加0值.(不确定这一点的细节).

测试内容:

  • 至少为严重错误或异常配置WCF跟踪.如果启用任何进一步的跟踪,请注意观察文件大小.在许多情况下,这将提供一些非常有用的信息.

    只需将其添加<configuration>到您的web.config ON THE SERVER中即可.log如果目录不存在,请创建该目录.

 <system.diagnostics>
     <sources>
       <source name="System.ServiceModel"
               switchValue="Error, Critical"
               propagateActivity="true">
         <listeners>
           <add name="traceListener"
               type="System.Diagnostics.XmlWriterTraceListener"
               initializeData= "c:\log\WCF_Errors.svclog" />
         </listeners>
       </source>
     </sources>
   </system.diagnostics>
Run Code Online (Sandbox Code Playgroud)
  • 确保.svc文件实际上会在浏览器中出现而不会出现错误.这将为您提供一些"第一次机会"帮助.例如,如果您有一个非序列化对象,您将在下面收到此消息.请注意,它清楚地告诉您什么不能序列化.确保启用了"mex"端点并在浏览器中显示.svc文件.

ExceptionDetail,可能由IncludeExceptionDetailInFaults = true创建,其值为:System.InvalidOperationException:在调用WSDL导出扩展时抛出异常:System.ServiceModel.Description.DataContractSerializerOperationBehavior contract:http://tempuri.org/: IOrderPipelineService ----> System.Runtime.Serialization.InvalidDataContractException: 类型'RR.MVCServices.PipelineStepResponse'无法序列化.请考虑使用DataContractAttribute属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员.

  • +1谢谢 - 我遇到了这个错误,我找到了你的答案,结果发现问题是由位掩码枚举引起的:当你设置了多个位(因此没有一个枚举值匹配)时序列化失败! (2认同)

小智 8

对我来说,抛出此错误消息是因为我的web.config服务行为默认情况下具有较低的消息限制,因此当WCF返回说200000字节且我的限制为64000字节时,响应被截断,因此您得到"..没有意义的答复".它有意义,它被截断并且无法解析.

我将粘贴我修复该问题的web.config更改:

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="YourNameSpace.DataServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <serviceTimeouts transactionTimeout="05:05:00" />
      <serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500"
       maxConcurrentInstances="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
Run Code Online (Sandbox Code Playgroud)

maxItemsInObjectGraph值是最重要的!
我希望这可以帮助任何人.


小智 1

好的,我刚刚更改了客户端,因此它使用配置文件而不是代码,但我得到了相同的错误!

代码:

ServiceReference1.Service1Client client = new WCFTest.ConsoleClient.ServiceReference1.Service1Client("NetTcpBinding_IService1");    
client.PrintMessage("Hello!");
Run Code Online (Sandbox Code Playgroud)

这是客户端的配置文件,从服务中新鲜生成......这让我认为这可能不是合同不匹配错误

<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="65536">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://servername:9999/TestService1/" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
                name="WSHttpBinding_IService1">
                <identity>
                    <dns value="&#xD;&#xA;          " />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://serverName:9990/TestService1/" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IService1" contract="ServiceReference1.IService1"
                name="NetTcpBinding_IService1">
                <identity>
                    <userPrincipalName value="MyUserPrincipalName " />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)