16

I know this has been beaten to death, but I cannot get this to work as it should. I have a WCF service with several contracts. They all work fine when calling them directly e.g. http://merlin.com/CompanyServices/CompanyWcfService.svc/Get_Document_Dates_Received/223278 I have used this WCF service successfully on InfoPath Forms and Nintex Workflows. Now I create a simple ASP.Net application, such as was done in http://www.webcodeexpert.com/2013/04/how-to-create-and-consume-wcf-services.html. I was able to add a service reference as described in the article. I added a button the form, and added the following code in the Button1_Click event:

protected void Button1_Click(object sender, EventArgs e)
{
    ServiceReference1.CompanyWcfServiceClient x = new ServiceReference1.CompanyWcfServiceClient();
    var result = x.Get_Document_Dates_Received("223278");
}

when I click on the button I get the error:

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

So I tried adding the following to the web.config: (copied directly from the web.config file of the CompanyWcfService.

<system.serviceModel>
<services>
  <service name="CompanyWcfServices.CompanyWcfService" behaviorConfiguration="ServiceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="CompanyWcfServices.ICompanyWcfService" behaviorConfiguration="webHttpEndpointBehavior" >
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">                                                                
    </endpoint>
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding>
      <security mode="None">
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name ="webHttpEndpointBehavior">
      <webHttp helpEnabled ="true" faultExceptionEnabled="true" automaticFormatSelectionEnabled="true"/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

I get the same exact error, there has to be something else going on.

I finally gave up and called the service like this:

HttpWebRequest request = WebRequest.Create(@"http://merlin/Companyservices/CompanyWcfService.svc/Get_Document_Dates_Received/223278") as HttpWebRequest;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = null;

var result = "";
try
{
    response = request.GetResponse() as HttpWebResponse;
    if (response.StatusCode == HttpStatusCode.OK)
    {
        using (Stream stream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(stream, Encoding.UTF8);
            result = reader.ReadToEnd();
        }
    }
}
catch (Exception ex)
{
    result = "";
}

I have spent hours reading posts and most of them suggest to copy the config information to the web.config file. This seems problematic to me (besides the fact that it doesn't seem to work). What if I need to consume a third party WCF service? Do I have to request the config information from the third party? And Visa Versa, if I create a WCF service designed to be consumed by third parties, do I need to provide them the config file as well?

Simon Martin
  • 4,203
  • 7
  • 56
  • 93
user1337493
  • 373
  • 2
  • 4
  • 14

7 Answers7

23

The error indicates that you don't have an endpoint defined in the client configuration section. When you add the service reference to your project it should create the client section for you. If not then in the web.config for your app within the system.serviceModel section add the following

<client>
  <endpoint 
      name="CompanyWcfService_webhttpBinding"
      address="http://merlin.com/CompanyServices/CompanyWcfService.svc" 
      binding="webHttpBinding" 
      contract="CompanyWcfServices.ICompanyWcfService" 
      behaviorConfiguration="webHttpEndpointBehavior"
  />
</client>
Matt Klepeis
  • 1,724
  • 1
  • 14
  • 25
  • Also. In the system.ServiceModel section in your app web.config you shouldn't need the services section. I'd delete that. You only need to defined items referenced in the client section. Like the behaviorConfiguration or binding settings if you customized. – Matt Klepeis Jul 11 '13 at 02:05
  • Matt, thanks for the response. Sorry though, I still get the same error. – user1337493 Jul 11 '13 at 13:19
  • This problem then seems to be a mismatch between the service and the web.config in the application. The error indicated that a service with the contract 'ServiceReference1.ICompanyWcfService' cannot be found. Notice in your web.config in the web app the contract is "CompanyWcfServices.ICompanyWcfService". Did you by chance create a service, add a reference in your web App to it and then change the service namespace? – Matt Klepeis Jul 14 '13 at 00:57
  • I would check the namespace in the service, as well as the contract in the endpoint definition and then re-add the service reference to the app and check the web.config for the app to see if in added what you needed. If you could post the system.ServiceModel section of the service then we can compare the two. – Matt Klepeis Jul 14 '13 at 01:01
  • Spot on! In our case everything worked properly in dev, but when on the production machine gave the exception: _The type initializer for XXX.yyy.zzz' threw an exception_. – StinkyCat Mar 23 '17 at 12:19
15

If we have layered architecture make sure to

1) add app.config in "all projects"
2) add service config details in all app.config
3) run the project
hkutluay
  • 6,794
  • 2
  • 33
  • 53
Anand
  • 151
  • 1
  • 2
  • Many copies of the same configuration "just in case", one day we update the WCF configuration but forget one copy, leading to different configurations in different files. This is the same for db connection strings. Microsoft is not doing this very well, seems like amateurism. – mins Jun 10 '20 at 22:25
7

If your project is referencing a library and trying to use the WCF functions from the functions of that library, then you can try copying the client endpoint from the project config file to the dll's config file. Some thing like this happened to me a while ago as the library that I referenced in the project would not use the project config file (in which the client end point was configured since the service was being referenced there) but its own so the result was the system could not find the endpoint configurations.

AbbasFaisal
  • 1,428
  • 2
  • 18
  • 21
  • Same here--in my case it was a test project referencing a shared function in my main project. The app.config being referenced was the one in the test project, not the main project. Thanks! – Jeff Aug 07 '17 at 18:51
5

In my case I had a WPF project referencing an external UserControl which had a service reference. I had to add the service reference to the main project as well.

Aleksandr Albert
  • 1,777
  • 1
  • 19
  • 26
  • 1
    Thanks! This was the same issue I had – Chance May 06 '16 at 20:04
  • In my case, it was a library project and a desktop application project so firstly I added the reference to library project but after looking at your answer had added the same reference to desktop application project, worked! similar issue! – hi0001234d Sep 02 '19 at 08:38
3

Adding binding and client values from app.config to default web.config resolved my issue.

1

Actually the trick to this one was to use the svcutil.exe to create the proxy. I had been trying to create the proxy through Visual Studio "Add Service" wizard. Once I did that, the configuration was a breeze.

SvcUtil.exe

user1337493
  • 373
  • 2
  • 4
  • 14
0

When it comes down to WCF, it typically requires the configuration to be defined within the config file of the executable that calls it.

Thus, if you are unfortunate enough to having to call a WCF DLL from within a VB6 program (as may be the case when using a COM-interop .NET module, for example), and you need to debug the VB6 program, then you will need to create a VB6.exe.config file in the directory where VB6.exe is located.

Failing to do the above may cause a "Could not find default endpoint element that references contract".

As a workaround, one can load the dll's config file at runtime and then call the constructor of the used Service with a Binding and an EndpointAddress as parameters (obtained from the dll's config).

Wolfgang Grinfeld
  • 870
  • 10
  • 11