【问题标题】:HttpContext is not available - Calling Webservice asychronouslyHttpContext 不可用 - 异步调用 Web 服务
【发布时间】:2011-02-08 22:48:09
【问题描述】:

我目前有一个 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 方法的正确方法吗?

谢谢, 贾维德

【问题讨论】:

    标签: .net vb.net web-services asynchronous delegates


    【解决方案1】:

    应通过 Web Reference 访问 Web 服务类,并且 ProcessRequest 方法应使用 WebMethod 属性进行修饰。

    如果您在同一个项目或另一个项目中将 Web 服务用作普通类,就好像它是普通类库一样,您不会通过 Web 服务器,因此 HttpContext.Current 对象为空。

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 2011-09-03
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多