1

I currently checking what needs to be done to migrate a dotnet core In-process azure function v3 to a dotnet coe Out-of-process/.NET Isolated azure function.

In my project the IHttpContextAccessor is used to get the HttpContext in some places. I tried to get the IHttpContextAccessor in the function, but it is null.

Is it possible to get the IHttpContextAccessor in Azure Function Isolated or do I need to change the code to no longer use it?

            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureServices(s =>
                {
                    s.AddHttpContextAccessor();
                })
                .Build();
        private readonly IHttpContextAccessor httpContextAccessor;

        public Function1(IHttpContextAccessor httpContextAccessor)
        {
            this.httpContextAccessor = httpContextAccessor;
        }

        [Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
            FunctionContext executionContext)
        {
            var logger = executionContext.GetLogger("Function1");
            logger.LogInformation(httpContextAccessor.HttpContext.Request.Scheme);
sschoof
  • 1,531
  • 1
  • 17
  • 24
  • You can refer to [Dotnet-Isolated Azure Functions - how to access HttpContext](https://stackoverflow.com/questions/68993844/dotnet-isolated-azure-functions-how-to-access-httpcontext), [Are you registering IHttpContextAccessor correctly?](https://adamstorr.azurewebsites.net/blog/are-you-registering-ihttpcontextaccessor-correctly), [IFunctionContextAccessor Implementation](https://gist.github.com/dolphinspired/796d26ebe1237b78ee04a3bff0620ea0) and [Access HttpContext in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0) – Ecstasy Nov 04 '21 at 09:32
  • 1
    You can refer to [HttpContext property of HttpContextAccessor is null](https://github.com/dotnet/aspnetcore/issues/5144) and also open an issue on GitHub: [azure-functions-dotnet-worker](https://github.com/Azure/azure-functions-dotnet-worker/issues) – Ecstasy Nov 04 '21 at 09:39
  • 1
    There is already a issue: https://github.com/Azure/azure-functions-dotnet-worker/issues/572 – sschoof Nov 04 '21 at 10:50

0 Answers0