【问题标题】:How to consume multiple SOAP Web Services with the same wsdl?如何使用相同的 wsdl 使用多个 SOAP Web 服务?
【发布时间】:2023-03-19 13:37:01
【问题描述】:

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

【问题讨论】:

  • 有助于提及您正在使用的平台。我假设 .Net 但标记它会是一种改进。
  • 很抱歉。正如你所说,我的平台是 .NET,使用 C# 作为语言。

标签: .net web-services soap wsdl soap-client


【解决方案1】:

在查看了有关此主题的大部分答案后。我已经看到他们之间没有任何共同之处。因此,我将分享我发现并为我工作的内容。

请记住,两个端点是相同的。我刚刚为我的项目添加了一个服务引用。因此,我将只有一个代理类来解决编译冲突。但是,我仍然必须找到一种方法来更改端点。为此,我在项目 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键值就是我用来选择对应环境的。

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

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

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

       //keep implementing your logic
    }
}

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多