我目前有一个Web应用程序项目,该项目还包括同一项目中的Web服务。我目前需要异步调用Web服务。因为,我没有在同一个项目中添加Web服务作为Web引用,我尝试使用委托来调用Web服务方法,我看到一个异常“HttpContext不可用。这个类只能在上下文中使用一个ASP.NET请求“当我尝试在我的Web服务方法调用中访问Application属性时。
Web Method in MyWebServices Class
Public Function ProcessRequest() As Boolean
If Application(STRING_KEY) Is Nothing Then 'Exception happens here'
Return True
Else
Return False
End If
End Sub
.ASPX Codebehind File
Public Delegate Function ProcessRequestDelegate() As Integer
Protected Sub btnSender_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sender.Click
Dim del As ProcessRequestDelegate
del = New ProcessRequestDelegate(AddressOf ProcessRequest)
del.BeginInvoke(null, null)
End Sub
Protected Function ProcessRequest() As Boolean
Dim services As MyWebService
services = New MyWebService()
Return services.ProcessRequest()
End Function
我在这里尝试做的是以异步方式调用Web方法的正确方法吗?
谢谢, Javid
答案 0 :(得分:1)
应该通过Web Reference访问Web服务类,并且应该使用WebMethod属性修饰ProcessRequest方法。
如果您在同一个项目中使用Web服务作为普通类,或者从另一个项目中使用Web服务,就像它是普通的类库一样,您不会通过Web服务器,因此HttpContext.Current对象为null。