Referencing ASP.NET framework 4.6.2 in ASP.NET Core, Which use System.Web.HttpContext.Current

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

c#
asp.net
.net-core
asked on Stack Overflow Jan 3, 2019 by Amin Ulhaq • edited Jan 4, 2019 by NineBerry

1 Answer

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.

answered on Stack Overflow Jan 3, 2019 by Manoj Choudhari

User contributions licensed under CC BY-SA 3.0