【问题标题】:Setting the endpoint in wcf service to be client specific. ASP.NET MVC将 wcf 服务中的端点设置为特定于客户端。 ASP.NET MVC
【发布时间】:2019-12-01 17:44:13
【问题描述】:

我正在创建一个调用 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.net asp.net-mvc web-services wcf web-applications


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多