Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;

C#
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.

C#
[WebMethod]
public void Login(User u) {
    
    // .. do the database activities and other business logic
    // .. response object is ready
    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

C#
[WebMethod]
public void Login(User u) {
    
    // .. do the database activities and other business logic
    // .. response object is ready
    
    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.
Posted
Updated 16-Oct-17 18:56pm
v4
Comments
Karthik_Mahalingam 16-Oct-17 23:10pm    
why do you use [WebMethod] for a class.
Nayan Ambaliya 16-Oct-17 23:53pm    
Sorry, that was my bad. I have changed and improved the question.
Would you by any chance shed some light on this?

Thanks.
Richard Deeming 17-Oct-17 13:08pm    
I'd be inclined to make that an extension method[^] on the HttpResponse class, and include the JSON serialization in the method.

You should avoid using Response.End, since that throws a ThreadAbortException, and is only intended for backwards-compatibility with "classic" ASP.

And if you're inheriting from WebService, you can use its Context[^] property to get the associated HttpContext, rather than using HttpContext.Current.

public static class GlobalUtilities 
{ 
    public static void ReturnJson(this HttpResponse response, object result)
    {
        string json = JsonConvert.SerialiseObject(result);
        
        response.Clear();
        response.ContentType = "application/json; charset=utf-8";
        response.Write(json);
        response.Flush();
    }
}

...

[WebMethod]
public void Login(User u) {
    
    // .. do the database activities and other business logic
    // .. response object is ready
    Context.Response.ReturnJson(responseObject);
}
Nayan Ambaliya 17-Oct-17 23:11pm    
This is good. I am always inclined towards how to create such utility methods and utilize them in the code.

Thanks.

1 solution

Quote:
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?

No,
It is true in the case of a static variable, not the static method
 
Share this answer
 
Comments
Nayan Ambaliya 17-Oct-17 1:38am    
Does this mean that multiple calls to a static method by different clients with input variables (as in the question) will not overwrite the input variable value as well as the HttpResponse as inside the method?
Karthik_Mahalingam 17-Oct-17 2:46am    
it wont override.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900