简体   繁体   中英

How to handle concurrent connections asp net (c#)

I have written a simple webapp in asp net. 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?

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

Here is a link on SO about this: What is better: Static variable VS Asp.NET Application Session?

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.

The static keyword on a class and/or a property means that there is only one instance in the entire application domain . This means that all requests to you website will share the values of your vars class as there is only one instance.

I suggest you to follow some ASP.NET tutorials and videos, to gather some basic understanding in ASP.NET. Get started here, they have excellent material: http://www.asp.net/get-started

Read more on the static keyword: http://msdn.microsoft.com/en-us/library/98f28cdx(v=VS.100).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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