我编写的代码与第 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/