0

My Base Class has the following method defined:

protected void RespondWithJson<T>(T graph)
{
    var response = HttpContext.Current.Response;
    ClearResponse();
    response.ContentType = "text/json";
    DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
    ds.WriteObject(response.OutputStream, graph);
    response.End();
    Sql_Disconnect();
}

The method belongs to all pages that will respond with JSON, depending on the query parameters in the URL.

The responses that I get from one of my pages makes no sense whatsoever, when you consider the source code involved.

The page is for submitting picks individually for our pick & pack operation. RespondWithJson is the only method that these pages will use to determine their response.

The only response the picksubmission page makes is of the type PickSubmissionResult

e.g.

RespondWithJson(new PickSubmissionResult() { message = "Some URL Parameters are missing from the call!" });

However, the response that I actually get from this page is of the type List<PickingRow>

A different page entirely on the server will make responses with List<PickingRow>.

So why is another page's methods being invoked on this page??

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
LeiMagnus
  • 253
  • 2
  • 13
  • Nothing to do with the answer, but you should read the answers to [Is Response.End() considered harmful?](https://stackoverflow.com/q/1087777/215552) – Heretic Monkey Aug 25 '20 at 14:28
  • Appreciate the comment - I think I have discovered the actual issue I think it's because each of my Pages has a method that is subscribed to an event that belongs to a static class. My understanding was that this static instance would be unique to each request/page - apparently this is not the case. As a result, a previously accessed page's version of this handler is invoked even when visiting a different page. - I will have to revise some of my code and see if the issue persists. – LeiMagnus Aug 25 '20 at 15:12

1 Answers1

0

Solved.

Previously, I had my "Requestor" class set to static. I have now replaced each Page's reference to this class with a reference to their own individual instance of this class (is now a dynamic class)

    protected Requestor requestor;

    protected override void Page_Load(object sender, EventArgs e)
    {
        requestor = new Requestor();
        requestor.SessionSet += Requestor_SessionSet;
    }

Now the Requestor_SessionSet handlers are only invoked on the page visited last, instead of all visited pages within the life-cycle of the domain.

The lesson here:

Static instances are not unique to the request/page but are server-wide

LeiMagnus
  • 253
  • 2
  • 13