0

I have a class in ASP.Net framework 4.6 SessionHelper Code of class is as follow

using System;
using System.Web;
using MyPortal.Common.Entity;
using MyPortal.Common.Helper;

namespace MyPortal.BusinessLayer.Helper
{
    public static class SessionHelper
    {
        public static User GetLoggedInUser { get { return (User) GetFromSession(User.SESSION_LOGGEDIN_USER); } }
        public static User GetLoggedInExternalUser { get { return (User)GetFromSession(User.SESSION_LOGGEDIN_EXTERNAL_USER); } }

        public static string GetValueFromSession(string sessionKey)
        {
            return HttpContext.Current.Session[sessionKey] == null ? string.Empty : HttpContext.Current.Session[sessionKey].ToString();
        }

        public static void SaveInSession(string sessionKey, object sessionValue)
        {
            HttpContext.Current.Session[sessionKey] = sessionValue;
        }

        public static void RemoveSession(string sessionKey)
        {
            if (HttpContext.Current.Session[sessionKey]!=null)
                HttpContext.Current.Session.Remove(sessionKey);
        }

    }
}

Now This SessionHelper class is used in many places MyPortal.BusinessLayer, this project or dll is running fine in a ASP.NET web project.

Now i have to use this BusinessLayer.dll in ASP.NET Core project, and want to access some of the methods, like getting loggedInUserBalance these methods in BusinessLayer.dll are using SessionHelper which itself using HttpContext.Current.Session

Now I am getting error in ASP.NET Core

System.TypeLoadException HResult=0x80131522 Message=Could not load type 'System.Web.HttpContext' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
Amin Ulhaq
  • 13
  • 2
  • 4

1 Answers1

0

The ideal way is to use IHttpContextAccessor.

In your startup you can write a code like this:

public void ConfigureServices(IServiceCollection services)
{
     services.AddMvc()
         .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
     services.AddHttpContextAccessor();
     services.AddTransient<IUserRepository, UserRepository>();
}

Then whereever you want to use HttpContext, you can inject IHttpContextAccesor as shown in below example:

public class UserRepository : IUserRepository
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public UserRepository(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public void LogCurrentUser()
    {
        var username = _httpContextAccessor.HttpContext.User.Identity.Name;
        service.LogAccessRequest(username);
    }
}

This documentation article explains about accessing httpcontext from various places.

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
  • The issue is I am using DLL which is build and compiled in asp.net framework 4.6 in ASP.NET Core 2.2, and that DLL used System.Web.HttpContext.Current.Session – Amin Ulhaq Jan 03 '19 at 15:31
  • let me more specific I have a class in ASP.Net framework 4.6 SessionHelper Code of class is as follow – Amin Ulhaq Jan 04 '19 at 10:17
  • using System; using System.Web; using MyPortal.Common.Entity; using MyPortal.Common.Helper; namespace MyPortal.BusinessLayer.Helper { public static class SessionHelper { public static string GetValueFromSession(string sessionKey) { return HttpContext.Current.Session[sessionKey] == null ? string.Empty : HttpContext.Current.Session[sessionKey].ToString(); } public static void SaveInSession(string sessionKey, object sessionValue) { HttpContext.Current.Session[sessionKey] = sessionValue; } } } – Amin Ulhaq Jan 04 '19 at 10:19
  • System.Web is deprecated in asp.net core. None of your System.Web pipeline will execute and hence you will not get the HttpContext set properly. One of the possible solution is to remove dependency of your method from httpcontext class. You can pass the HttpContext.Current.Session[sessionKey] value directly to the method as parameter. That way you will be able to reuse your logic in asp.net core application as well. – Manoj Choudhari Jan 04 '19 at 10:26