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?

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.


An east way to consume a WCF service if you have a reference to the assembly which defines the interface, is using the System.ServiceModel.ChannelFactory class.

For example, if you would like to use BasicHttpBinding:

var emailService = ChannelFactory<IEmailService>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(new Uri("http://some-uri-here.com/));

If you don't have a reference to the service assembly, then you can use one of the overloaded constructors on the generated proxy class to specify binding settings.


Need Your Help

What is __pycache__?

python python-3.x caching

From what I understand, a cache is an encrypted file of similar files.

How can I check/upgrade Proguard version whn using it in Eclipse for Android development?

android eclipse obfuscation proguard

The documentation on this is extremely poor. I understand that ProGuard can be enabled by manually editing "default.properties" in the project's rot directory. And all the settings go into the "pro...