389

I've added a proxy to a webservice to a VS2008/.NET 3.5 solution. When constructing the client .NET throws this error:

Could not find default endpoint element that references contract 'IMySOAPWebService' in the ServiceModel client configuration section. This might be because no configuaration file was found for your application or because no endpoint element matching this contract could be found in the client element.

Searching for this error tells me to use the full namespace in the contract. Here's my app.config with full namespace:

<client>
  <endpoint address="http://192.168.100.87:7001/soap/IMySOAPWebService"
            binding="basicHttpBinding" bindingConfiguration="IMySOAPWebServicebinding"
            contract="Fusion.DataExchange.Workflows.IMySOAPWebService" name="IMySOAPWebServicePort" />
</client>

I'm running XP local (I mention this because a number of Google hits mention win2k3) The app.config is copied to app.exe.config, so that is also not the problem.

Any clues?

Ben
  • 54,723
  • 49
  • 178
  • 224
edosoft
  • 17,121
  • 25
  • 77
  • 111

35 Answers35

622

"This error can arise if you are calling the service in a class library and calling the class library from another project."

In this case you will need to include the WS configuration settings into the main projects app.config if its a winapp or web.config if its a web app. This is the way to go even with PRISM and WPF/Silverlight.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
L.R.
  • 6,229
  • 2
  • 15
  • 2
  • 1
    This wasn't the cause for my specific problem, but I'm sure this will help others. Thanks – edosoft Jul 05 '10 at 11:46
  • 10
    Is there any way to automatically merge the two? What if the class library updates its configuration? Are you just stuck remembering to update the copied config info in all projects that are referencing it? This fix seems to rely too much on the developer's vigilance... – Sean Hanley Nov 05 '10 at 13:30
  • 2
    I'm getting the same error for a WP7 app (Silverlight I believe) and it took me far too long to notice that `ServiceReferences.ClientConfig` is generated in the project directory. Copying the `` and `` elements from the file in my library to my main app (which were previously empty) got things working. – David Mason Apr 06 '11 at 03:10
  • OK, I did this too, and it worked, but I sure as heck would like to understand WHY it worked! It doesn't make sense to me that it would. – Rod Aug 25 '11 at 16:32
  • For Silverlight 4 I include the .ClientConfig file (from my library) to the main project as a link. This makes editing the settings of the file easier (vs. multiple copies in different projects). – Aardvark Aug 30 '11 at 15:51
  • 1
    What if the project that's calling it is a MSTest project? i.e. no app.config or web.config – BobC Sep 28 '11 at 21:03
  • 4
    The reason this happens (as I understand it) is that config values are read from the main project in a solution, be that web, winforms, wpf etc. Say for example you have a class library project to access a database, the connectionString entry will need to be in the main project config rather than the class library config. – Ciarán Bruen Dec 05 '11 at 16:35
  • 7
    So we can conclude that, if we use WCF in a library, it would be better to code settings directly like the link http://stackoverflow.com/questions/7688798/net-deploying-a-wcf-client-without-an-app-config shown. – Youngjae Jan 08 '12 at 09:28
  • 1
    I have even tried to add reference to Service from new project but I get same error – Saher Ahwal Jul 19 '12 at 21:05
101

I solved this (I think as others may have suggested) by creating the binding and endpoint address instances myself - because I did not want to add new settings to the config files (this is a replacement for some existing library code which is used widely, and previously used an older Web Service Reference etc.), and so I wanted to be able to drop this in without having add new config settings everywhere.

var remoteAddress = new System.ServiceModel.EndpointAddress(_webServiceUrl);

using (var productService = new ProductClient(new System.ServiceModel.BasicHttpBinding(), remoteAddress))
{
    //set timeout
    productService.Endpoint.Binding.SendTimeout = new TimeSpan(0,0,0,_webServiceTimeout);

    //call web service method
    productResponse = productService.GetProducts();
} 

Edit

If you are using https then you need to use BasicHttpsBinding rather than BasicHttpBinding.

