开发者

How do I add WCF client endpoints programmatically?

开发者 https://www.devze.com 2023-03-08 08:22 出处:网络
I need my service to consume other services, and I need to configure these dependencies in code. How do I do this?

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

精彩评论

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