c# - 根据环境更改 SOAP 操作 URI

标签 c# web-services

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

引用.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)]

有没有办法让它变得动态,即是否可以根据配置设置更改 soap 操作 URI?

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

最佳答案

因为它需要一个编译时间常量,我们必须在编译之前设置这个值,所以根据环境设置它并使用像 ConfigurationManager 这样的东西是不可能的。

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

   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

关于c# - 根据环境更改 SOAP 操作 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37616446/

相关文章:

c# - HttpClientHandler :Dispose (bool )' is inaccessible from method ` System.Net.ServicePointManager:CloseConnectionGroup(字符串)

c# - 如何获取 LightSwitch 中 IContentItemProxy 上的 SetBinding 方法的路径?

c# - 注入(inject) UserManager 和 UserStore

c# - 缓存辅助类是单例模式的候选者吗?

android - SSL 握手异常 : Certificate Exception using HttpURLConnetion with Android 4. 2.2

java - getRequestDispatcher 如何在同一服务器的不同项目中为 servlet 工作?

asp.net-mvc - 设计选择 : WCF or Service Stack?

web-services - RESTful API 响应状态码消歧

c# - 在 ASP.NET 2.0 中管理设置(配置)的最佳方式是什么?

.net - 防止 .NET 写入 C :\Windows\Temp