简体   繁体   English

将wcf服务中的端点设置为特定于客户端。 ASP.NET MVC

[英]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. 我正在创建一个ASP.net Web应用程序,该应用程序调用Web服务。 The endpoint configuration of this web service (WCF) is dependent on the client. 此Web服务(WCF)的端点配置取决于客户端。 Meaning that this endpoint address could be different for two different users on different instances of the web app. 这意味着该终结点地址对于Web应用程序不同实例上的两个不同用户可能是不同的。 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. 当前,我在Web.config中设置了地址,但是,这意味着当一个用户设置端点时,也会为所有其他用户设置该地址。

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? 有没有一种方法可以在客户端设置端点,以使其不影响Web应用程序的其他实例?

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. 如您所说,由于配置文件已对服务端点地址进行了硬编码,因此我们可以使用Channel Factory调用服务,这是在调用服务时需要在运行时指定服务端点地址和绑定信息的方式。
Assumed that there is a below server-side service. 假设下面有一个服务器端服务。
Server-side(10.157.13.69). 服务器端(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). 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>

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/zh-cn/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://docs.microsoft.com/zh-cn/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
https://www.c-sharpcorner.com/UploadFile/ff2f08/channel-factory-in-wcf/ 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. 请随时告诉我是否有什么我可以帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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