【问题标题】:WCF: The contract 'X' in client configuration does not match the name in service contractWCF:客户端配置中的合同“X”与服务合同中的名称不匹配
【发布时间】:2015-12-21 23:33:31
【问题描述】:

当我尝试在 VS 中运行我的 WCF 服务时显示此错误消息,并且我试图通过“客户端配置”和“服务合同”找出它实际指的是什么:

客户端配置中的合同“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>

【问题讨论】:

标签: c# wcf


【解决方案1】:

我遇到了同样的问题,我花了很多时间寻找解决方案。然后找到this article关于WCF工具svcutil.exe生成的代码。

不保证生成的 C# 代码也适用于服务端合同。就我而言,问题出在ReplyAction =“*”(我也在问题中看到)。根据MSDN documentation

在服务中指定星号指示 WCF 不添加回复 对消息的操作,如果您正在针对 直接发消息。

改变后

[System.ServiceModel.OperationContractAttribute(Action = "", ReplyAction = "*")]

[System.ServiceModel.OperationContractAttribute(Action = "")]

问题解决了吗?

【讨论】:

    【解决方案2】:

    查看项目中的 app.config 文件。 如果您不以编程方式配置客户端,则 app.config 文件必须包含客户端配置节点。

    更新: 您的第一个代码 sn-p 包括这一行:

    [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 我们可以阅读:

    用于在应用程序配置文件中定位服务元素的名称。默认是服务实现类的名称。

    所以,我们有: 服务实现类的名称是“XXX.YYY.MyService”,并且(在第二个代码 sn-p 中)我们看到“”,但属性的 ConfigurationName 值为“IMyService”。

    如果您只是从行中删除 'ConfigurationName = "IMyService" '

    [System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy", ConfigurationName = "IMyService")]
    

    像这样:

    [System.ServiceModel.ServiceContractAttribute(Namespace = "http://xxx/yyy")]
    

    这应该可以解决问题。

    【讨论】:

    • 向问题添加了 web.config - 配置文件中是否有客户端配置示例?
    • 嗯,这不太奏效,但是当我把它全部拿出来并在方法上使用 [ServiceContract] 和 [OperationContract] 时,它起作用了!谢谢。
    猜你喜欢
    • 2011-12-27
    • 2019-06-27
    • 1970-01-01
    • 2010-10-13
    • 2018-12-28
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多