C#:方括号替代/多个 SOAP 地址的类

本文关键字:SOAP 地址 多个 方括号 | 更新日期: 2023-09-27 17:55:53

我使用该工具生成了一个 SOAP 类wsdl.exe。不幸的是,它似乎绑定到一个特定的 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
}

因此,来自身份验证的属性(具有 HTTP URL 的属性)是可变的。到目前为止,我找到的唯一解决方案是在 Service1 类中创建一个静态字符串并像这样更改身份验证代码:

/// <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# 中属性的概念。

C#:方括号替代/多个 SOAP 地址的类

命名空间

属性不是服务终结点。目的是准确定义一个命名空间,或者更好地定义 soap 调用中使用的实体/方法的 xml 命名空间。

服务器部件的 url 终结点是在发布服务的位置隐式定义的。

在客户端部分取决于具体的实现。

对于 wsdl.exe客户端,我记得语法是这样的:

Service1 ws = new Service1();
ws.Url = "http://anyserver.addr/of/the/service.asmx";
bool auth = ws.Authenticate(21,"****");