0
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _

Public Class Starter
    Inherits System.Web.Services.WebService

    <WebMethod()>
    Public Shared Sub Invoke()
        Dim webForm = New WebForm1()
        ScriptManager.RegisterStartupScript(webForm, GetType(Page), "Execute", "document.getElementById('confirm').click;", True)
        HttpContext.Current.Response.Redirect("Dashboard.aspx") 'System.NullReferenceException

        MsgBox(HttpContext.Current IsNot Nothing) 'False
    End Sub

End Class

I have already googled for other questions (here or in other websites). Many have mentioned about EnableSession = True. I have tried it in my code but an error message was shown after that:

'EnableSession' is not declared. It may be inaccessible due to its protection level.

How can I fix the HttpContext.Current? Do I need to add any configuration settings to web.config for asmx web service? Thanks

Ursidae
  • 87
  • 9
  • The line `` should be ``. – Andrew Morton May 31 '19 at 08:45
  • After making ``, `HttpContext.Current` is still `Nothing` – Ursidae May 31 '19 at 09:16
  • What are you actually trying to do with the web service? It is not intended for things like redirecting. More information: [What is a “web service” in plain English?](https://stackoverflow.com/q/226108/1115360) – Andrew Morton May 31 '19 at 09:23
  • My ultimate aim is to regularly download an html file, which is the static version of a continuously updating webpage. The planned flow is: postback from the page -> initiate task scheduler -> execute task scheduling -> web service to access HttpContext -> JavaScript function to generate offline content -> postback to start downloading. Everything seems to be done now, except the web service part. – Ursidae May 31 '19 at 10:25
  • Anyway, the problem I'm facing right now is not about `Redirect`. At least `HttpContext.Current` has to be fixed first. – Ursidae May 31 '19 at 10:33
  • If you want to download HTML then you would need a handler (ashx, can return anything) rather than a web service (asmx, returns JSON, XML or CSV). – Andrew Morton May 31 '19 at 10:49
  • But handler needs `HttpContext` as parameter, which the Job class cannot provide (`HttpContext.Current` is `Nothing`) – Ursidae Jun 03 '19 at 03:32
  • I think that [How to run Background Tasks in ASP.NET](https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx) could have the information you need. That is, if generating the page really takes so long (say, more than 30 seconds) that it can't be an ordinary handler. – Andrew Morton Jun 03 '19 at 10:36

0 Answers0