简体   繁体   中英

Setting the endpoint in wcf service to be client specific. ASP.NET MVC

I'm creating an ASP.net web app which makes calls to a web service. The endpoint configuration of this web service (WCF) is dependent on the client. Meaning that this endpoint address could be different for two different users on different instances of the web app. Currently I set the address in the Web.config, however, this means that when one user sets the endpoint, it is also set for all other users.

Is there a way to set the endpoint on the client side so that it does not effect the other instances of the web app?

I currently change the endpoint using:

BasicHttpsBinding binding = new BasicHttpsBinding("ServiceSoap");
EndpointAddress epa = new EndpointAddress(new Uri(newUrl));
service = new ServiceSoapClient(binding, epa);

As you said, since the configuration file has hard-coded the service endpoint address, we could call the service by using Channel Factory, the way that we need to specify the service endpoint address and binding information during the runtime when calling the service.
Assumed that there is a below server-side service.
Server-side(10.157.13.69).

    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost sh = new ServiceHost(typeof(MyService)))
            {
                sh.Open();
                Console.WriteLine("Service is ready....");

                Console.ReadLine();
                sh.Close();
            }

        }
    }

    [ServiceContract]
    interface IService
    {
        [OperationContract]
        string GetData();
    }
    public class MyService : IService
    {
        public string GetData()
        {
            return $"Hello, Now is {DateTime.Now.ToString()}";
        }
}

App.config(Server-side).

  <system.serviceModel>
    <services>
      <service name="Server1.MyService">
        <endpoint address="" binding="basicHttpBinding" contract="Server1.IService"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:5577"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Client Invocation.

    class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("http://10.157.13.69:5577");
            BasicHttpBinding binding = new BasicHttpBinding();
            ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));

            IService service = factory.CreateChannel();
            try
            {
                var result = service.GetData();
                Console.WriteLine(result);

            }
            catch (Exception)
            {
                throw;
            }
        }

    }
    [ServiceContract]
    interface IService
    {
        [OperationContract]
        string GetData();
}

During the process of the client call, we manually specify the service endpoint information during runtime then call the service. I think this might be a solution to your requirement.
Please refer to the below document.
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.channelfactory-1?view=netframework-4.8
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
https://www.c-sharpcorner.com/UploadFile/ff2f08/channel-factory-in-wcf/
Feel free to let me know if there is anything I can help with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM