0

Can someone please check why my .ashx file is not firing the alert is being hit fine

function setHeartbeat() {
        setInterval("heartbeat()", 20000); 
           }

         function heartbeat() {
             alert('ajax called');
                 $.ajax({
                     type: "GET",
                     url: "SessionHeartbeat.ashx",
                     data: null,
                     success: function (data) {
                         beatHeart(30);
                         setHeartbeat();

    }});
    }

.ashx file looks like this and is called SessionHeartbeat.ashx

public class SessionHeartbeat : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable { get { return false; } }

        public void ProcessRequest(HttpContext context)
        {
            context.Session["Heartbeat"] = DateTime.Now;
        }
    }
}

this is my web.config

<httpHandlers>
    <add verb="GET,HEAD" path="SessionHeartbeat.ashx" validate="false" type="SessionHeartbeat"/>
</httpHandlers>
AShah
  • 846
  • 2
  • 17
  • 33

2 Answers2

3

If you use handler class, you have to add them to <httpHandlers> tag inside web.config.

What you have is a generic handler. You do not need to do anything, so remove it from <httpHandlers> tag inside web.config.

The following code should work as long as you can access SessionHeartbeat.ashx from client browser.

<script>

    setInterval(function() {
        $.ajax({
            type: "GET",
            url: "<%= ResolveUrl("~/SessionHeartbeat.ashx") %>",
            data: null,
            success: function (data) {
                console.log('Heartbeat called at ' + data);
            }
        });
    }, 20000);

</script>

public class SessionHeartbeat : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        var dateTimeNow = DateTime.Now;
        context.Session["Heartbeat"] = dateTimeNow;
        context.Response.Write(dateTimeNow);
    }

    public bool IsReusable { get { return false; } }
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • thank this worked for me fine although I deleted my .ashx file and recreated it and then it started working - you were correct I didn't need it in the web.config file - thanks so much! – AShah Feb 01 '18 at 22:46
1

The example you mentioned worked after updating web.config:

<configuration>
      <!--Other settings-->
      <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
      </system.webServer>
      <system.web>
          <httpHandlers>
             <add verb="GET,HEAD" path="SessionHeartbeat.ashx" validate="false" type="SessionHeartbeat"/>
          </httpHandlers>
      </system.web>
</configuration>

But this depends on IIS version and how it's configured. See this answer: Generic Handler not working on IIS. It explains that a Generic Handler still needs settings in your web.config in some scenarios and that was the case when I've followed the example in the link you posted.

Also I have previously added response write in ProcessRequest

public void ProcessRequest(HttpContext context)
{
    context.Response.Write("Hello World");
    context.Session["Heartbeat"] = DateTime.Now;
}

If you deploy this in IIS you probably will need to register your handler but it was not necessary in my IIS Express.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • yes that's being called fine the ajax function is also being called as the alert is firing – AShah Feb 01 '18 at 21:43