Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF: Could not find default endpoint element that references contract 'IService' in the ServiceModel client configuration section. when hosting in IIS

I have a WCF service which is being hosted in IIS. I have a WCF client also (a console application). I have used svcutil to build the proxy class and configuration file and then added those to my client project. It built properly. But when I tried to run the program, it is throwing the below exception

Could not find default endpoint element that references contract 'IService' 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.

//My client program code

namespace MyFirstWCFClient
{
 class Program
 {
    static void Main(string[] args)
    {
        ServiceClient objClient = new ServiceClient();
        Console.WriteLine("Client calling the service....");

        string strName=Console.ReadLine();
        Console.WriteLine(objClient.HelloWorld("Shyju"));
        Console.Read();

    }
 }
}

Output.config file of my client is

  <?xml version="1.0" encoding="utf-8"?>
   <configuration>
    <system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" establishSecurityContext="true" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost/IISHostedserviceTest/Service.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
            contract="IService" name="WSHttpBinding_IService">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
     </client>
 </system.serviceModel>
</configuration>

and in the web.config of my service has the below configuration

   <system.serviceModel>
   <services>
    <service name="Service" behaviorConfiguration="ServiceBehavior">
    <!-- Service Endpoints -->
    <endpoint address="http://localhost/IISHostedserviceTest/Service.svc" binding="wsHttpBinding" contract="IService">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
     </behavior>
   </serviceBehaviors>
  </behaviors>
 </system.serviceModel>

I used this(http://www.wcftutorial.net/WCF-IIS-Hosting.aspx) tutorial to have a try on WCF.

Can anyone guide me how to resolve this ?

like image 968
Shyju Avatar asked Nov 27 '22 03:11

Shyju


2 Answers

Quick question: if your client app is called myclient.exe, is your config in the same directory as the EXE and called MyClient.exe.config ?

You can't just take the output.config from svcutil - you will need to either add a app.config to your client console project (which will be renamed to myclient.exe.config when compiling), or you need to copy/rename the output.config to myclient.exe.config in order for your client app to find and use it.

like image 110
marc_s Avatar answered Dec 04 '22 16:12

marc_s


You have to use the constructor for the client that specifies the endpoint configuration name, eg.

objClient = new ServiceClient ("WSHttpBinding_IService");

That will tell the proxy to use the configuration you specified in the config file.

like image 40
axel_c Avatar answered Dec 04 '22 16:12

axel_c