【问题标题】:load WCF service by environment in .net core project在 .net core 项目中按环境加载 WCF 服务
【发布时间】:2019-01-17 21:28:12
【问题描述】:

在 .NET 核心项目中添加 WCF 时遇到问题。 当我过去使用 .net 时,我可以在 web.config 中添加多个环境,这样我就可以在运行时加载正确的 Web 服务(Dev、Rec、Prod)。

当我将 WCF 服务的引用添加为连接服务时,.net 核心项目中的问题它创建了一个文件 ConnectedService.json,其中包含 WCF 服务的 URL。

{
  "ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf",
  "Version": "15.0.20406.879",
  "GettingStartedDocument": {
    "Uri": "https://go.microsoft.com/fwlink/?linkid=858517"
  },
  "ExtendedData": {
    "Uri": "*****?singleWsdl",
    "Namespace": "Transverse.TokenService",
    "SelectedAccessLevelForGeneratedClass": "Public",
    "GenerateMessageContract": false,
    "ReuseTypesinReferencedAssemblies": true,
    "ReuseTypesinAllReferencedAssemblies": true,
    "CollectionTypeReference": {
      "Item1": "System.Collections.Generic.List`1",
      "Item2": "System.Collections.dll"
    },
    "DictionaryCollectionTypeReference": {
      "Item1": "System.Collections.Generic.Dictionary`2",
      "Item2": "System.Collections.dll"
    },
    "CheckedReferencedAssemblies": [],
    "InstanceId": null,
    "Name": "Transverse.TokenService",
    "Metadata": {}
  }
}

我的问题是如何根据使用的环境加载正确的服务。

注意。

在我的项目中,我没有 appsettings 也没有 web 配置。它是一个 .net core 类库,在 ASP.NET core Application 中作为中间件调用。

【问题讨论】:

    标签: c# .net wcf asp.net-core


    【解决方案1】:

    据我从this article 了解到,这是微软的建议:

    1. 添加新的类文件
    2. 添加与服务reference.cs相同的命名空间
    3. 添加部分类以扩展引用服务类(在 Reference.cs 中声明)
    4. 以及实现 ConfigureEndpoint() 的部分方法(在 Reference.cs 中声明)
    5. 通过为 Endpoint 设置新值来实现 ConfigureEndpoint() 方法

    例子:

    namespace Your_Reference_Service_Namespace
    {
        public partial class Your_Reference_Service_Client
        {
            static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials)
            {
                serviceEndpoint.Address = 
                    new System.ServiceModel.EndpointAddress(new System.Uri("http://your_web_service_address"), 
                    new System.ServiceModel.DnsEndpointIdentity(""));
            }
        }
    }
    
    1. 在这里,您可以从appsettings.json 文件中获取值

      new System.Uri(configuration.GetValue("yourServiceAddress")

    【讨论】:

      【解决方案2】:

      对解决方案感兴趣的人 我在每个 appseetings.{environment}.json 中为我的服务添加了一个端点,在服务类中我根据环境变量 ASPNETCORE_ENVIRONMENT 注入我的服务的新实例

         services.AddTransient<Transverse.TokenService.ITokenService>(provider =>
              {
                  var client = new Transverse.TokenService.TokenServiceClient();
                  client.Endpoint.Address = new System.ServiceModel.EndpointAddress(Configuration["Services:TokenService"]);
                  return client;
              });
      

      也许不是最好的,但效果很好。

      【讨论】:

      • 您好,能否请您分享一下您是如何调用 WS 的。我添加了您提到的代码。我验证了新地址 url 来自 appsettings。但是当在我的代码中我想通过第一个 DemoServiceClient client = new DemoServiceClient(); 执行 ws 方法时我检查了变量客户端的地址,它仍在使用原始地址 url,而不是来自 appsettings 的地址。谢谢!
      【解决方案3】:

      我正在使用 .Net Core 3.1,这是我调用 WCF 服务时的方式。

      var sUserClientRemoteAddress = _configuration.GetValue<string>("WCFRemoteAddress:UserClient");
      UserClient userService = new UserClient(UserClient.EndpointConfiguration.CustomBinding_IUser, sUserClientRemoteAddress);
      

      首先,从 appsettings.json 中获取端点远程地址

      其次,在 CTOR WCF 客户端类参数中使用该地址调用 Web 服务客户端

      提前致谢。

      【讨论】:

        【解决方案4】:

        使用ChannelFactory 来使用您的服务。 WCF ChannelFactory vs generating proxy

        ChannelFactory 允许您设置EndpointAddressHow to: Use the ChannelFactory

        端点的 URL 可以从配置文件中加载。在更高级的设置中,可以执行服务的目录查找以检索部署应用程序的环境的 URL。 https://en.wikipedia.org/wiki/Service_provider_interface

        https://github.com/jolmari/netcore-wcf-service-proxy

        使用代理使用多个 WCF 服务的示例 在 ASP.NET Core Web 应用程序中实现。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-19
          • 2020-02-07
          • 2019-04-02
          • 1970-01-01
          • 2021-11-07
          • 2021-09-28
          • 2018-11-17
          • 2018-05-24
          相关资源
          最近更新 更多