0x777
  • 825
  • 8
  • 14
Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
  • 1
    This is a helpful answer. On a web service I am using, the custom endpoint had to be bound in the object's initial declaration. It wouldn't work if I tried to do it later. – Paul Morel Apr 03 '12 at 22:33
  • 2
    As much as I suspect that the top answer might have done the trick too, your solution worked, and it seems preferable to me hacking together my own config files. – Sam I am says Reinstate Monica Oct 15 '12 at 23:05
  • Works a charm! I much prefer being able to set the Endpoint in code rather than distributing an "app.config" file with my app. – Daniel Gee Oct 06 '13 at 23:44
  • 2
    If it's an Https web service remember to change BasicHttpBinding() to BasicHttpsBinding() – Anthony Nov 06 '13 at 15:33
  • This solution is best for applications like EXCEL-DNA which does not have app.config or web.config. – user781700 May 07 '14 at 15:48
  • with this solution I can deploy only the .exe without the .exe.config! – lukerobbers Sep 29 '16 at 13:40
  • Good answer I wanted to create a proxy API using a Web API 2 solution but not have to put anything in the Web.Config this answer allowed me to keep the communcation close to the actual service method. Good work! – Trevor Apr 07 '17 at 16:12
83

Having tested several options, I finally solved this by using

contract="IMySOAPWebService"

i.e. without the full namespace in the config. For some reason the full name didn't resolve properly

jcollum
  • 43,623
  • 55
  • 191
  • 321
edosoft
  • 17,121
  • 25
  • 77
  • 111
  • 4
    It seems, that contract name has to be written exactly in the same way as the client. In my case, I had `var proxy = new ExternalServices.ServiceClient("MyServiceEndpoint");` It worked when the I added the namespace to contract: `contract="ExternalServices.IMyService"` – Anatoly Mironov Apr 16 '12 at 11:37
  • This didnt work for me. My problem might be a little bit different . I get this error time to time not always. What might be the issue. Could error mighht be at service side?_Thanks – albatross Feb 22 '16 at 15:34
61

I've had this same issue. It turns out that for a web REFERENCE, you have to supply the URL as the first parameter to the constructor:

new WebService.WebServiceSoapClient("http://myservice.com/moo.aspx");

For a new style web SERVICE REFERENCE, you have to supply a name that refers to an endpoint entry in the configuration:

new WebService.WebServiceSoapClient("WebServiceEndpoint");

With a corresponding entry in Web.config or App.config:

<client>
      <endpoint address="http://myservice.com/moo.aspx"
        binding="basicHttpBinding" 
        bindingConfiguration="WebService"
        contract="WebService.WebServiceSoap"
        name="WebServiceEndpoint" />
    </client>
  </system.serviceModel>

Pretty damn hard to remove the tunnel vision on "it worked in an older program"...

Andomar
  • 232,371
  • 49
  • 380
  • 404
  • 3
    Ah ha! This fixed it for me, I was just using an empty constructor before, which kept failing: new WebService.WebServiceSoapClient(); //fail – travis Dec 07 '10 at 17:21
  • This solutin did work !!! but, i am really curious as to why the default end point was not loaded ? any ideas to what could be the reasons ? – Dipti Mehta May 24 '11 at 05:35
  • @Andomar sorry to bring up an old thread. Does one have any advantage over another - WebReference and ServiceReference? I think the former would be more convenient for me but ServiceReference is the cool new thing I guess... – Kev Jan 11 '13 at 17:36
18

"This error can arise if you are calling the service in a class library and calling the class library from another project."

"In this case you will need to include the WS configuration settings into the main projects app.config if its a winapp or web.config if its a web app. This is the way to go even with PRISM and WPF/Silverlight."

Yes, but if you can't change main project (Orchard CMS for example), you can keep WCF service config in your project.

You need to create a service helper with client generation method:

public static class ServiceClientHelper
{
    public static T GetClient<T>(string moduleName) where T : IClientChannel
    {
        var channelType = typeof(T);
        var contractType = channelType.GetInterfaces().First(i => i.Namespace == channelType.Namespace);
        var contractAttribute = contractType.GetCustomAttributes(typeof(ServiceContractAttribute), false).First() as ServiceContractAttribute;

        if (contractAttribute == null)
            throw new Exception("contractAttribute not configured");

        //path to your lib app.config (mark as "Copy Always" in properties)
        var configPath = HostingEnvironment.MapPath(String.Format("~/Modules/{0}/bin/{0}.dll.config", moduleName)); 

        var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = configPath }, ConfigurationUserLevel.None);
        var serviceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);

        if (serviceModelSectionGroup == null)
            throw new Exception("serviceModelSectionGroup not configured");

        var endpoint = serviceModelSectionGroup.Client.Endpoints.OfType<ChannelEndpointElement>().First(e => e.Contract == contractAttribute.ConfigurationName);
        var channelFactory = new ConfigurationChannelFactory<T>(endpoint.Name, configuration, null);
        var client = channelFactory.CreateChannel();
        return client;
    }
}

and use it:

