Duplex WCF Project In Visual Studio InvalidOperationException

213 Views Asked by At

I'm a beginner in C# and I'm trying to build a simple library project the service is working nicely but in client always throw exception in service object

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

where it could be the problem ?

client config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup> 
    <supportedRuntime version="v4.0" 
                      sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.serviceModel>
    <bindings>
      <wsDualHttpBinding>
        <binding name="WSDualHttpBinding_IServiceLibrary" />
      </wsDualHttpBinding>
    </bindings>
    <client>
      <endpoint addrres="http://localhost:8733/Design_Time_Addresses/WCF/Service1/"
                binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_IServiceLibrary"
                contract="ServiceReference.IServiceLibrary" 
                name="WSDualHttpBinding_IServiceLibrary">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

service config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="WCF.Properties.Settings.DemaLibraryConnectionString"
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\databases\DemaLibrary.mdf;Integrated Security=True;Connect Timeout=30"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
      app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WCF.ServiceLibrary">
        <endpoint address="" binding="wsDualHttpBinding"
                  contract="WCF.IServiceLibrary">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding"
                  contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/WCF/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
              set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="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>
</configuration>

host

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" 
                      sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
      app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WCF.ServiceLibrary">
        <endpoint address="" binding="wsDualHttpBinding"
                  contract="WCF.IServiceLibrary">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/WCF/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>
2

There are 2 best solutions below

0
Derrick Moeller On BEST ANSWER

Your client requires an app.config file that contains the endpoint of your service. Something like this...

app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBinding">
          <security mode="Transport" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://yourdomain.com/YourService.svc"
                binding="wsHttpBinding" bindingConfiguration="wsHttpBinding"
                contract="YourDomain.Contracts.Service.IService" />
    </client>   
  </system.serviceModel>
</configuration>
4
Tim On

My first thought was you hadn't copied the service library's <system.serviceModel> section to the config for the application that is hosting the service, but it looks like you did.

However, your contract name is wrong. You have ServiceReference.IServiceLibrary for the contract in your client, but the service has WCF.IServiceLibrary. Even if the code is exactly the same, the namespace difference will make them be viewed as different.

On your client, change the contract name to WCF.IServiceLibrary like this:

<client>
  <endpoint address="http://localhost:8733/Design_Time_Addresses/WCF/Service1/"
            binding="wsDualHttpBinding"
            bindingConfiguration="WSDualHttpBinding_IServiceLibrary"
            contract="WCF.IServiceLibrary"
            name="WSDualHttpBinding_IServiceLibrary">
    <identity>
      <dns value="localhost" />
    </identity>
  </endpoint>
</client>