首页 > 解决方案 > 将 wcf 服务中的端点设置为特定于客户端。ASP.NET MVC

问题描述

我正在创建一个调用 Web 服务的 ASP.net Web 应用程序。此 Web 服务 (WCF) 的端点配置取决于客户端。这意味着对于 Web 应用程序的不同实例上的两个不同用户,此端点地址可能不同。目前我在 Web.config 中设置了地址,但是,这意味着当一个用户设置端点时,它也会为所有其他用户设置。

有没有办法在客户端设置端点,这样它就不会影响 Web 应用程序的其他实例?

我目前使用以下方法更改端点:

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

标签: asp.netasp.net-mvcweb-serviceswcfweb-applications

解决方案


正如你所说,由于配置文件已经硬编码了服务端点地址,我们可以使用通道工厂来调用服务,这种方式在调用服务时需要在运行时指定服务端点地址和绑定信息。
假设有下面的服务器端服务。
服务器端(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(服务器端)。

  <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>

客户端调用。

    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();
}

在客户端调用过程中,我们在运行时手动指定服务端点信息,然后调用服务。我认为这可能是您要求的解决方案。
请参考以下文件。
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/
如果有什么可以告诉我的帮助。


推荐阅读