Search code examples
c#wcf.net-core

Multiple environments for .NET Core WCF service reference


I am trying to add a WCF service reference to my .NET Core app with the ability to switch which environment (service endpoint) it's pointed at.

Using .NET Framework you are able to specify multiple client endpoints within the web.config and then pass the specified name into the service client.

Is there a similar way to achieve this in .NET Core?


Solution

  • Yes, you can do that. In fact it is the only use of a partial method I have ever come across. Implement a partial method ConfigureEndpoint on a partial class of your client and set the URI from any config source:

    public partial class YourServiceClient
    {
        static partial void ConfigureEndpoint(ServiceEndpoint serviceEndpt, ClientCredentials creds)
        {
            string uri = //get this from any .NET Core configuration provider...
    
            serviceEndpoint.Address = new EndpointAddress(new Uri(uri));
        {
    }