Skip to main content

Posts

Showing posts with the label .NET 8

Fixing "HttpContext.Current is null" in .NET 8 Migrations

  One of the most immediate and widespread failures developers encounter when porting legacy ASP.NET 4.x applications to .NET 8 is the disappearance of   System.Web . Specifically, the ubiquitous   HttpContext.Current   property. In legacy enterprise codebases, this static property served as a global service locator for the request lifecycle, accessed arbitrarily deep within Business Logic Layers (BLL) and static helper classes. In .NET 8, compiling legacy logic often results in referencing  Microsoft.AspNetCore.Http , only to find that while the types look similar, there is no static  Current  property. If you attempt to access context via a static property without the correct infrastructure, you will encounter  NullReferenceException  or logic errors where user identity is lost across  await  boundaries. The Root Cause: ThreadStatic vs. AsyncLocal The fundamental issue isn't just that the API changed; the underlying threading mode...

Migrating ASP.NET to .NET 8: Handling HttpContext.Current with System.Web Adapters

  The most formidable barrier to migrating legacy ASP.NET MVC applications to .NET 8 is not the syntax changes or the project file format; it is the architectural dependency on   System.Web.dll . Specifically, enterprise applications built between 2010 and 2018 often treat  HttpContext.Current  as a global singleton, accessing request data, session state, and user identity deep within business logic layers, static helper methods, and repositories. When you target .NET 8,  System.Web  disappears. The modern  Microsoft.AspNetCore.Http.HttpContext  is designed to be injected via Dependency Injection (DI), not accessed statically. Rewriting hundreds of thousands of lines of code to thread  IHttpContextAccessor  through every method signature is rarely feasible for an initial migration. This post details how to implement the  System.Web Adapters , a library maintained by Microsoft that creates a compatibility layer, allowing you to run l...