Load WSDL by environment in .NET Web API project

Salman Rafique 0 Reputation points
2023-04-07T11:04:34.3566667+00:00

I have different WSDL URLs for development and production. Whenever I add WSDL web reference in my ASP.NET 7.0 Web API application, it generates ConnectedService.json and Reference.cs files. Both these files contain the WSDL URL, and I need to dynamically pick the URL from appsettings.json based on the environment. File: Reference.cs - URL included like:

private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
{
    if ((endpointConfiguration == EndpointConfiguration.UMarketSC))
    {
        return new System.ServiceModel.EndpointAddress("http://xx.xx.xx.xx:xxxx/services/umarketsc");
    }

throw new System.InvalidOperationException(string.Format("Could not find endpoint with name '{0}'.", endpointConfiguration));

}

File: ConnectedService.json URL included like:

"ExtendedData": {
    "inputs": [
        "http://xx.xx.xx.xx:xxxx/services/umarketsc?wsdl"
    ],
}
Developer technologies ASP.NET ASP.NET API
Developer technologies .NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2023-04-07T11:59:40.42+00:00

    It is not clear what exactly you're struggling with. The service reference has a constructor with endpoint configuration (enum) and URL. Use standard dependency injection to get the URL from configuration. How you design this is totally up to you. I typically wrap the code generated service reference in a standard service pattern and register the service with the dependency injection container. You can also use inheritance.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.