如何使用相同的 wsdl 使用多个 SOAP Web 服务?

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

我正在创建一个服务层,它使用基于环境的端点。使用 ASP.NET Web API 2 和 C# 开发的服务层。端点是 SOAP,而一个面向测试,而另一个面向生产环境。一个镜像另一个,这就是为什么两者都公开相同的 WSDL 的原因。由于端点镜像,编译时恰好发生冲突。由于两个代理 类 具有相同的签名。因此,我的主要问题是如何使我的 WEB API 服务能够与两者一起工作?

在查看了有关此主题的大部分答案之后。我发现他们之间没有任何共同点。因此,我将分享我的想法和为我工作的东西。

请记住两个端点是相同的。我刚刚向我的项目添加了一个服务参考。这样一来,我将只有一个代理 class 来解决编译冲突。然而,我仍然必须找到一种方法来改变终点。为此,我在项目 web.config 文件的 appSettings 部分添加了三个键。

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

然后EndPoint键值就是我用的select对应的环境

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

然后从控制器调用代理class创建

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

       //keep implementing your logic
    }
}

也许这不是最好的实现,但它对我有用。所以我愿意接受任何问题 and/or 建议。

我希望这对需要它的人有用。