2015-02-24 180 views
1

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

+0

有助于提及您正在使用的平台。我假设.Net,但标记它会是一个改进。 – mccainz 2015-02-24 19:20:54

+0

对不起。正如你所说我的平台是C#语言的.NET。 – yopez83 2015-02-24 19:28:53

回答

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

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

我希望这项工作给需要它的人。