WCF: Contract name could be any on the client side.Sometimes we’ve got a very clear fault message:          <faultstring xml:lang=”en-US”>Could not find default endpoint element that references contract ‘ServiceProvider_configuration_Ref.ConfigurationServicePortType’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.</faultstring>
         <detail>
            <ExceptionDetail xmlns=”http://schemas.datacontract.org/2004/07/System.ServiceModel” xmlns:i=”http://www.w3.org/2001/XMLSchema-instance”>
               <HelpLink i:nil=”true”/>
               <InnerException i:nil=”true”/>
               <Message>Could not find default endpoint element that references contract ‘ServiceProvider_configuration_Ref.ConfigurationServicePortType’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.</Message>
.
It means two things do not conform each other:1)      The contract attribute in the endpoint element of the config file:   <endpoint address=https://ie-services-configuration.ServiceProvider.combinding=customBinding bindingConfiguration=customBinding_Configuration    contract=MyCompany.MyProject.ServiceProvider_configuration_Ref.ConfigurationServicePortType      name=ConfigurationServiceHttpPort /> 2)       And ConfigurationName property of the ServiceContract attribute in the proxy code. It usually generated by Add Service Reference wizard and placed in the hidden file the Reference.cs

     [System.CodeDom.Compiler.GeneratedCodeAttribute(“System.ServiceModel”, “3.0.0.0”)]    [System.ServiceModel.ServiceContractAttribute(
Namespace=“http://ws.ServiceProvider.com”, ConfigurationName=“ServiceProvider_configuration_Ref.ConfigurationServicePortType”)]
    public interface ConfigurationServicePortType {
 How I could get this situation? Very easily. For instance when I am editing my .config file with the Microsoft Service Configuration Editor and selecting to fix my endpoint -> contract name. I choose the dll with the definition of the service contract and there we go! It shows me not the ServiceProvider_configuration_Ref.ConfigurationServicePortType but MyCompany. MyProject.ServiceProvider_configuration_Ref.ConfigurationServicePortType and this creates the error mentioned above. We fix this error easily by changing the config file. Of course we can change the Reference.sc but it is generated be wizard and after updating this reference the error appears again. Is it a whole story? Nope. It is more interesting. Say we have the source code for this Web-service. Then we are creating the client for this Web-service. We are waiting that the full name of the service class should be the same in the service source code and in the client proxy code. It’s wrong! It is completely wrong.That means the “loose coupling” J What the client could get from the service it is the metadata: one or many wsdl-s and schemas (xsd).  Inside there is only the contract name. Usually it is the name of service interface or, if you apply the [ServiceContract] right to the service class, the service class name. The namespace of it does not passed by the metadata! When one of the client tools generates the proxy code it usually adds some new namespace. For example the Visual Studio adds the Default namespace parameter from the project properties.What about the service class name? The metadata passes the name under the [ServiceContract] attribute. And the client tool that generates the proxy code does not know is it the name of the interface or the name of the class. Then is just creates the interface with this name and with the [ServiceContract] attribute. Then it creates the class derived from this interface. And this class has auto generated name!What about the service contract full name?For instance:the source code of the web-service:namespace CMS_Stub { [ServiceContract(
Namespace = ” http://ws.ServiceProvider.com “)]
public interface ICMS_Stubpublic class CMS_StubService : ICMS_Stub the client proxy code:namespace MyCompany. MyProject.ServiceProvider_configuration_Ref {[ServiceContract(
Namespace = ” http://ws.ServiceProvider.com “,
ConfigurationName=“ServiceProvider_configuration_Ref.ICMS_Stub”)]
public interface ICMS_Stubpublic class CMS_StubClient: ICMS_Stub And what we see in the contract attribute in the endpoint element of the client config file?


  • CMS_Stub.ICMS_Stub?

  • CMS_Stub.CMS_StubService?

  • MyCompany.MySolution.MyProject.DownloadUri.ServiceProvider_configuration_Ref.ICMS_Stub?

  • MyCompany.MySolution.MyProject.DownloadUri.ServiceProvider_configuration_Ref.CMS_StubClient?

  • ServiceProvider_configuration_Ref.ICMS_Stub?
Yes, you are right, the last one, the ConfigurationName of the ServiceContract.