Cannot get HttpContext to work in .Net Core 3.1 Console Application

I want to use HttpContext in my .NET Core 3.1 Console App, but it is always null. This is what my code looks like:

internal class Startup
{
    public static IHttpContextAccessor _httpContextAccessor;

    internal Startup(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public static void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpContextAccessor();
    }

    private static void Main(string[] args)
    {
        var serviceCollection = new ServiceCollection();
        ConfigureServices(serviceCollection);

        var context = _httpContextAccessor.HttpContext;

    }

context is always null. Can anyone offer advice? Thanks



Read more here: https://stackoverflow.com/questions/64399448/cannot-get-httpcontext-to-work-in-net-core-3-1-console-application

Content Attribution

This content was originally published by Rich at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: