Session对象正在localhost上工作,但在服务器上失败

时间:2015-11-25 01:46:56

标签: c# asp.net-mvc entity-framework session

我在MVC 5 Web应用程序中创建了一个SessionManager类来维护应用程序的会话。这个类在localhost上运行正常,但在服务器上运行。这是我的SessionManager类代码:

public class SessionManager : BaseController
{
    private HttpContext _context;

    public SessionManager()
    {
        //TODO:
    }
    public SessionManager(HttpContext context)
    {
        _context = context;
    }

    private RAWOnlineDBEntities db = new RAWOnlineDBEntities();

    private string _FromMailID = string.Empty;
    private string _FromPassword = string.Empty;
    private string _CompanyName = string.Empty;
    private string _AdminEmailID = string.Empty;


    public string FromMailID
    {
        get 
        {
            if (_context != null && _context.Session != null)
            {
                if (_context.Session["FromMailID"] == null)
                {
                    _context.Session["FromMailID"] = db.Users.Where(u => u.CompanyID == CompanyID && u.UserID == UserID && u.IsActive == true).Select(table => table.InternalMailID).FirstOrDefault();
                }
            }
            //return _FromMailID = _context.Session["FromMailID"].ToString();   // localhost
            // Server
            return _FromMailID = db.Users.Where(u => u.CompanyID == CompanyID && u.UserID == UserID && u.IsActive == true).Select(table => table.InternalMailID).FirstOrDefault();
        } 
    }

    public string FromPassword
    {
        get
        {
            try
            {
                if (_context != null && _context.Session != null)
                {
                    if (_context.Session["FromPassword"] == null)
                    {
                        _context.Session["FromPassword"] = db.Users.Where(u => u.CompanyID == CompanyID && u.UserID == UserID && u.IsActive == true).Select(table => table.InternalMailIDPassword).FirstOrDefault();
                    }
                }
                _FromPassword = _context.Session["FromPassword"].ToString();
            }
            catch (Exception ex)
            {
                //ErrorSignal.FromCurrentContext().Raise(ex); //ELMAH Signaling
                IsSuccess = false;
                ViewBag.ErrorMessage = "Transaction failed. Please try again. If problem persists, contact system administrator.";
                ViewBag.InfoMessage = "";
                // Handle exception here and Log Error to text file...
                string sLogFilePath = Server.MapPath(@"~/ApplicationFiles/ErrorLog/ErrorLogFile.txt");
                GeneralRepository.LogErrorToFile(ex, sLogFilePath, "FromPassword from session manager");
            }
            return _FromPassword;
        }
    }

    public string CompanyName
    {
        get
        {
            if (_context != null && _context.Session != null)
            {
                if (_context.Session["CompanyName"] == null)
                {
                    _context.Session["CompanyName"] = db.CompanyMasters.Where(u => u.CompanyID == CompanyID && u.IsActive == true).Select(table => table.CompanyName).FirstOrDefault();
                }
            }
            return _CompanyName = _context.Session["CompanyName"].ToString();
        }
    }

    public string AdminEmailID
    {
        get
        {
            if (_context != null && _context.Session != null)
            {
                if (_context.Session["AdminEmailID"] == null)
                {
                    _context.Session["AdminEmailID"] = (from table in db.Users
                                               where table.RoleID == ADMIN_ROLE
                                               && table.IsActive == true
                                               && table.UserName.ToUpper() == "ADMIN"
                                               select table.EmailID).FirstOrDefault();
                }
            }
            return _AdminEmailID = _context.Session["AdminEmailID"].ToString();
        }
    }

}

我在我的控制器动作方法中调用这个类......

System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
        SessionManager objSessionManager = new SessionManager(currentContext);
StringBuilder MsgBody = MakeMailBodyForReportCreated(objReportTransactionData);
        EmailRepository.SendEmail(objSessionManager.FromMailID, objSessionManager.FromPassword, ToMailAddress, CCMailAddress, MsgSubject, MsgBody.ToString());

这是我的web.config文件

<sessionState mode="InProc" timeout="20"></sessionState>
  <pages validateRequest="false" enableSessionState="true" />
<modules>


  <remove name="Session" />
  <add name="Session" type="System.Web.SessionState.SessionStateModule" />

</modules>

请帮忙......

0 个答案:

没有答案