c# - WCF契约不匹配问题

标签 c# .net asp.net wcf

我有一个与 WCF 服务通信的客户端控制台应用程序,但出现以下错误: “服务器没有提供有意义的回复;这可能是由于契约(Contract)不匹配、 session 过早关闭或内部服务器错误造成的。”

我认为这是因为契约(Contract)不匹配,但我不明白为什么。该服务本身运行良好,并且这两个部分一起工作,直到我添加了模拟代码。

谁能看出哪里出了问题?

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

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>

最佳答案

其他可能的原因:

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

要测试的内容:

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

    只需在 <configuration> 下添加这个在你的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 文件。

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior contract: http://tempuri.org/:IOrderPipelineService ----> System.Runtime.Serialization.InvalidDataContractException: Type 'RR.MVCServices.PipelineStepResponse' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.

关于c# - WCF契约不匹配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1264431/

相关文章:

c# - Linq/Enumerable Any Vs 包含

c# - Stackoverflow 调用 ManagementScope.Connect();

c# - 为什么选择通用函数成员而不是非通用函数成员?

c# - ASP.NET MVC Razor View 建立在两个对象的 Viewmodel 之上。架构建议如何更新引用两个对象的计算字段

asp.net - 创建帐户后不让用户登录

javascript - 如何在传单 map 上绘制标记

c# - 如何知道正在更改服务器上的共享文件夹的计算机的主机名/IP 地址

c# - 在值后显示货币符号

c# - 将 WCF 契约(Contract)移动到单独的 dll

c# - System.IO.FileSystemWatcher.CompletionStatusChanged 处的 System.ArgumentOutOfRangeException