【发布时间】: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 服务!所以我不能简单地添加网络引用,因为我需要从配置文件中读取运行服务的服务器地址。
有什么想法吗?
【问题讨论】: