【问题标题】:Changing soap action URI based on environment根据环境更改肥皂动作 URI
【发布时间】:2016-06-03 14:02:10
【问题描述】:

我编写的代码与第 3 方网络服务集成。问题是服务的soap操作在测试和生产环境中不同,尽管这个URIs不重要,如果soap操作URI错误,主机返回400 - 错误的请求错误。 主机服务是用 Java 编写的。我们正在使用 C# Web 服务代理。

参考.cs

/// <remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://1.1.1.1:9080/wss/Ping", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
        [return: System.Xml.Serialization.XmlElementAttribute("PingResp", Namespace="http://namespace/data")]
        public PingResp Ping([System.Xml.Serialization.XmlElementAttribute("PingReq", Namespace="http://namespace/data")] PingReq PingReq) {
            object[] results = this.Invoke("Ping", new object[] {
                        PingReq});
            return ((PingResp)(results[0]));
        }

对于生产环境,SoapAction 是:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://2.2.2.2:9080/wss/Ping", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]

有没有办法使这种动态化,即是否可以根据配置设置更改肥皂操作 URI?

我尝试过扩展SoapDocumentMethodAttribute 类,但它是密封的。

【问题讨论】:

    标签: c# web-services


    【解决方案1】:

    因为它需要一个编译时间常数,所以我们必须在编译之前设置这个值,所以基于环境使用它并使用类似ConfigurationManager 是不可能的。

    您可以使用通过#if 预处理器填充的 const 属性创建一个全局静态类,并在构建时指定您的条件编译符号。

       public static class GlobalConstants
        {
    #if DEBUG
            public const string Uri = "devUri";
    #endif
    #if TEST
            public const string Uri = "testUri";
    #endif
    #if RELEASE
            public const string Uri = "releaseUri";
    #endif
    
        }
    

    你可以像这样使用它

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(GlobalConstants.Uri, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
    

    这确实会在迁移到新环境时增加复杂性,但它是一种获得接近您正在寻找的东西的方法。

    一些关于在构建时指定编译符号的文档:https://msdn.microsoft.com/en-us/library/0feaad6z.aspx

    【讨论】:

      猜你喜欢
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      相关资源
      最近更新 更多