C#: square bracket alternative / a class for multiple SOAP addresses

会有一股神秘感。 提交于 2019-12-13 03:59:35

问题


I've generated a SOAP class using wsdl.exe tool. Unfortunately, it seems to be bound to a specific URL and I need to be able to change it on per-instance basis (I'd like to be able to connect to multiple URLs that share the same interface). So, I'd like to change such code:

/// <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

}

So that the attributes from Authenticate (the ones with HTTP URL's) are variable. The only solution I've found so far is to make a static string inside of Service1 class and change the Authenticate code like that:

/// <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

}

Is there a better solution that would pull this info from instances instead of forcing me to change them every request? I must admit I don't exactly understand the concept of attributes in C#.


回答1:


The namespace attributes aren't the service endpoint. The purpose is to define exactly a namespace, or bettere the xml namespace of the entities/methods used in soap call.

The url endpoint of the server part is implicit defined where the service is published.

In the client part depend on the specific implementation.

For the wsdl.exe client as I remember the syntax is somethings like that:

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

bool auth = ws.Authenticate(21,"****");


来源:https://stackoverflow.com/questions/14523135/c-square-bracket-alternative-a-class-for-multiple-soap-addresses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!