2

When connecting a SOAP service in .NET Core the Connected Service is shown as expected in the solution explorer

enter image description here

The ConnectedService.json does contain the definitions as supposed. I.e.

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

The Uri from ExtendedData ends up in the Reference.cs file

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

If a deployment process looks like TEST > STAGING > PRODUCTION one might like to have corresponding endpoints. I.e. https://production.example.net/Service.svc.

We use Azure Devops for build and Azure Devops/Octopus Deploy for deployments

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157

2 Answers2

2

The solution (as I figured) was to change the endpoint address when you register the dependency i.e.

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

services.AddTransient<IAnvandareService, AnvandareServiceClient>((ctx) => new AnvandareServiceClient()
{
    Endpoint =
    {
        Address = new EndpointAddress($"https://{environment}.example.net/Service.svc")
    }
});
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
2

This is just an expansion of the answer provided by Eric Herlitz. Primarily meant to show how to use your appsettings.json file to hold the value for the endpoint url.

You will need to add the different endpoints to your appsettings.{enviroment}.json files.

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

Then you will need to make sure your environment variable is updated when you publish to different environments. How to transform appsettings.json

In your startup class find the method ConfigureServices() and register your service for dependency injection

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

Then to consume the service in some class you can inject the service into the constructor:

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

    public ADProvider(IAPQ2WebApiHttpClient httpClient, IMemoryCache cache, ADSoap adClient) : base(httpClient, cache)
    {
        ADService = adClient;
    }
}
LazyDog
  • 317
  • 4
  • 5