Hello-World
Hello-World

Reputation: 9555

how do you pass an httpcontext object to a web service

How do you pass an httpcontext.current object to a web service and use that object in the service, I get an error saying that it is expecting a string - surly this must be possible?

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

Upvotes: 1

Views: 1251

Answers (2)

Mario S
Mario S

Reputation: 11945

The HttpContext is not serilazible, so it can't be sent as a string. HttpContext is an complex object with other complex properties, so it would be rather big if you would serialize it (which means it would be alot slower sending the data).

I believe it's better to encapsulate the information you need in a custom class instead and send that to the service.

That is, create a class with simple types that can be serialized (string, int, double, etc.) and fill it with the information you need.

Upvotes: 3

CodingGorilla
CodingGorilla

Reputation: 19842

You cannot pass an HttpContext ByVal. ByVal means by value, which means that the HttpContext's value needs to be copied in order to be passed to your method. Since it's a [complex] object, you can't do that. Instead you need to pass it ByRef, which means pass a reference to the object to your method and work off of that reference.

Upvotes: 1

Related Questions