using (var client = ServiceClientHelper.GetClient<IDefaultNameServiceChannel>(yourLibName)) {
                ... get data from service ...
            }

See details in this article.

melvas
  • 2,346
  • 25
  • 29
18

I had a situation like this, where i had

  • WCF Service Hosted somewhere
  • Main Project
  • Consumer Project of type 'class Library' which has Service reference to a WCF Service
  • Main project calls methods from consumer project

Now the Consumer project had all the related configuration setting in <system.serviceModel> Tag of my app.config, its was still throwing the same error as the above.

All i did is added the same tag <system.serviceModel> to my main project's app.config file, and finally we were good to go.

The Real problem, as far as in my case was, it was reading the wrong configuration file. Instead of consumer's app.config, it was referring main proj's config. it took me two hours to figure that out.

Bravo
  • 3,341
  • 4
  • 28
  • 43
  • 1
    Same. Search for `` in your library, then copy it to your main application's app.config. Just another symptom of class library app.configs not being read at run time. I spend sooooo much time compensating for this oversight (imo). If I want the library to read it's config from it's app.config, let it. Otherwise, why have an app.config for class libraries in the first place?? – SteveCinq Aug 22 '17 at 22:02
  • I do have a similar situation, and both my Consumer Project's App config's service model section and the main project's app config's service model are identical, but still i am getting this error. May be there's something else which is causing this at my end. – ManojRawat May 25 '23 at 12:22
16

This one drove me crazy.

I'm using Silverlight 3 Prism (CAB) with WCF

When I call a WCF service in a Prism module, I get the same error:

Could not find default endpoint element that references contract 'IMyService' in the service model client configuaration section. This might be because no configuaration file was found for your application or because no end point element matching this contract could be found in the client element

