dotnetspark
dotnetspark

Reputation: 581

How to consume multiple SOAP Web Services with the same wsdl?

I am creating a service layer which consumes an end-point based upon the environment. The service layer it's being developed with ASP.NET Web API 2 and C#. The end-points are SOAP whereas, one faces testing while the other faces the production environment. One mirrors the other, reason why both expose the same WSDL. Since the end-point mirrors, it happens to be a conflict while compiling. Due to both proxy classes has the same signature. Therefore, my major question is how could I make my WEB API Service capable of working with both?

Upvotes: 1

Views: 2467

Answers (1)

dotnetspark
dotnetspark

Reputation: 581

After reviewing most of the answers about this topic. I have seen there is nothing in common between them. Therefore I am going to share what I figured out and worked for me.

Keeping in mind both end-points are the same. I just added one Service Reference to my project. So that, I would have just one proxy class what it comes to resolved the compilation conflict. However I still had to find a way to change the end-points. To do so, I added three keys in the appSettings section of the project web.config file.

  <appSettings>        
    <add key="EndPoint" value="TST" />
    <add key="TST" value="http://endpoint_test/Service" />
    <add key="PRD" value="http://endpoint_prod/Service" />
  </appSettings>

The EndPoint key value then is what I used to select the corresponding environment.

/// <summary>
/// Factory to create proxy classes of a service
/// </summary>
public static class ServiceFactory
{
    /// <summary>
    /// Creates an instance of ServiceClient class from the end-point.
    /// Which stands for the run-time end point hosting the service, such as 
    /// Test or Production, defined in the web.config.
    /// </summary>
    /// <returns>Returns a ServiceClient instance.</returns>
    public static ServiceClient CreateInstance() 
    {
        ServiceClient client = new ServiceClient();

        //getting the end point
        switch (ConfigurationManager.AppSettings["EndPoint"])
        {
            case "TST":
                client.Endpoint.Address = new EndpointAddress("https://endpoint_test/Service");
                break;
            case "PRD":
                client.Endpoint.Address = new EndpointAddress("https://endpoint_prod/Service");
                break;
        }

        return client;
    }
}

Then from the controller invoke the proxy class creation

public class PaymentController : ApiController
{
    public IHttpActionResult Action_X()
    {
        //Getting the proxy class
        ServiceClient client = ServiceFactory.CreateInstance();

       //keep implementing your logic
    }
}

Maybe it is not the best implementation but, it worked to me. So I am open to any question and/or suggestions.

I hope this work to whoever needs it.

Upvotes: 1

Related Questions