如何在.NET Core中转换SOAP EndpointAddress?

时间:2019-03-29 09:39:30

标签: asp.net-core soap

在.NET Core中连接SOAP服务时,连接的服务将在解决方案资源管理器中按预期显示

enter image description here

ConnectedService.json确实包含了假定的定义。即

{
  "ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf",
  ...
  "ExtendedData": {
    "Uri": "https://test.example.net/Service.svc",
    "Namespace": "UserWebService",
    "SelectedAccessLevelForGeneratedClass": "Public",
...
}

Uri中的ExtendedData最终出现在Reference.cs文件中

private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
{
    if ((endpointConfiguration == EndpointConfiguration.WSHttpBinding_IAnvandareService))
    {
        return new System.ServiceModel.EndpointAddress("https://test.example.net/Service.svc");
    }
    throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}

如果部署过程看起来像TEST > STAGING > PRODUCTION,则可能希望具有相应的端点。即https://production.example.net/Service.svc

我们使用Azure Devops进行构建,并使用Azure Devops / Octopus Deploy进行部署

2 个答案:

答案 0 :(得分:1)

解决方案(我认为)是在注册依赖项时更改端点地址,即

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

services.AddTransient<IAnvandareService, AnvandareServiceClient>((ctx) => new AnvandareServiceClient()
{
    Endpoint =
    {
        Address = new EndpointAddress($"https://{environment}.example.net/Service.svc")
    }
});

答案 1 :(得分:0)

这只是Eric Herlitz提供的答案的扩展。主要是为了说明如何使用您的appsettings.json文件保存端点url的值。

您需要将不同的端点添加到您的appsettings。{enviroment} .json文件中。

{
  ...
  "ServiceEndpoint": "http://someservice/service1.asmx",
   ...
}

然后,当您发布到不同的环境时,您需要确保环境变量已更新。 How to transform appsettings.json

在启动类中,找到方法ConfigureServices()并注册服务以进行依赖项注入

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<ADSoap, ADSoapClient>(fac =>
        {
            var endpoint = Configuration.GetValue<string>("ServiceEndpoint");
            return new ADSoapClient(ADSoapClient.EndpointConfiguration.ADSoap12)
            {
                Endpoint =
                {
                    Address = new EndpointAddress(new Uri(endpoint))
                }
            };
        });
    }

然后在某些类中使用该服务,您可以将该服务注入到构造函数中:

public class ADProvider : BaseProvider, IADProvider
{
    public ADSoap ADService { get; set; }

    public ADProvider(IAPQ2WebApiHttpClient httpClient, IMemoryCache cache, ADSoap adClient) : base(httpClient, cache)
    {
        ADService = adClient;
    }
}