我已经使用wsdl.exe
工具生成了一个 SOAP 类。不幸的是,它似乎绑定到一个特定的 URL,我需要能够在每个实例的基础上更改它(我希望能够连接到共享同一接口的多个 URL)。所以,我想改变这样的代码:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="Service1Soap", Namespace="http://productmarket.bigbrain.math.uni.lodz.pl/")]
public partial class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol {
// some code here
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://productmarket.bigbrain.math.uni.lodz.pl/Authenticate", RequestNamespace="http://productmarket.bigbrain.math.uni.lodz.pl/", ResponseNamespace="http://productmarket.bigbrain.math.uni.lodz.pl/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public bool Authenticate(int ContractorId, string Password) {
object[] results = this.Invoke("Authenticate", new object[] {
ContractorId,
Password});
return ((bool)(results[0]));
}
// more code here
}
因此来自 Authenticate 的属性(带有 HTTP URL 的属性)是可变的。到目前为止,我发现的唯一解决方案是在 Service1 类中创建一个静态字符串,并像这样更改 Authenticate 代码:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="Service1Soap", Namespace="http://productmarket.bigbrain.math.uni.lodz.pl/")]
public partial class Service1 : System.Web.Services.Protocols.SoapHttpClientProtocol {
//some code
public static string prefix = "http://productmarket.bigbrain.math.uni.lodz.pl/";
public static string soap_namespace = "http://productmarket.bigbrain.math.uni.lodz.pl/";
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute(Service1.prefix+"Authenticate", RequestNamespace=Service1.soap_namespace, ResponseNamespace=Service1.soap_namespace, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public bool Authenticate(int ContractorId, string Password) {
object[] results = this.Invoke("Authenticate", new object[] {
ContractorId,
Password});
return ((bool)(results[0]));
}
//some code
}
有没有更好的解决方案可以从实例中提取这些信息,而不是强迫我在每个请求中都更改它们?我必须承认我并不完全理解 C# 中属性的概念。