调用Web服务方法在VS 2008

时间:2015-06-26 14:19:02

标签: c# vb.net visual-studio-2008 axis

我的情况: 我有一个Winform应用程序(.NET 2008),我必须使用Web服务(Java Axis)。 我使用SoapUI测试Web服务,它工作正常。但是,当我在VS 2008中通过服务引用使用它时,它返回null。

显然,它似乎是一个众所周知的问题,它有一个很好的解决方案。对不起,这次我找不到解决方案!!

还有其他一些像我这样的案例(like this),但它对我不起作用。

这是WSDL:

<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher" xmlns:intf="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher" xmlns:tns1="http://services.web.wsfd.bancorio.com.ar" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher">
<wsdl:types>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://services.web.wsfd.bancorio.com.ar">
        <element name="process" type="xsd:anyType"/>
    </schema>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher">
        <element name="processReturn" type="xsd:anyType"/>
    </schema>
</wsdl:types>
<wsdl:message name="processRequest">
    <wsdl:part element="tns1:process" name="part"/>
</wsdl:message>
<wsdl:message name="processResponse">
    <wsdl:part element="impl:processReturn" name="processReturn"/>
</wsdl:message>
<wsdl:portType name="SOAPService">
    <wsdl:operation name="process">
        <wsdl:input message="impl:processRequest" name="processRequest"/>
        <wsdl:output message="impl:processResponse" name="processResponse"/>
    </wsdl:operation>
</wsdl:portType>
    <wsdl:binding name="SOAPMessageDispatcherSoapBinding" type="impl:SOAPService">
    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="process">
        <wsdlsoap:operation soapAction=""/>
        <wsdl:input name="processRequest">
        <wsdlsoap:body use="literal"/>
        </wsdl:input>
        <wsdl:output name="processResponse">
        <wsdlsoap:body use="literal"/>
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>
<wsdl:service name="SOAPServiceService">
    <wsdl:port binding="impl:SOAPMessageDispatcherSoapBinding" name="SOAPMessageDispatcher">
        <wsdlsoap:address location="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher"/>
    </wsdl:port>
</wsdl:service>

这是VS 2008创建的自动定义:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher", ConfigurationName="ServiceReference1.SOAPService")]
public interface SOAPService {

    // CODEGEN: Se está generando un contrato de mensaje, ya que la operación process no es RPC ni está encapsulada en un documento.
    [System.ServiceModel.OperationContractAttribute(Action="", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    WindowsFormsApplication3.ServiceReference1.processResponse process(WindowsFormsApplication3.ServiceReference1.processRequest request);
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class processRequest {

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://services.web.wsfd.bancorio.com.ar", Order=0)]
    public object process;

    public processRequest() {
    }

    public processRequest(object process) {
        this.process = process;
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class processResponse {

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher", Order=0)]
    public object processReturn;

    public processResponse() {
    }

    public processResponse(object processReturn) {
        this.processReturn = processReturn;
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface SOAPServiceChannel : WindowsFormsApplication3.ServiceReference1.SOAPService, System.ServiceModel.IClientChannel {
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class SOAPServiceClient : System.ServiceModel.ClientBase<WindowsFormsApplication3.ServiceReference1.SOAPService>, WindowsFormsApplication3.ServiceReference1.SOAPService {

    public SOAPServiceClient() {
    }

    public SOAPServiceClient(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }

    public SOAPServiceClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public SOAPServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public SOAPServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    WindowsFormsApplication3.ServiceReference1.processResponse WindowsFormsApplication3.ServiceReference1.SOAPService.process(WindowsFormsApplication3.ServiceReference1.processRequest request) {
        return base.Channel.process(request);
    }

    public object process(object process1) {
        WindowsFormsApplication3.ServiceReference1.processRequest inValue = new WindowsFormsApplication3.ServiceReference1.processRequest();
        inValue.process = process1;
        WindowsFormsApplication3.ServiceReference1.processResponse retVal = ((WindowsFormsApplication3.ServiceReference1.SOAPService)(this)).process(inValue);
        return retVal.processReturn;
    }
}

最后,我从服务参考更改为Web参考。 这是VS 2008 for Web Reference创建的定义:

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.5420")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="SOAPMessageDispatcherSoapBinding", Namespace="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher")]
public partial class SOAPServiceService : System.Web.Services.Protocols.SoapHttpClientProtocol {

    private System.Threading.SendOrPostCallback processOperationCompleted;

    private bool useDefaultCredentialsSetExplicitly;

    /// <remarks/>
    public SOAPServiceService() {
        this.Url = global::WindowsFormsApplication3.Properties.Settings.Default.WindowsFormsApplication3_ServicioDeFirma_SOAPServiceService;
        if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
            this.UseDefaultCredentials = true;
            this.useDefaultCredentialsSetExplicitly = false;
        }
        else {
            this.useDefaultCredentialsSetExplicitly = true;
        }
    }

    public new string Url {
        get {
            return base.Url;
        }
        set {
            if ((((this.IsLocalFileSystemWebService(base.Url) == true) 
                        && (this.useDefaultCredentialsSetExplicitly == false)) 
                        && (this.IsLocalFileSystemWebService(value) == false))) {
                base.UseDefaultCredentials = false;
            }
            base.Url = value;
        }
    }

    public new bool UseDefaultCredentials {
        get {
            return base.UseDefaultCredentials;
        }
        set {
            base.UseDefaultCredentials = value;
            this.useDefaultCredentialsSetExplicitly = true;
        }
    }

    /// <remarks/>
    public event processCompletedEventHandler processCompleted;

    /// <remarks/>
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
    [return: System.Xml.Serialization.XmlElementAttribute("processReturn", Namespace="http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher")]
    public object process([System.Xml.Serialization.XmlElementAttribute("process", Namespace="http://services.web.wsfd.bancorio.com.ar")] object process1) {
        object[] results = this.Invoke("process", new object[] {
                    process1});
        return ((object)(results[0]));
    }

    /// <remarks/>
    public void processAsync(object process1) {
        this.processAsync(process1, null);
    }

    /// <remarks/>
    public void processAsync(object process1, object userState) {
        if ((this.processOperationCompleted == null)) {
            this.processOperationCompleted = new System.Threading.SendOrPostCallback(this.OnprocessOperationCompleted);
        }
        this.InvokeAsync("process", new object[] {
                    process1}, this.processOperationCompleted, userState);
    }

    private void OnprocessOperationCompleted(object arg) {
        if ((this.processCompleted != null)) {
            System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
            this.processCompleted(this, new processCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
        }
    }

    /// <remarks/>
    public new void CancelAsync(object userState) {
        base.CancelAsync(userState);
    }

    private bool IsLocalFileSystemWebService(string url) {
        if (((url == null) 
                    || (url == string.Empty))) {
            return false;
        }
        System.Uri wsUri = new System.Uri(url);
        if (((wsUri.Port >= 1024) 
                    && (string.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) == 0))) {
            return true;
        }
        return false;
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.5420")]
public delegate void processCompletedEventHandler(object sender, processCompletedEventArgs e);

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.5420")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class processCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {

    private object[] results;

    internal processCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
            base(exception, cancelled, userState) {
        this.results = results;
    }

    /// <remarks/>
    public object Result {
        get {
            this.RaiseExceptionIfNecessary();
            return ((object)(this.results[0]));
        }
    }
}

我正在阅读本网站上的许多论坛,等等。但我找不到解决方案。

感谢大家。

1 个答案:

答案 0 :(得分:0)

实际上,我解决了(临时,像个穴居人)做网页请求和阅读网络回复。它有效吗?是的,但我认为这不是一个好方法,不是质量方式。

我认为必须在某个地方由某人解决。

我做了类似的事情:

Using cliente As New WebClient()

    cliente.Headers.Add("SOAPAction", """http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher""")
    cliente.Headers.Add("Content-Type", "text/xml; charset=utf-8")

    Dim mensaje As String = String.Empty
    mensaje &= "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ser=""http://services.web.wsfd.bancorio.com.ar"">"
    mensaje &= "    <soapenv:Header/>"
    mensaje &= "    <soapenv:Body>"
    mensaje &= "{0}"
    mensaje &= "    </soapenv:Body>"
    mensaje &= "</soapenv:Envelope>"

    Dim data = Encoding.UTF8.GetBytes(mensaje)
    Dim result = cliente.UploadData("http://wds2.ar.bsch:9089/WSFDAxis/services/SOAPMessageDispatcher", data)

    Dim xml = New XmlDocument()
    xml.LoadXml(result)

    Dim nodes = xml.GetElementsByTagName("codRet")

    Dim codRet = String.Empty

    For Each node As XmlNode In nodes
        codRet = node.InnerText
    Next

    Return CInt(codRet) = 0

End Using