【问题标题】:Dynamically call web service!动态调用网络服务!
【发布时间】:2010-12-30 14:03:54
【问题描述】:

我需要动态调用网络服务!为此,我制作了简单的网络服务 Service1.asmx

然后我为 Service1 Web 服务创建了代理类,我在命令提示符下使用 WSDL.exe 实用程序和以下命令: wsdl /language:VB /out:myclass.vb http://localhost:3245/Service1.asmx?WSDL

此命令创建类 myclass.vb。我将那个类包含在我的 Windows 应用程序项目中,但是当我这样做时会出现很多错误。

这是创建类的样子:

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

一些错误是:

类型“System.Web.Services.WebServiceBindingAttribute”未定义。 C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\WindowsApplication3\WindowsApplication3\class.vb 16 2 WindowsApplication3

错误 4 类型“System.Web.Services.Protocols.SoapHttpClientProtocol”未定义。 C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\WindowsApplication3\WindowsApplication3\class.vb 18 14 WindowsApplication3 等等……

更新:

我这样做是因为此服务需要在服务器上,并且此 Windows 应用程序仅从服务器调用此 Web 服务。问题是我需要在某种配置文件中写入服务器地址,然后读取该地址,然后调用 Web 服务!所以我不能简单地添加网络引用,因为我需要从配置文件中读取运行服务的服务器地址。

有什么想法吗?

【问题讨论】:

    标签: c# .net vb.net


    【解决方案1】:

    如果您使用的是 VS 2005,添加 Web 引用允许您指定 Web 引用的 URL 行为。通过将其设置为“动态”,它会将 url 存储在应用程序的配置文件中,并允许您在必要时更改它。

    【讨论】:

      【解决方案2】:

      您必须添加对System.Web.Services 程序集的引用。

      为方便起见,您也可以右键单击您的 Visual Studio 2008 项目,依次单击“添加服务引用”、“高级...”和“添加 Web 引用...”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-23
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        相关资源
        最近更新 更多