How do I add WCF client endpoints programmatically?

后端 未结 1 1649
南方客
南方客 2020-12-20 18:03

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 (

相关标签:
1条回答
  • 2020-12-20 18:36

    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 讨论(0)
提交回复
热议问题