1

您如何将 httpcontext.current 对象传递给 Web 服务并在服务中使用该对象,我收到一条错误消息,说它需要一个字符串 - 这一定是可能的?

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Sub doThis(ByVal HC As HttpContext)
        'do something
    End Sub
End Class


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim s As test2.WebService = New test2.WebService
    s.doThis(HttpContext.Current)
End Sub
4

2 回答 2

3

HttpContext 不是可序列化的,因此不能作为字符串发送。HttpContext 是一个具有其他复杂属性的复杂对象,因此如果您将其序列化,它会相当大(这意味着发送数据会慢很多)。

我相信最好将您需要的信息封装在自定义类中并将其发送到服务。

也就是说,使用可以序列化的简单类型(字符串、整数、双精度等)创建一个类,并用您需要的信息填充它。

于 2012-08-20T17:24:19.223 回答
1

你不能通过HttpContext ByVal. ByVal按值表示,这意味着HttpContext需要复制 ' 的值才能传递给您的方法。由于它是一个 [复杂] 对象,因此您不能这样做。相反,您需要传递它ByRef,这意味着将对对象的引用传递给您的方法并处理该引用。

于 2012-08-20T17:24:46.753 回答