简体   繁体   中英

Creating and Accessing WCF Services. Visual studio 2012 gives error

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM