简体   繁体   中英

Dynamically call web service!

I need to dynamically call web service! For this I made simple web service Service1.asmx

Then I created the proxy class for the Service1 Web Service, I use the WSDL.exe utility and following command at the command prompt: wsdl /language:VB /out:myclass.vb http://localhost:3245/Service1.asmx?WSDL

This command create class myclass.vb. That class I include in my Windows Application project, but when I do this I get lot of errors.

This is the how created class look like:

Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization

'
'This source code was auto-generated by wsdl, Version=2.0.50727.1432.
'

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432"),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Web.Services.WebServiceBindingAttribute(Name:="Service1Soap", [Namespace]:="http://tempuri.org/")>  _
Partial Public Class Service1
    Inherits System.Web.Services.Protocols.SoapHttpClientProtocol

    Private HelloWorldOperationCompleted As System.Threading.SendOrPostCallback

    '''<remarks/>
    Public Sub New()
        MyBase.New
        Me.Url = "http://localhost:3245/Service1.asmx"
    End Sub

    '''<remarks/>
    Public Event HelloWorldCompleted As HelloWorldCompletedEventHandler

    '''<remarks/>
    <System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace:="http://tempuri.org/", ResponseNamespace:="http://tempuri.org/", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)>  _
    Public Function HelloWorld() As String
        Dim results() As Object = Me.Invoke("HelloWorld", New Object(-1) {})
        Return CType(results(0),String)
    End Function

    '''<remarks/>
    Public Function BeginHelloWorld(ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
        Return Me.BeginInvoke("HelloWorld", New Object(-1) {}, callback, asyncState)
    End Function

    '''<remarks/>
    Public Function EndHelloWorld(ByVal asyncResult As System.IAsyncResult) As String
        Dim results() As Object = Me.EndInvoke(asyncResult)
        Return CType(results(0),String)
    End Function

    '''<remarks/>
    Public Overloads Sub HelloWorldAsync()
        Me.HelloWorldAsync(Nothing)
    End Sub

    '''<remarks/>
    Public Overloads Sub HelloWorldAsync(ByVal userState As Object)
        If (Me.HelloWorldOperationCompleted Is Nothing) Then
            Me.HelloWorldOperationCompleted = AddressOf Me.OnHelloWorldOperationCompleted
        End If
        Me.InvokeAsync("HelloWorld", New Object(-1) {}, Me.HelloWorldOperationCompleted, userState)
    End Sub

    Private Sub OnHelloWorldOperationCompleted(ByVal arg As Object)
        If (Not (Me.HelloWorldCompletedEvent) Is Nothing) Then
            Dim invokeArgs As System.Web.Services.Protocols.InvokeCompletedEventArgs = CType(arg,System.Web.Services.Protocols.InvokeCompletedEventArgs)
            RaiseEvent HelloWorldCompleted(Me, New HelloWorldCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState))
        End If
    End Sub

    '''<remarks/>
    Public Shadows Sub CancelAsync(ByVal userState As Object)
        MyBase.CancelAsync(userState)
    End Sub
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")>  _
Public Delegate Sub HelloWorldCompletedEventHandler(ByVal sender As Object, ByVal e As HelloWorldCompletedEventArgs)

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432"),  _
 System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code")>  _
Partial Public Class HelloWorldCompletedEventArgs
    Inherits System.ComponentModel.AsyncCompletedEventArgs

    Private results() As Object

    Friend Sub New(ByVal results() As Object, ByVal exception As System.Exception, ByVal cancelled As Boolean, ByVal userState As Object)
        MyBase.New(exception, cancelled, userState)
        Me.results = results
    End Sub

    '''<remarks/>
    Public ReadOnly Property Result() As String
        Get
            Me.RaiseExceptionIfNecessary
            Return CType(Me.results(0),String)
        End Get
    End Property
End Class

And some of errors are:

Type 'System.Web.Services.WebServiceBindingAttribute' is not defined. C:\\Documents and Settings\\Owner\\My Documents\\Visual Studio 2008\\Projects\\WindowsApplication3\\WindowsApplication3\\class.vb 16 2 WindowsApplication3

Error 4 Type 'System.Web.Services.Protocols.SoapHttpClientProtocol' is not defined. C:\\Documents and Settings\\Owner\\My Documents\\Visual Studio 2008\\Projects\\WindowsApplication3\\WindowsApplication3\\class.vb 18 14 WindowsApplication3 etc....

Update:

I'm doing this because this service need to be on server and this windows application only call this web service from server. The problem is that I need to write address of server in some kind of config file and later read that address and then call web service! So I can't simple add web reference, because I need to read address of server where service is run from config file.

Any idea?

You must to add reference to System.Web.Services assembly.

For your convenience, you can also to right-click your Visual Studio 2008 project, click "Add Service Reference", "Advanced..." and "Add Web Reference..."

If you are using VS 2005 adding a web reference allows you to specify the URL behavior of the web reference. By setting this to 'dynamic' it will store the url in the config file of the application, there by allowing you to change it if necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM