0

我已经使用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# 中属性的概念。

4

1 回答 1

1

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

服务器部分的 url 端点是隐式定义的,服务发布在哪里。

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

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

Service1 ws = new Service1();
ws.Url = "http://anyserver.addr/of/the/service.asmx";

bool auth = ws.Authenticate(21,"****");
于 2013-01-25T14:03:29.620 回答