开发者

How do I add WCF client endpoints programmatically?

I need my service to consume other services, and I need to configure these dependencies in code. How do I do this?

This is very simple in config via the following (example):

   <client>
  <endpoint name="registerService"
            address="http://127开发者_高级运维.0.0.1/registration/" binding="basicHttpBinding"    
            contract="*"/>
  </client>

But for some reason finding the code equivalent is not as easy as I thought it'd be.


If you're using the Visual Studio generated proxy (via "Add Service Reference..."), then you're using the ClientBase abstract class & you'll have a number of constructors that allow you to pass in a config section, an endpoint, a binding etc.

http://msdn.microsoft.com/en-us/library/ms576141.aspx

And if you're instantiating a ChannelFactory then you again have a number of constructors to use.

http://msdn.microsoft.com/en-us/library/ms576132.aspx

// create bindings & endpoints
var binding = new System.ServiceModel.BasicHttpBinding();
var endpoint = new EndpointAddress("http://localhost/MyService.svc");

var factory = new ChannelFactory<IMyService>(binding, endpoint);

var channel = factory.CreateChannel();
// then call your operations...
channel.MyOperation();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