WCF合同不匹配问题

时间:2009-08-12 06:01:03

标签: c# .net 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();

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

5 个答案:

答案 0 :(得分:32)

其他可能的原因:

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

测试内容:

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

    只需在<configuration> ON THE SERVER 中的web.config下添加此项。如果目录不存在,请创建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文件。
  

ExceptionDetail,可能由。创建   IncludeExceptionDetailInFaults = TRUE,   其价值是:       System.InvalidOperationException:在对a的调用中抛出异常   WSDL导出扩展:   System.ServiceModel.Description.DataContractSerializerOperationBehavior        合同:http://tempuri.org/:IOrderPipelineService   ----&GT; System.Runtime.Serialization.InvalidDataContractException:   的类型   'RR.MVCServices.PipelineStepResponse'   无法序列化。考虑标记   它与DataContractAttribute   属性,并标记其所有   你想用序列化的成员   DataMemberAttribute属性。

答案 1 :(得分: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>

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

答案 2 :(得分:2)

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

您可能面临类似的问题。

以下是帮助我解决问题的代码段:

      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);
        }

答案 3 :(得分:1)

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

代码:

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>

答案 4 :(得分:0)

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

相关问题