简体   繁体   English

如何处理并发连接ASP Net(C#)

[英]How to handle concurrent connections asp net (c#)

I have written a simple webapp in asp net. 我已经在ASP网络中编写了一个简单的Web应用程序。 The first time I go through the program Everything works fine, but if I go there again it uses that variables from my last session. 第一次执行程序时,一切正常,但是如果再次执行该程序,它将使用上次会话中的变量。

So the questions are 所以问题是

  1. Is there anyway to correct this? 反正有没有纠正这个问题?

  2. Does it have something to do with how I make my public variables? 它与我如何设置公共变量有关?

  3. Could I store the variables as cookies, then delete them when the page closes? 我可以将变量存储为cookie,然后在页面关闭时将其删除吗?

This is how I make my public variables: 这是我设置公共变量的方式:

static class vars
{
    public static List<string> directory_names = new List<string>();
    public static List<string> directory = new List<string>();  
    public static int numvar = 0;
    //There are more but they all are made the same way
}

Any Ideas are welcome. 欢迎任何想法。

Thanks, Adam 谢谢亚当

You should avoid static as it means the variable will be the same for every user connected (plus you will probably face threading issue). 您应该避免使用static,因为这意味着每个连接的用户变量都将相同(另外,您可能会遇到线程问题)。

Here is a link on SO about this: What is better: Static variable VS Asp.NET Application Session? 这是关于SO的链接: 更好的是:静态变量VS Asp.NET应用程序会话?

There are several thing we need to address here. 我们需要在这里解决几件事。

First of all, I think there are more code involved than the class you provided. 首先,我认为涉及的代码比您提供的类更多。 I think you need to add that in order for us to give you an accurate answer. 我认为您需要添加该内容,以便我们为您提供准确的答案。

However, from what I can see from the class you have provided, there is one thing in particular that you have done and that is the static keyword. 但是,从您提供的类中可以看到,您要做的一件事情特别是static关键字。

The static keyword on a class and/or a property means that there is only one instance in the entire application domain . 类和/或属性上的static关键字表示在整个应用程序域中只有一个实例。 This means that all requests to you website will share the values of your vars class as there is only one instance. 这意味着对您网站的所有请求将共享vars类的值,因为只有一个实例。

I suggest you to follow some ASP.NET tutorials and videos, to gather some basic understanding in ASP.NET. 我建议您遵循一些ASP.NET教程和视频,以对ASP.NET有所了解。 Get started here, they have excellent material: http://www.asp.net/get-started 从这里开始,他们有很好的材料: http : //www.asp.net/get-started

Read more on the static keyword: http://msdn.microsoft.com/en-us/library/98f28cdx(v=VS.100).aspx 阅读更多有关static关键字的信息: http : //msdn.microsoft.com/zh-cn/library/98f28cdx(v=VS.100).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM