Web方法中的Web方法和静态方法 [英] Webmethods in a web service and static methods

查看:68
本文介绍了Web方法中的Web方法和静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家,



这可能是一个非常愚蠢的问题,但我很困惑。



我有一个Web服务,它有很多web方法(用[WebMethod]属性声明)



所有这些服务都执行业务逻辑,与数据库交互并创建类的自定义对象,并将值指定给其属性作为响应值。



我必须将对象转换为JSON并返回客户端。



我在静态类中有一个名为ReturnResponseToClient的静态方法;



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();

    }

}





上述方法在结束时被调用Web服务中的每个[WebMethod]然后将响应返回给客户端。我已经这样做了,所以我不必在每个Web服务的每个[WebMethod]中编写5行代码。





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) {
    
    // .. do the database activities and other business logic
    // .. response object is ready
    GlobalUtilities.ReturnResponseToClient(JsonConvert.SerialiseObject(responseObject), HttpContext.Current);
    
}





问题是:

这可能适合一次使用虽然开发,但是,我怀疑当多个用户使用系统和具有不同参数的相同Web服务时肯定会崩溃。这是因为它是静态的,并且可能在另一个调用仍在执行时覆盖输入参数。那是对的吗?如果是这样,可以做些什么呢?



我尝试过:



我尝试在其中创建一个普通的公共类和一个公共方法,这样每次我想将响应发送回客户端时,我都可以创建该公共类的对象,然后通过该对象访问该方法。这样,对该方法的所有调用都保持独立,并且不会覆盖实际响应。





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) {
    
    // .. 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.

推荐答案

Quote:

这可能适用于开发时的一次使用,但是,我怀疑当多个用户使用系统和具有不同参数的相同Web服务时它肯定会崩溃。这是因为它是静态的,并且可能在另一个调用仍在执行时覆盖输入参数。那是对的吗?

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?



否,

如果静态,则 true 变量,而不是静态方法


这篇关于Web方法中的Web方法和静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