Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: The contract 'X' in client configuration does not match the name in service contract

Tags:

c#

wcf

This error message shows when I try run my WCF service in VS, and I'm trying to figure out what it's actually referring to by 'client configuration' and 'service contract':

The contract 'IMyService' in client configuration does not match the name in service contract

I assume the service contract part refers to this:

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

Any ideas what the client configuration refers to?

Edit: In my web.config I have this section for 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>
like image 595
FBryant87 Avatar asked Nov 29 '22 10:11

FBryant87


2 Answers

I had the same problem and I spent many hours looking for the solution. Then I found this article about generated code by the WCF tool svcutil.exe.

Generated C# code is not guaranteed to be also suitable for a service-side contract. In my case the problem was in ReplyAction = "*" (which I see also in the question). According to 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.

After changing

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

to

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

was the problem solved.

like image 87
Tomas Avatar answered Dec 04 '22 16:12

Tomas


See app.config file in your project. If you don't configure client programmatically, then app.config file must include a node of configuration of clients.

Update: Your first code snippet includes this line:

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

In documentation for "ConfigurationName" property: https://msdn.microsoft.com/en-us/library/system.servicemodel.servicecontractattribute.configurationname%28v=vs.110%29.aspx we can read:

The name used to locate the service element in an application configuration file. The default is the name of the service implementation class.

So, we have: name of the service implementation class are "XXX.YYY.MyService", and (in second code snippet) we see " <service name="XXX.YYY.MyService"> " but property's ConfigurationName value is "IMyService".

If you just remove 'ConfigurationName = "IMyService" ' from line

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

like this:

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

that should fix the issue.

like image 23
Alexander Kiselev Avatar answered Dec 04 '22 15:12

Alexander Kiselev