Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12141

Static class value will not persist when project published to IIS host

$
0
0

Sorry, I'm really lost, I don't know what makes the problem, Is It IIS or my code? so I'll explain from first.I'm working on a website project using ASP.net MVC and Web API. I need to save users data in a static class, so I'll use them for validations and other things. For that I have a static class called Globals. There are some properties in it, one of them is a list of users:

public static class Globals{     private static List<UserData> users { get; set; } = new List<UserData>();     // ETC}public class UserData{    public string identityCode { get; set; }    public bool userIsLogIn { get; set; } = false;    // ETC}

When user request the API, I will create a cookie with a code for it and I will add that cookie code to list as new user. For that I have created a class to check the cookie:

public static class CookieHandler{    public static CookieHeaderValue CheckUpCookie(HttpRequestMessage Request)    {        CookieHeaderValue cookieValue = Request.Headers.GetCookies("identityCode").FirstOrDefault();        if (cookieValue == null || !Globals.users.Any(x => x.identityCode == userIdentityCode["identityCode"].Value))        {            string cookieCode = 123456; // Random number generates, for example 123456            Globals.users.Add(new UserData() { identityCode = cookieCode });            var resCookie = new CookieHeaderValue("identityCode", cookieCode );            resCookie.Expires = DateTimeOffset.Now.AddDays(1);            resCookie.Domain = Request.RequestUri.Host;            resCookie.Path = "/";            resCookie.HttpOnly = true;            resCookie.Secure = false;            return resCookie;        }         else         {            cookieValue.Expires = DateTimeOffset.Now.AddDays(1);            cookieValue.Domain = Request.RequestUri.Host;            cookieValue.Path = "/";            cookieValue.HttpOnly = true;            cookieValue.Secure = false;            return cookieValue;        }    }}

In future I will check if user have the cookie and I will check if the same cookie code is available in the users list, If yes so I'll return the user if not I will create new one and I'll return new one:

CookieHeaderValue cookieConnection = CookieHandler.CheckUpCookie(Request);UserData userConnection = Globals.users.FirstOrDefault(x => x.identityCode == cookieConnection["identityCode"].Value);// Now I will use userConnection in future validations and ETC.

Everything is working completely fine in debug and develop mode. BUT when I publish the project to the IIS Service, Whenever I check the users list it's empty. I tried finding the problem by logs and I find out that everything is working ok, Code successfully generates, and the user will add in users list BUT next time that I check the list, It's empty. I have no code to clean or delete the users list. When the list is empty It always returns new user. It seems like something like garbage collector or something cleans the static class, Because when I checked, Every properties in static class is cleaned!

I have tried IIS management settings. Checking the recycle logs but I got nothing. I checked everything carefully. The cookie is saved correctly, The code generates correctly but the problem still remains.

Can anyone help?


Viewing all articles
Browse latest Browse all 12141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>