【问题标题】:WCF contract mismatch problemWCF合约不匹配问题
【发布时间】:2010-11-18 20:43:26
【问题描述】:

我有一个客户端控制台应用程序与 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();

这是 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>

【问题讨论】:

    标签: c# .net asp.net wcf


    【解决方案1】:

    其他可能的原因:

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

    测试什么:

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

      只需将其添加到您的web.config 中的&lt;configuration&gt;在服务器上。如果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>
    
    • 确保 .svc 文件实际上会在浏览器中出现而没有错误。这会给你一些“第一次机会”的帮助。例如,如果您有一个不可序列化的对象,您将在下面收到此消息。请注意,它清楚地告诉您不能序列化的内容。确保您已启用“mex”端点并在浏览器中打开 .svc 文件。

    一个异常详细信息,可能由 IncludeExceptionDetailInFaults=true, 其值为: System.InvalidOperationException:在调用 WSDL 导出扩展: System.ServiceModel.Description.DataContractSerializerOperationBehavior 合约:http://tempuri.org/:IOrderPipelineService ----> System.Runtime.Serialization.InvalidDataContractException: 类型 'RR.MVCServices.PipelineStepResponse' 无法序列化。考虑标记 它与 DataContractAttribute 属性,并标记其所有 您想要序列化的成员 DataMemberAttribute 属性。

    【讨论】:

    • +1 谢谢 - 我被这个错误击中,我找到了你的答案,结果发现问题是由位掩码枚举引起的:当你设置了多个位时(因此没有匹配的枚举值)序列化失败!
    • 我遇到了类似的问题。服务很好。一位客户正在工作;但第二个客户端不工作:stackoverflow.com/questions/12420314/…
    【解决方案2】:

    对我来说,抛出此错误消息是因为默认情况下我的 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>
    

    maxItemsInObjectGraph 值最重要!
    我希望这对任何人都有帮助。

    【讨论】:

    【解决方案3】:

    我遇到了类似的问题。在花了 2 个小时思考这个问题并试图在网上找到答案之后,我决定遵循使用 System.Runtime.Serialization.DataContractSerializer 在服务器端序列化和反序列化返回值/对象的方法,最后发现我错过了在其中一个枚举上添加 EnumMember 属性。

    您可能会遇到类似的问题。

    这是帮助我解决问题的代码 sn-p:

          var dataContractSerializer = new System.Runtime.Serialization.DataContractSerializer(typeof(MyObject));
          byte[] serializedBytes;
            using (System.IO.MemoryStream mem1 = new System.IO.MemoryStream())
            {
                dataContractSerializer.WriteObject(mem1, results);
                serializedBytes = mem1.ToArray();
            }
    
            MyObject deserializedResult;
            using (System.IO.MemoryStream mem2 = new System.IO.MemoryStream(serializedBytes))
            {
                deserializedResult = (MyObject)dataContractSerializer.ReadObject(mem2);
            }
    

    【讨论】:

    • 这是本地调试最快的选项,谢谢!
    【解决方案4】:

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

    代码:

    ServiceReference1.Service1Client client = new WCFTest.ConsoleClient.ServiceReference1.Service1Client("NetTcpBinding_IService1");    
    client.PrintMessage("Hello!");
    

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

    <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>
    

    【讨论】:

    • 是的,可能不是 - 错误消息还提供了另外两种可能的原因:“.....,会话过早关闭或内部服务器错误。”
    【解决方案5】:

    如果您的 WCF 中有两个具有相同名称和参数的方法,则会引发此错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2012-07-26
      相关资源
      最近更新 更多