98

Any ideas how to fix this?

UserService.UserServiceClient userServiceClient = new UserServiceClient();
            userServiceClient.GetUsersCompleted += new EventHandler<GetUsersCompletedEventArgs>(userServiceClient_GetUsersCompleted);
            userServiceClient.GetUsersAsync(searchString);

.

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_UserService" 
                     maxBufferSize="2147483647" 
                     maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:52185/UserService.svc" 
                  binding="basicHttpBinding" 
                  bindingConfiguration="BasicHttpBinding_UserService" 
                  contract="UserService.UserService"
                  name="BasicHttpBinding_UserService" />
    </client>
    <behaviors>
        <serviceBehaviors>
            <behavior name="Shell.Silverlight.Web.Service3Behavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
        <service behaviorConfiguration="Shell.Silverlight.Web.Service3Behavior" 
                 name="Shell.Silverlight.Web.Service3">
            <endpoint address="" 
                      binding="basicHttpBinding" 
                      contract="Shell.Silverlight.Web.Service3" />
            <endpoint address="mex" 
                      binding="mexHttpBinding" 
                      contract="IMetadataExchange" />
        </service>
    </services>
</system.serviceModel>

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

Resolved!

I didn't mention that this was a Silverlight application. I had the wcf reference in a DLL which had it's own "ServiceReferences.ClientConfig" file. I moved the contents of the DLL's ServiceReferences.ClientConfig to the main silverlight project and it worked.

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
NotDan
  • 31,709
  • 36
  • 116
  • 156

13 Answers13

186

I had a run in with the same problem. My application was also a Silverlight application and the service was being called from a class library with a custom UserControl that was being used in it.

The solution is simple. Copy the endpoint definitions from the config file (e.g. ServiceReferences.ClientConfig) of the class library to the config file of the silverlight application. I know you'd expect it to work without having to do this, but apparently someone in Redmond had a vacation that day.

casperOne
  • 73,706
  • 19
  • 184
  • 253
