Programatically create a ClientBase<T> instance. RRS feed

  • Question

  • I'm working on an API that allows us to programatically create our service client class instance from configuration info that is not stored within a conventional config file.

    We've had great success and had to pull some tricks but I'm stuck when it comes to "behaviors". As you'll know the ClientBase<T> class exposes a few base constructors but none of these take created Endpoint (only an endpoint name or a raw address).

    Thus the ClientBase<T> class is tightly coupled to the configuration file management system.

    We have good support for bindings (because so long as can create a full formed Binding object we can use the constructors that accept these) but with endpoints we can only pass a name or an address not a complete Endpoint (is there such a class? there is EndpointTrait but it's inaccessible).

    I've been scrutinizing the MS reference source for ClientBase<T> and it looks pretty tough to somehow create an instance without being tied to a config file.

    Much of our code that creates service (clients I'm speaking of) is working and we simply replace:

    var service = new MyServiceClient();
    // becomes (WcfManager is a static class of ours)
    var service = WcfManager.CreateService<MyServiceClient,MyServiceInterface>();

    we also support the constructor that accepts a single endpoint name but we (under the hood just use the Address). Ultimately we call the base constructor (Binding, EndpointAddress);

    This works fine for crude endpoint definitions.

    We have access to an XML blob that mirrors the configuration file endpoint structure but all we can ultimately do is create an EndpointAddress which seems to prevent us including behavior and other info.

    So that's our problem - programmatically create the service<T> instance without access to a config file...


    I can see that the base ClientBase<T> class does expose a constructor that accepts a single ServiceEndpoint that might be the way to go but sadly the created derived client class does not implement that constructor...


    Thursday, August 3, 2017 2:30 PM

All replies

  • Hi Korporal Kernel,

    Do you mean add Behaviors to your service endpoint, if so, please refer to the following code.

    WebHttpBinding restBinding = new WebHttpBinding();
    ServiceEndpoint restSEP = 
                                       restBinding, "rest");
    restSEP.Behaviors.Add(new WebHttpBehavior());

    For more information, please refer to:


    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 8, 2017 6:10 AM