Dear experts,
This may be a very dumb question but I am confused.
I have a web service which has many web methods (declared with the attribute [WebMethod])
All these services do business logic, interact with the database and creates a custom object of a class with values assigned to its properties as response values.
I have to convert the object to JSON and return to the client.
I have a static method in a static class called ReturnResponseToClient as;
public static class GlobalUtilities {
public static string ReturnResponseToClient(string response, HttpContext currentContext) {
context.Response.Clear();
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(response);
context.Response.Flush();
context.Response.End();
}
}
The above method gets called at the end of every [WebMethod] in the web service which then returns the response to the client. I have done this so that I do not have to write 5 lines of code in every [WebMethod]s in every web service.
[WebMethod]
public void Login(User u) {
GlobalUtilities.ReturnResponseToClient(JsonConvert.SerialiseObject(responseObject), HttpContext.Current);
}
The question is:
This may work for one use while development, but, I suspect it will definitely crash when multiple users are using the system and the same web service with different parameters. This is because it is static and may overwrite the input parameters while another call is still executing. Is that correct? If so, what can be done about it?
What I have tried:
I have tried creating a normal public class and a public method within it so that everytime I want to send the response back to the client, I can create an object of that public class and then access the method via that object. This way all the calls to that method remains independent and does not overwrite the actual response
[WebMethod]
public void Login(User u) {
GlobalUtilities gu = new GlobalUtilities();
gu.ReturnResponseToClient(JsonConvert.SerialiseObject(responseObject), HttpContext.Current);
}
The above would of course work when I create a non-static class.
If there is any other way I can achieve this, please assist me.
Thanks.