Migration to .NET 5

Gaurav Bahuguna 1 Reputation point
2021-06-14T11:07:53.857+00:00

I am trying to migrate a 4.6.1 .NET framework to .NET 5 but below code is giving error -
Error CS0117 'HttpContext' does not contain a definition for 'Current'

using Disa.Core.Containers;
using System.Web;

namespace Disa.Core.Logging
{
public static class ServiceLocator
{
public static void Set(IContainer container)
{
HttpContext.Current.Items.Add("Container", container);
}

    public static IContainer Get()
    {
        return (IContainer)HttpContext.Current.Items["__Container__"];
    }
}

}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,201 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,141 Reputation points
    2021-06-14T12:13:25.497+00:00

    The ASP.NET 5 context works much differently than ASP.NET 4.6.1. HttpContext is accessed through the IHttpContextAccessor interface using dependency injection. Keep in mind, this information is openly published in the .NET 5 fundamental documentation. Below is an example that uses standard constructor injection loosely based on your code example..

        public interface IContainer  
        {  
            string Name { get; set; }  
        }  
      
        public class Container : IContainer  
        {  
            public string Name { get; set; }  
        }  
      
        public interface IServiceLocator  
        {  
            IContainer Get();  
            void Set(IContainer container);  
        }  
      
      
        public class ServiceLocator : IServiceLocator  
        {  
            private readonly IHttpContextAccessor _httpContextAccessor;  
            public ServiceLocator(IHttpContextAccessor httpContextAccessor)  
            {  
                _httpContextAccessor = httpContextAccessor;  
            }  
            public void Set(IContainer container)  
            {  
                _httpContextAccessor.HttpContext.Items.Add("__Container__", container);  
            }  
      
            public IContainer Get()  
            {  
                return (IContainer)_httpContextAccessor.HttpContext.Items["__Container__"];  
            }  
        }  
    

    Register the service in the startup.cs file

    services.AddScoped<IServiceLocator, ServiceLocator>();  
    

    Constructor injection and implementation

        public class DiDemoController : Controller  
        {  
            public readonly IServiceLocator _service;  
            public DiDemoController(IServiceLocator service)  
            {  
                _service = service;  
            }  
            public IActionResult Index()  
            {  
                Container container = new Container() { Name = "Hello World" };  
                _service.Set(container);  
      
                ViewBag.Name = _service.Get().Name;  
                return View();  
            }  
        }  
    
      
    @{  
        ViewData["Title"] = "Index";  
    }  
      
    <h1>Index</h1>  
      
    <div>@(ViewBag.Name ?? "Empty") </div>  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 56,846 Reputation points
    2021-06-14T14:29:06.287+00:00

    For performance reasons the static .HttpContext.Current methods nor any of the other ,Current methods ported to asp.net core.

    This means you must pass an HttpContext to the static methods as a parameter. I found this one of the main porting challenge. In addition the use of static methods to access config settings.

    For porting I created a static library for settings, and reworked code that called HttpContext.Current., but using an object property rather than a static.

    1 person found this answer helpful.
    0 comments No comments