0

I were studying WCF here http://msdn.microsoft.com/ru-ru/library/bb386386.aspx and I successfully did Testing the Service step. However in Accessing the Service step I faced problems. It builds without any error, but when I tried to write smth to textLabel space and pressed button1 I get the error in button1_Click function, namely ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

Error message

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

I find such code in the app.project file

<endpoint address="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService11"
            contract="ServiceReference1.IService1" name="BasicHttpBinding_IService11" />

I`m 100% sure, that code is without any error, because I copied it from the above site without any modification. So I will be glad to hear your assumptions how fix that.

Rocketq
  • 5,423
  • 23
  • 75
  • 126
  • "This error can arise if you are calling the service in a class library and calling the class library from another project." And here I have found almost the same problem http://stackoverflow.com/questions/352654/could-not-find-default-endpoint-element. – Rocketq Apr 07 '13 at 13:03

1 Answers1

2

You should specify the name of the endpoint when constructing the client:

using (var client = new ServiceReference1.Service1Client("BasicHttpBinding_IService11"))
{
    client.SomeMethod();
}

or use * if you have only one endpoint in the config file:

using (var client = new ServiceReference1.Service1Client("*"))
{
    client.SomeMethod();
}

The reason you need to specify the name is because you could have multiple endpoints (with different bindings for example) for the same service in the config file and if you do not specify the name the framework wouldn't know which endpoint you want to invoke.

Also notice how I have wrapped the IDisposable client in a using statement to ensure proper disposal once you have finished working with it.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks to your attention, but I didnt understand where I should write that specification about endpoint. Anyway I could try to do that tasks again. – Rocketq Apr 10 '13 at 20:55
  • 1
    You should do that at the place where you are instantiating the proxy (in `button1_Click`). – Darin Dimitrov Apr 11 '13 at 05:45
  • Using the `using` statement with a WCF client is bad practice as it won't properly dispose the client. See [Avoiding Problems with the Using Statement](http://msdn.microsoft.com/en-us/library/aa355056.aspx). – Tim Oct 06 '13 at 20:29