sprite
  • 3,724
  • 3
  • 28
  • 30
  • 51
    This solution is valid for ASP.NET and MVC projects as well. If you add the service to a Class Library it wont work out the box, grab the system.serviceModel section from the app.config in your library project and put it in your web.config. – Adam Pope Aug 11 '11 at 14:27
  • 5
    It looks like this is true for any kind of project, I was using quartz.net as windows service and had a job on a different assembly that was referencing the web service. It worked for me also. Thx. – thiagoleite Sep 05 '12 at 18:55
  • 2
    Hi, it worked for a Windows Phone project as well. As @thiagoleite says it seems it's working for most of the project types. – Thanushka Dec 31 '12 at 08:46
  • THANK YOU -- This saved me so much time just now. I was pulling my hair out wondering what I was doing wrong, but this is really counter-intuitive. – SeanKilleen Aug 05 '13 at 02:56
  • 3
    +1 for the Redmond vacation joke... Anyway, is there a way to add it as an external file instead playing with the `web.config`? – Shimmy Weitzhandler Oct 29 '15 at 04:19
  • @Shimmy not that I know of, but maybe someone else can help you with that. And +1 back at you! – sprite Nov 15 '15 at 05:50
  • 1
    @sprite I ended up setting my required settings in code (I've overridden the client constructor), as Vishal's [answer](http://stackoverflow.com/a/16081825/75500). This way it gets shipped over across assemblies. Thanks! – Shimmy Weitzhandler Nov 15 '15 at 10:35
45

You can also set these values programatically in the class library, this will avoid unnecessary movement of the config files across the library. The example code for simple BasciHttpBinding is -

BasicHttpBinding basicHttpbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
basicHttpbinding.Name = "BasicHttpBinding_YourName";
basicHttpbinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
basicHttpbinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

EndpointAddress endpointAddress = new EndpointAddress("http://<Your machine>/Service1/Service1.svc");
Service1Client proxyClient = new Service1Client(basicHttpbinding,endpointAddress);
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Vishal
  • 606
  • 5
  • 5
  • @wooncherk, my class library has just a reference to a wsdl. Where should I put this code? I build it out into a C# dll and put the dll into my app files, where I reference it from IronPython. Thank you – Срба Nov 22 '15 at 12:45
12

Just in case anyone hits the same problem whilst using WPF (rather than WCF or Silverlight):

I had this error, when connecting to a Web Service. When my code was in the "main" WPF Application solution, no problem, it worked perfectly. But when I moved the code to the more sensible DAL-layer solution, it would throw the exception.

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

As has been stated by "Sprite" in this thread, you need to manually copy the tag.

For WPF apps, this means copying the tag from the app.config in my DAL solution to the app.config in the main WPF Application solution.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
  • this is great, but would be better as a short comment under my own answer, just like "Adam Pope" did. It would get read by a lot more people. – sprite Aug 14 '11 at 07:36
  • 2
    @sprite: I would've added a comment, but StackOverflow comments are always shown as just one long paragraph. For a 5-paragraph reply like mine, that would've resulted in one long, unreadable, unfriendly paragraph. Writing a seperate response allowed me to give a clearer answer, which is more likely to help other users (which, let's face it, is the point of websites like this) – Mike Gledhill Dec 06 '11 at 09:35
6

I ran into the same issue, for whatever reason Visual Studio did not update the web config when I first added the service. I found that updating the Service Reference also fixed this issue.

Steps:

  1. Navigate to the Service Reference Folder
  2. Expand it
  3. Right Click and Select update Service Reference
  4. Observe web Config be updated
Dale C
  • 567
  • 6
  • 9
  • Yes, this did the trick for me. I'm using a 3rd party wsdl service reference & was getting this error until I updated the Service reference – ejhost Feb 09 '15 at 17:34
  • For me too. It was obvious since it works fine and suddenly not. I may have made a change and didn't update the service reference. – ehh Jul 14 '16 at 08:42
4

Change the web.config of WCF service as "endpoint address="" binding="basicHttpBinding"..." (previously binding="wsHttpBinding")After build the app, in "ServiceReferences.ClientConfig" ""configuration> has the value. Then it will work fine.

3

Rename the output.config produced by svcutil.exe to app.config. it worked for me.

ssilas
  • 31
  • 2
2

Do you have an Interface that your "UserService" class implements.

Your endpoints should specify an interface for the contract attribute:

contract="UserService.IUserService"
Nick Josevski
  • 4,156
  • 3
  • 43
  • 63
1

Not sure if this is an issue. Endpoint and binding both have the same name

david valentine
  • 4,625
  • 2
  • 21
  • 15
1

Not sure if it's really a problem, but I see you have the same name for your binding configuration ().

I usually try to call my endpoints something like "UserServiceBasicHttp" or something similar (the "Binding" really doesn't have anything to do here), and I try to call my binding configurations something with "....Configuration", e.g. "UserServiceDefaultBinding", to avoid any potential name clashes.

Marc

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

This problem occures when you use your service via other application.If application has config file just add your service config information to this file. In my situation there wasn't any config file so I use this technique and it worked fine.Just store url address in application,read it and using BasicHttpBinding() method send it to service application as parameter.This is simple demonstration how I did it:

Configuration config = new Configuration(dataRowSet[0]["ServiceUrl"].ToString());

var remoteAddress = new System.ServiceModel.EndpointAddress(config.Url);


SimpleService.PayPointSoapClient client = 
    new SimpleService.PayPointSoapClient(new System.ServiceModel.BasicHttpBinding(), 
    remoteAddress);
SimpleService.AccountcredResponse response = client.AccountCred(request);
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
0

Had to add the service in the calling App.config file to have it work. Make sure that you but it after all . This seemed to work for me.

Jackstine
  • 486
  • 4
  • 12
0

For those who work with AX 2012 AIF services and try to call there C# or VB project inside AX (x++) and suffer from such errors of "could not find default endpoint"... or "no contract found" ... go back to your visual studio (c#) project and add these lines before defining your service client, then deploy the project and restart AX client and retry: Note, the example is for NetTcp adapter, you could easily use any other adapter instead according to your need.

 Uri Address = new Uri("net.tcp://your-server:Port>/DynamicsAx/Services/your-port-name");
 NetTcpBinding Binding = new NetTcpBinding();
 EndpointAddress EndPointAddr = new EndpointAddress(Address);
 SalesOrderServiceClient Client = new SalesOrderServiceClient(Binding, EndPointAddr);
Boody
  • 144
  • 7
-2

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.)

In short just remove it from the class library and put into a start up project.

VRK
  • 400
  • 2
  • 5