title: How to pass endpoint details dynamically from configuration?
body: <p>I followed <a href="https://blogs.msdn.microsoft.com/webdev/2016/06/26/wcf-connected-service-for-net-core-1-0-0-and-asp-net-core-1-0-0-is-now-available/" rel="nofollow noreferrer">article</a> to include WCF service into my ASP.NET core application.</p>
  
  <p>Looking at below line in reference.cs, it seems default endpoint configuration is hard-coded inside reference.cs.  </p>
  
  <pre><code>return new System.ServiceModel.EndpointAddress("http://localhost:49945/SimpleService.svc");
  </code></pre>
  
  <p>This is how I can create my client in asp.net core controller-</p>
  
  <pre><code>BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
              EndpointAddress endpointAddress = new EndpointAddress("http://localhost:49945/SimpleService.svc");
              wcfClient = new SimpleServiceClient(basicHttpBinding, endpointAddress);
  </code></pre>
  
  <p>So my questions are-</p>
  
  <ol>
  <li><p>Where should I maintain endpoint details so that it can be easily configurable during deployment?</p></li>
  <li><p><strong>How I can pass endpoint details (address and binding) dynamically</strong> from configuration file (appsetting.json)?</p></li>
  </ol>
  
  <p>Generated Reference.cs file looks like follows-</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated by a tool.
  //     //
  //     Changes to this file may cause incorrect behavior and will be lost if
  //     the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace SimpleServiceReference
  {
      using System.Runtime.Serialization;
  
  
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.5.0.0")]
      [System.Runtime.Serialization.DataContractAttribute(Name="CompositeType", Namespace="http://schemas.datacontract.org/2004/07/SimpleService")]
      public partial class CompositeType : object
      {
  
          private bool BoolValueField;
  
          private string StringValueField;
  
          [System.Runtime.Serialization.DataMemberAttribute()]
          public bool BoolValue
          {
              get
              {
                  return this.BoolValueField;
              }
              set
              {
                  this.BoolValueField = value;
              }
          }
  
          [System.Runtime.Serialization.DataMemberAttribute()]
          public string StringValue
          {
              get
              {
                  return this.StringValueField;
              }
              set
              {
                  this.StringValueField = value;
              }
          }
      }
  
      [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.5.0.0")]
      [System.ServiceModel.ServiceContractAttribute(ConfigurationName="SimpleServiceReference.ISimpleService")]
      public interface ISimpleService
      {
  
          [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ISimpleService/GetData", ReplyAction="http://tempuri.org/ISimpleService/GetDataResponse")]
          System.Threading.Tasks.Task&lt;string&gt; GetDataAsync(int value);
  
          [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ISimpleService/GetDataUsingDataContract", ReplyAction="http://tempuri.org/ISimpleService/GetDataUsingDataContractResponse")]
          System.Threading.Tasks.Task&lt;SimpleServiceReference.CompositeType&gt; GetDataUsingDataContractAsync(SimpleServiceReference.CompositeType composite);
      }
  
      [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.5.0.0")]
      public interface ISimpleServiceChannel : SimpleServiceReference.ISimpleService, System.ServiceModel.IClientChannel
      {
      }
  
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.5.0.0")]
      public partial class SimpleServiceClient : System.ServiceModel.ClientBase&lt;SimpleServiceReference.ISimpleService&gt;, SimpleServiceReference.ISimpleService
      {
  
      /// &lt;summary&gt;
      /// Implement this partial method to configure the service endpoint.
      /// &lt;/summary&gt;
      /// &lt;param name="serviceEndpoint"&gt;The endpoint to configure&lt;/param&gt;
      /// &lt;param name="clientCredentials"&gt;The client credentials&lt;/param&gt;
      static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
  
          public SimpleServiceClient() : 
                  base(SimpleServiceClient.GetDefaultBinding(), SimpleServiceClient.GetDefaultEndpointAddress())
          {
              this.Endpoint.Name = EndpointConfiguration.BasicHttpBinding_ISimpleService.ToString();
              ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
          }
  
          public SimpleServiceClient(EndpointConfiguration endpointConfiguration) : 
                  base(SimpleServiceClient.GetBindingForEndpoint(endpointConfiguration), SimpleServiceClient.GetEndpointAddress(endpointConfiguration))
          {
              this.Endpoint.Name = endpointConfiguration.ToString();
              ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
          }
  
          public SimpleServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) : 
                  base(SimpleServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
          {
              this.Endpoint.Name = endpointConfiguration.ToString();
              ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
          }
  
          public SimpleServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) : 
                  base(SimpleServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
          {
              this.Endpoint.Name = endpointConfiguration.ToString();
              ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
          }
  
          public SimpleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                  base(binding, remoteAddress)
          {
          }
  
          public System.Threading.Tasks.Task&lt;string&gt; GetDataAsync(int value)
          {
              return base.Channel.GetDataAsync(value);
          }
  
          public System.Threading.Tasks.Task&lt;SimpleServiceReference.CompositeType&gt; GetDataUsingDataContractAsync(SimpleServiceReference.CompositeType composite)
          {
              return base.Channel.GetDataUsingDataContractAsync(composite);
          }
  
          public virtual System.Threading.Tasks.Task OpenAsync()
          {
              return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action&lt;System.IAsyncResult&gt;(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
          }
  
          public virtual System.Threading.Tasks.Task CloseAsync()
          {
              return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action&lt;System.IAsyncResult&gt;(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
          }
  
          private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
          {
              if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ISimpleService))
              {
                  System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
                  result.MaxBufferSize = int.MaxValue;
                  result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                  result.MaxReceivedMessageSize = int.MaxValue;
                  result.AllowCookies = true;
                  return result;
              }
              throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
          }
  
          private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
          {
              if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ISimpleService))
              {
                  return new System.ServiceModel.EndpointAddress("http://localhost:49945/SimpleService.svc");
              }
              throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
          }
  
          private static System.ServiceModel.Channels.Binding GetDefaultBinding()
          {
              return SimpleServiceClient.GetBindingForEndpoint(EndpointConfiguration.BasicHttpBinding_ISimpleService);
          }
  
          private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
          {
              return SimpleServiceClient.GetEndpointAddress(EndpointConfiguration.BasicHttpBinding_ISimpleService);
          }
  
          public enum EndpointConfiguration
          {
  
              BasicHttpBinding_ISimpleService,
          }
      }
  }
  </code></pre>