Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create WCF endpoint configurations in the client app, in code?

I am trying to consume a WCF web service from a .NET client application, and I think I need to be able to programmatically create endpoints, but I don't know how. I think I need to do this because, when I try to run the application, I am getting the following error:

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

While troubleshooting this error, I created a simple windows forms application, in which I try to consume the same web service. With this test application I can connect to the web service successfully, and I get a valid response. But, I can reproduce the exact error cited above within in my test app by removing the system.serviceModel node and all of its child nodes from the application's app.config file (I might not have to remove ALL of that section, I'm not sure). So, my first thought was that I need to add that section to the app.config file for the real app, and everything should be fine. Unfortunately, for ridiculous reasons that I won't get into here, that is not an option. So, I am left with having to generate this information in code, inside the client app.

I am hoping someone here can help me work through this, or can point me toward a good resource for this sort of problem.

Is it possible to create endpoint configurations in the client app, in code?

like image 210
campbelt Avatar asked Feb 23 '11 23:02

campbelt


1 Answers

By default, when you do an Add Service Reference operation, the WCF runtime will generate the client-side proxy for you.

The simplest way to use it is to instantiate the client proxy with a constructor that takes no parameters, and just grab the info from the app.config:

YourServiceClient proxy = new YourServiceClient();

This requires the config file to have a <client> entry with your service contract - if not, you'll get the error you have.

But the client side proxy class generated by the WCF runtime also has additional constructors - one takes an endpoint address and a binding, for instance:

BasicHttpBinding binding = new BasicHttpBinding(SecurityMode.None);
EndpointAddress epa = new EndpointAddress("http://localhost:8282/basic");

YourServiceClient proxy = new YourServiceClient(binding, epa);

With this setup, no config file at all is needed - you're defining everything in code. Of course, you can also set just about any other properties of your binding and/or endpoint here in code.

like image 175
marc_s Avatar answered Sep 21 '22 23:09

marc_s