WCF:客户端配置中的合同 'X' 与服务合同中的名称不匹配
WCF: The contract 'X' in client configuration does not match the name in service contract
当我在 VS 中尝试 运行 我的 WCF 服务时显示此错误消息,我试图弄清楚 'client configuration' 和 'service contract' 实际指的是什么:
客户端配置中的合同'IMyService'与服务合同中的名称不匹配
我假设服务合同部分指的是:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]
public interface IMyService
{
// CODEGEN: Generating message contract since the operation MyService is neither RPC nor document wrapped.
[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")]
[System.ServiceModel.XmlSerializerFormatAttribute()]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(Task))]
SendResponse Request(SendRequest request);
}
知道 客户端配置 指的是什么吗?
编辑:在我的 web.config 中,我有 system.serviceModel 的这个部分:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyServiceBinding">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="XXX.YYY.MyService">
<endpoint binding="basicHttpBinding" bindingConfiguration="MyServiceBinding" name="MyServiceSendHttps"
contract="IMyService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost" />
</baseAddresses>
</host>
</service>
</services>
查看项目中的 app.config 文件。
如果您不以编程方式配置客户端,则 app.config 文件必须包含客户端配置节点。
更新:
您的第一个代码片段包括这一行:
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]`.
在 "ConfigurationName" 属性 的文档中:
https://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.configurationname%28v=vs.110%29.aspx
我们可以阅读:
The name used to locate the service element in an application configuration file. The default is the name of the service implementation class.
所以,我们有:
服务实现的名称 class 是 "XXX.YYY.MyService",并且(在第二个代码片段中)我们看到“”但是 属性 的 ConfigurationName 值是 "IMyService".
如果您只是从第
行删除 'ConfigurationName = "IMyService" '
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]
像这样:
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy")]
应该可以解决问题。
我遇到了同样的问题,我花了很多时间寻找解决方案。然后我发现 this article 关于 WCF 工具生成的代码 svcutil.exe.
不保证生成的 C# 代码也适用于服务端契约。在我的例子中,问题出在 ReplyAction = "*" (我也在问题中看到)。根据MSDN documentation:
Specifying an asterisk in the service instructs WCF not to add a reply
action to the message, which is useful if you are programming against
messages directly.
改变后
[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")]
到
[System.ServiceModel.OperationContractAttribute(Action = "")]
问题解决了吗
当我在 VS 中尝试 运行 我的 WCF 服务时显示此错误消息,我试图弄清楚 'client configuration' 和 'service contract' 实际指的是什么:
客户端配置中的合同'IMyService'与服务合同中的名称不匹配
我假设服务合同部分指的是:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]
public interface IMyService
{
// CODEGEN: Generating message contract since the operation MyService is neither RPC nor document wrapped.
[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")]
[System.ServiceModel.XmlSerializerFormatAttribute()]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(Task))]
SendResponse Request(SendRequest request);
}
知道 客户端配置 指的是什么吗?
编辑:在我的 web.config 中,我有 system.serviceModel 的这个部分:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyServiceBinding">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="XXX.YYY.MyService">
<endpoint binding="basicHttpBinding" bindingConfiguration="MyServiceBinding" name="MyServiceSendHttps"
contract="IMyService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost" />
</baseAddresses>
</host>
</service>
</services>
查看项目中的 app.config 文件。 如果您不以编程方式配置客户端,则 app.config 文件必须包含客户端配置节点。
更新: 您的第一个代码片段包括这一行:
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]`.
在 "ConfigurationName" 属性 的文档中: https://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.configurationname%28v=vs.110%29.aspx 我们可以阅读:
The name used to locate the service element in an application configuration file. The default is the name of the service implementation class.
所以,我们有:
服务实现的名称 class 是 "XXX.YYY.MyService",并且(在第二个代码片段中)我们看到“
如果您只是从第
行删除 'ConfigurationName = "IMyService" '[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]
像这样:
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy")]
应该可以解决问题。
我遇到了同样的问题,我花了很多时间寻找解决方案。然后我发现 this article 关于 WCF 工具生成的代码 svcutil.exe.
不保证生成的 C# 代码也适用于服务端契约。在我的例子中,问题出在 ReplyAction = "*" (我也在问题中看到)。根据MSDN documentation:
Specifying an asterisk in the service instructs WCF not to add a reply action to the message, which is useful if you are programming against messages directly.
改变后
[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")]
到
[System.ServiceModel.OperationContractAttribute(Action = "")]
问题解决了吗