C# WCF合同不匹配问题
我有一个客户端控制台应用程序正在与WCF服务对话,我收到以下错误: 服务器没有提供有意义的答复;这可能是由于协定不匹配、会话过早关闭或内部服务器错误造成的 我想这是因为合同不匹配,但我不明白为什么。服务本身运行得很好,在我添加模拟代码之前,这两个部分一直在一起工作 有人能看出哪里出了问题吗 以下是客户端,所有操作都是通过代码完成的:C# WCF合同不匹配问题,c#,.net,asp.net,wcf,C#,.net,Asp.net,Wcf,我有一个客户端控制台应用程序正在与WCF服务对话,我收到以下错误: 服务器没有提供有意义的答复;这可能是由于协定不匹配、会话过早关闭或内部服务器错误造成的 我想这是因为合同不匹配,但我不明白为什么。服务本身运行得很好,在我添加模拟代码之前,这两个部分一直在一起工作 有人能看出哪里出了问题吗 以下是客户端,所有操作都是通过代码完成的: NetTcpBinding binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.
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();
NetTcpBinding=new NetTcpBinding();
binding.Security.Mode=SecurityMode.Message;
binding.Security.Message.ClientCredentialType=MessageCredentialType.Windows;
EndpointAddress endPoint=新的EndpointAddress(新Uri(“网络”)。tcp://serverName:9990/TestService1"));
ChannelFactory通道=新的ChannelFactory(绑定,端点);
channel.Credentials.Windows.AllowedImpersonationLevel=TokenImpersonationLevel.Impersonation;
IService1服务=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>
好的,我刚刚更改了客户端,使其使用配置文件而不是代码,我得到了相同的错误 代码:
servicerence1.Service1Client=new WCFTest.ConsoleClient.servicerence1.Service1Client(“NetTcpBinding_-IService1”);
client.PrintMessage(“你好!”);
这是客户机的配置文件,刚从服务中生成。。。这让我觉得这可能不是一个合同错配错误
其他可能的原因:
- 尝试在没有默认构造函数的情况下序列化对象
- 尝试序列化其他类型的不可序列化对象(例如异常)。要防止序列化项,请将
属性应用于字段或属性[IgnoreDataMember]
- 检查枚举字段,确保它们设置为有效值(或可为空)。在某些情况下,可能需要向枚举添加0值。(不确定这一点的细节)
- 至少针对关键错误或异常。如果启用任何进一步的跟踪,请小心观察文件大小。这将在许多情况下提供一些非常有用的信息
只需在服务器的
中的web.config
下添加此项即可。如果不存在
目录,则创建该目录log
---->System.Runtime.Serialization.InvalidDataContractException:
类型
'RR.MVCServices.PipelineStepResponse'
不能序列化。< /强>考虑标记
它使用datacontract属性
属性,并标记其所有
要使用序列化的成员
DataMemberAttribute属性
对我来说,抛出此错误消息是因为默认情况下,web.config服务行为的消息限制较低,因此当WCF返回200000字节,而我的限制为64000字节时,响应被截断,因此您会得到“…无意义的回复”。它很有意义,只是被截断了,无法解析
我将粘贴修复该问题的web.config更改:
maxItemsInObjectGraph值是最重要的
我希望这对任何人都有帮助。如果您的WCF中有两个名称和参数相同的方法,它将抛出此错误。我遇到了类似的问题。在花了2个小时的时间在这个问题上,并试图在线找到答案之后,我决定在服务器端使用System.Runtime.Serialization.DataContractSerializer对返回值/对象进行序列化和反序列化,最后发现我没有在其中一个枚举上添加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);
}
是的,可能不是-错误消息还提供了两种可能的原因:“…,会话过早关闭或内部服务器错误。”+1谢谢-我被此错误击中,找到了您的答案,结果发现问题是由位掩码枚举引起的:当您有多个位集时(因此没有一个匹配的枚举值)序列化失败!我也有类似的问题。服务很好。一名客户正在工作;但第二个客户不工作:我遇到了类似的问题。服务很好。一名客户正在工作;但是第二个客户端不工作:这是本地调试的最快选项,谢谢!