简体   繁体   English

会话对象在Azure上为空

[英]Session object becomes null on Azure

So, I developed an application in MVC on C#. 因此,我在C#上的MVC中开发了一个应用程序。 Everything works perfectly and smooth locally. 一切在本地都能正常运行。

I decided to mount the whole thing on Microsoft Azure. 我决定将整个安装在Microsoft Azure上。 My Session object becomes null for some reason, causing a session expires because of my validations. 由于某种原因,我的Session对象变为null,由于我的验证,导致会话过期。 This is random, sometimes after some seconds, sometimes after a minute. 这是随机的,有时在几秒钟后,有时在一分钟后。 I am sure this is not because of time of inactivity because it happens when I am clicking on things. 我确定这不是因为没有时间,而是因为我单击事物时会发生这种情况。

This is my login method where I create my object: 这是我创建对象的登录方法:

    public ActionResult Login(MyWebApplication.Models.LoginRQ User)
    {
        if (ModelState.IsValid)
        {
            MyWebApplication.Models.LoginRS login = new MyWebApplication.Models.LoginRS();
            try
            {
                MyWebApplication.LoginBusiness LoginProxy = new MyWebApplication.LoginBusiness();
                login = LoginProxy.Login(User);

                if (login.Logged == false)
                {
                    ViewBag.MessageError = Wrong user";
                }

                Session["InfoUser"] = login;
                return RedirectToAction("Index","Inicio");
            }
            catch (Exception ex)
            {
                ViewBag.MessageError = ex.Message;
            }

        }
        return View(User);
    }

This is my validation on every method: 这是我对每种方法的验证:

    public static bool ValidateSession(MyWebApplication.Models.LoginRS InfoUser)
    {
        if (InfoUser == null)
        {
            throw new ArgumentException("SESSION EXPIRED");
        }
        return true;
    }

I use this validation when instanciating a method from my business layer. 从我的业务层实例化方法时,会使用此验证。 For example, this is my business layer for Customer CRUD: 例如,这是我的客户CRUD业务层:

public class CustomerBusiness { internal MyWebApplication.Models.LoginRS InfoUser; 公共类CustomerBusiness {内部MyWebApplication.Models.LoginRS InfoUser; internal MyWebApplication.Models.Customer OBJRES_Residentes = new MyWebApplication.Models.Customer(); 内部MyWebApplication.Models.Customer OBJRES_Residentes =新的MyWebApplication.Models.Customer();

    [Logger]
    public CustomerBusiness(MyWebApplication.Models.LoginRS InfoUser)
    {
        MyWebApplication.LoginBusiness.ValidateSession(InfoUser);
        this.InfoUser = InfoUser;
    }

} As you can see, I need the InfoUser so I log the userName on CRUD operations. }如您所见,我需要InfoUser,因此我将用户名记录在CRUD操作上。

This is part of my WebConfig: 这是我的WebConfig的一部分:

  <system.web>
    <sessionState mode="InProc" timeout ="20" cookieless="false"></sessionState>
    <customErrors mode="Off">
    </customErrors>
  </system.web>

No lets talk about how I configured this on Azure. 没有让我们谈论我如何在Azure上配置它。 I created a WebApplication and a SQL Server Azure DB. 我创建了一个WebApplication和一个SQL Server Azure DB。 This is an image: 这是一张图片:

Azure_Portal Azure_Portal

This is so frustrating that I am thinking about using DB cache, even when I know the performance would be affected. 这太令人沮丧了,即使我知道性能会受到影响,我也在考虑使用数据库缓存。

Any ideas? 有任何想法吗? I really appreciate your help guys 我真的很感谢你们的帮助

You're doing it in the wrong way. 您做错了方法。 You should have your own State Server, rather than storing the session on the web server. 您应该拥有自己的状态服务器,而不是将会话存储在Web服务器上。 The idea of using Azure, is to add elastic scale when needed. 使用Azure的想法是在需要时添加弹性刻度。

Here a few options: 这里有一些选择:

1-Add Azure Redis Cache and make it your session manager to all instances of your web app; 1-添加Azure Redis缓存并使其成为您的会话管理器到Web应用程序的所有实例;

https://azure.microsoft.com/en-us/documentation/articles/cache-aspnet-session-state-provider/ https://azure.microsoft.com/en-us/documentation/articles/cache-aspnet-session-state-provider/

2-Use Sql Database to manage the state. 2-使用Sql数据库管理状态。

https://azure.microsoft.com/en-us/blog/using-sql-azure-for-session-state/ https://azure.microsoft.com/en-us/blog/using-sql-azure-for-session-state/

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

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