It turns out that its looking in the Shell's .xap file for a ServiceReferences.ClientConfig file, not in the module's ServiceReferences.ClientConfig file. I added my endpoint and binding to the existing ServiceReferences.ClientConfig file in my Silverlight Shell application (it calls it's own WCF services).

Then I had to rebuild the Shell app to generate the new .xap file for my Web project's ClientBin folder.

Now this line of code finally works:

MyServiceClient myService = new MyServiceClient();
Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
Jeff Moeller
  • 186
  • 2
  • 4
16

Several responses here hit upon the correct solution when you're facing the mind-numbingly obscure error of referencing the service from a class file: copy service config info into your app.config web.config of your console or windows app. None of those answers seem to show you what to copy though. Let's try and correct that.

Here's what I copied out of my class library's config file, into my console app's config file, in order to get around this crazy error for a service I write called "TranslationServiceOutbound".

You basically want everything inside the system.serviceModel section:

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_ITranslationServiceOutbound" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://MyHostName/TranslationServiceOutbound/TranslationServiceOutbound.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITranslationServiceOutbound"
    contract="TranslationService.ITranslationServiceOutbound" name="BasicHttpBinding_ITranslationServiceOutbound" />
</client>

markaaronky
  • 1,231
  • 12
  • 29
12

I was getting this error within an ASP.NET application where the WCF service had been added to a class library which is being added to the ASP.NET application as a referenced .dll file in the bin folder. To resolve the error, the config settings in the app.config file within the class library referencing the WCF service needed to be copied into the web.config settings for the ASP.NET site/app.

zanderwel
  • 155
  • 3
  • 7
  • While other answers might've described the same problem. This answer described my exact situation and I finally understood the issue. Thank you for saving my day – Wouter Vanherck Jun 27 '18 at 09:22
10

I found (as well as copying to the client UI's App.config as I was using a Class Library interface) I had to prefix the name of the binding with the name of the Service Reference (mine is ServiceReference in the below).

e.g.:

<endpoint address="http://localhost:4000/ServiceName" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_ISchedulerService"
      contract="ServiceReference.ISchedulerService" 
      name="BasicHttpBinding_ISchedulerService" />

instead of the default generated:

<endpoint address="http://localhost:4000/ServiceName" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_ISchedulerService"
      contract="ISchedulerService" 
      name="BasicHttpBinding_ISchedulerService" />
Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
10

I had the same problem, but changing the contract namespace didn't work for me. So I tried a .Net 2 style web reference instead of a .Net 3.5 service reference. That worked.

To use a Web reference in Visual Studio 2008, click on 'Add Service Reference', then click 'Advanced' when the dialog box appears. In that you will find an option that will let you use a Web reference instead of a Service reference.

Cyril Gupta
  • 13,505
  • 11
  • 64
  • 87
  • 2
    This is what I ended up doing as well. I wish the problem made sense to me. – Jarrett Widman May 20 '09 at 16:07
  • This worked for me, also. Now I have all this extra junk in my solution (Settings.Settings, a new Web References folder) for no good reason. Will have to come back and re-visit this when I have more time. – Mike K Oct 20 '09 at 20:29
  • Agreed. I had the same problem, and fixed it by changing it to a web reference. – Stephen Hosking Apr 29 '10 at 04:51
10

Unit testing a non-library application that consumes a service can cause this problem.

The information that others have entered addresses the root cause of this. If you are trying to write automated test cases and the unit you are testing will actually invoke the service interface, you need to add the service reference to the test project. This is a flavor of the application using library type of error. I did not immediately realize this though because my code that consumes the interface is not in a library. However, when the test actually runs it will be running from the test assembly, not the assembly under test.

Adding a service reference to the unit test project resolved my issue.

PatrickV
  • 2,057
  • 23
  • 30
8

I have a situation which in the Unit test. I copied the app.config file to the unit test project. So the unit test project also contains endpoint information.

5

I faced this problem once. It was because i was still developing the interface that uses WCF service. I configured test application and continued development. Then in development, i changed some of the services' namespaces. So i double checked "system.serviceModel -> client -> endpoint -> contract" in web.config to match WCF class. Then problem is solved.

vardars
  • 541
  • 1
  • 8
  • 20
4

The namespace in your config should reflect the rest of the namespace path after your client's default namespace (as configured in the project properties). Based on your posted answer, my guess is that your client is configured to be in the "Fusion.DataExchange.Workflows" namespace. If you moved the client code to another namespace you would need to update the config to match the remaining namespace path.

Chris Porter
  • 3,627
  • 25
  • 28
3

In case if you are using WPF application using PRISM framework then configuration should exist in your start up project (i.e. in the project where your bootstrapper resides.)

VRK
  • 400
  • 2
  • 5
3

This error can arise if you are calling the service in a class library and calling the class library from another project.

Manuel Alves
  • 3,885
  • 2
  • 30
  • 24
3

I Have a same Problem.I'm Used the WCF Service in class library and calling the class library from windows Application project.but I'm Forget Change <system.serviceModel> In Config File of windows application Project same the <system.serviceModel> of Class Library's app.Config file.
solution: change Configuration of outer project same the class library's wcf configuration.

3

Do not put service client declaration line as class field, instead of this, create instance at each method that used in. So problem will be fixed. If you create service client instance as class field, then design time error occurs !

Dan McClain
  • 11,780
  • 9
  • 47
  • 67
Mucahid Uslu
  • 367
  • 3
  • 11
3

Hi I've encountered the same problem but the best solution is to let the .NET to configure your client side configuration. What I discover is this when I add a service reference with a query string of http:/namespace/service.svc?wsdl=wsdl0 it does NOT create a configuration endpoints at the client side. But when I remove the ?wsdl-wsdl0 and only use the url http:/namespace/service.svc, it create the endpoint configuration at the client configuration file. for short remoe the " ?WSDL=WSDL0" .

Joey
  • 31
  • 1
2

Allow me to add one more thing to look for. (Tom Haigh's answer already alludes to it, but I want to be explicit)

My web.config file had the following defined:

<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>

I was already using basicHttpsBinding for one reference, but then I added a new reference which required basicHttpBinding (no s). All I had to do was add that to my protocolMapping as follows:

<protocolMapping>
    <add binding="basicHttpBinding" scheme="http" />
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>

As L.R. correctly points out, this needs to be defined in the right places. For me, that meant one in my Unit Test project's app.config as well as one in the main service project's web.config.

Community
  • 1
  • 1
David
  • 4,665
  • 4
  • 34
  • 60
2

I had this error when I was referencing the Contract in the configuration file element without the global scope operator.

i.e.

<endpoint contract="global::MyNamepsace.IMyContract" .../>

works, but

<endpoint contract="MyNamepsace.IMyContract" .../>

gives the "Could not find default endpoint element that references contract" error.

The assembly containing MyNamepsace.IMyContract is in a different assembly to the main application, so this may explain the need to use the global scope resolution.

saille
  • 9,014
  • 5
  • 45
  • 57
2

There seem to be several ways to create/fix this issue. For me, the CRM product I am using was written in native code and is able to call my .NET dll, but I run into the configuration information needing to be at/above the main application. For me, the CRM application isn't .NET, so I ended up having to put it in my machine.config file (not where I want it). In addition, since my company uses Websense I had a hard time even adding the Service Reference due to a 407 Proxy Authentication Required issue, that to required a modification to the machine.cong.

Proxy solution:

To get the WCF Service Reference to work I had to copy the information from the app.config of my DLL to the main application config (but for me that was machine.config). And I also had to copy the endpoint information to that same file. Once I did that it starting working for me.

TPaul
  • 219
  • 2
  • 7
2

Ok. My case was a little diffrent but finally i have found the fix for it: I have a Console.EXE -> DLL -> Invoking WS1 -> DLL -> Invoking WS2

I have had both the configurations of the service model of WS1, and WS2 in the Console.EXE.config as recommended. - didnt solve the issue.

But it still didn't work, until i have added the WebReference of WS2 to WS1 also and not only to the DLL that actually creating and invoking the proxy of WS2.

Itay Levin
  • 1,579
  • 2
  • 16
  • 23
2

When you are adding a service reference

enter image description here

beware of namespace you are typing in:

enter image description here

You should append it to the name of your interface:

<client>
  <endpoint address="http://192.168.100.87:7001/soap/IMySOAPWebService"
            binding="basicHttpBinding" 
            contract="MyNamespace.IMySOAPWebService" />
</client>
Waldemar Gałęzinowski
  • 1,125
  • 1
  • 10
  • 18
2

I got same error and I have tried many things but didn't work, than I noticed that my "contract" was not same at all projects, I changed the contract as would be same for all projects inside solution and than it worked. This is project A

<client>
    <endpoint address="https://xxxxxxxx" binding="basicHttpBinding" bindingConfiguration="basic" contract="ServiceReference.IIntegrationService" name="basic" />
</client>

Project B :

<client>
    <endpoint address="xxxxxxxxxxxxx" binding="basicHttpBinding" bindingConfiguration="basic" contract="ServiceReference1.IIntegrationService" name="basic" />
</client>

Finally I changed for both as :

<client>
    <endpoint address="https://xxxxxxxxxxx" binding="basicHttpBinding" bindingConfiguration="basic" contract="MyServiceReferrence.IIntegrationService" name="basic" />
</client>
nzrytmn
  • 6,193
  • 1
  • 41
  • 38
2

If you reference the web service in your class library then you have to copy app.config to your windows application or console application

solution: change Configuration of outer project same the class library's wcf configuration.

Worked for me

user229044
  • 232,980
  • 40
  • 330
  • 338
Roshan
  • 29
  • 1
2

I had the same Issue
I was using desktop app and using Global Weather Web service

I deleted the service reference and added the web reference and problem solved Thanks

Kamran Akhter
  • 513
  • 2
  • 9
  • 21
2

Solution for me was to remove the endpoint name from the Endpoint Name attribute in client web.config this allowed the proxy to use

ChannelFactory<TService> _channelFactory = new ChannelFactory<TService>("");

only took all day to work out. Also the contract name was wrong once this fix was in place although it had been wrong when the initial error appear. Double then triple check for contract name strings people !! attrib: Ian

rob
  • 8,134
  • 8
  • 58
  • 68
1

I had the same issue and it was solved only when the host application and the dll that used that endpoint had the same service reference name.

silver
  • 1,633
  • 1
  • 20
  • 32
1

In my case, I Add service in both start up project and projects that use service with the same name. It worked...

0

In my case I had renamed app.config to [appname].exe.config manually. This ended up adding an extra .config suffix to the file name. Solution was to rename it to [appname].exe to eliminate the extra .config extension.

AMA
  • 139
  • 1
  • 12
0

There was another way I found the right endpointConfigurationName.

Adding the endpointConfigurationName as below

EservicesNew.ServiceClient eservicenew = new EservicesNew.ServiceClient("BasicHttpsBinding_IService");

Find the endpointConfigurationName as below

After adding the Web Reference, open the configuration.svcinfo file from the generated reference file.

enter image description here

There is two endpoints found and I used the right endpoint bindingConfiguration value which is BasicHttpsBinding_IService

enter image description here

Naz141
  • 433
  • 1
  • 8
  • 31
0

In my case, exe.config file was missing in production environment.

İlker Elçora
  • 610
  • 5
  • 13
-1

In my case, I was referring to this service from a library project, not a startup Project. Once I copied <system.serviceModel> section to the configuration of the main startup project, The issue got resolved.

During running stage of any application, the configuration will be read from the startup/parent project instead of reading its own configurations mentioned in separate subprojects.

Nikhil Dinesh
  • 3,359
  • 2
  • 38
  • 41