Giter Club home page Giter Club logo

Comments (11)

patrick-elmore avatar patrick-elmore commented on July 16, 2024 2

We found a workaround for this that may work for others. It involves a very minimal custom middleware component:

public class HttpContextAccessorMiddleware(IHttpContextAccessor httpContextAccessor) : IFunctionsWorkerMiddleware
{
    public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
    {
        httpContextAccessor.HttpContext = context.GetHttpContext();
        await next(context);
    }
}

Then wire it up in Program.cs

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication(builder =>
    {
        builder.UseMiddleware<HttpContextAccessorMiddleware>();
    })

This should allow you to inject IHttpContextAccessor as you did prior to moving to isolated. I have only confirmed this on .NET 6, but I assume it will work in 8 as well.

from azure-functions-dotnet-worker.

soonwong avatar soonwong commented on July 16, 2024 2

We are using the above middleware pattern in our .net8 out-of-process functions. However it does not appear to be 100% parity with in-proc.

I'm doing the following to get access to Application Insights request telemetry so that we can insert custom properties. HttpContext is available but this returns NULL still.

var telemetry = _httpContextAccessor.HttpContext?.Features.Get<RequestTelemetry>();

I'm aware that this isn't quite on point; just be aware that the above middleware suggestion may not solve everything.

from azure-functions-dotnet-worker.

MarkMilfordMe avatar MarkMilfordMe commented on July 16, 2024 1

Yes this works with the middleware in .NET 8

from azure-functions-dotnet-worker.

rhythmnewt avatar rhythmnewt commented on July 16, 2024

Came across same issue, after migrating from .net 6 to .net 8 the IHttpContextAccessor is null when retrieved from context.

image

from azure-functions-dotnet-worker.

Tech-Sohail avatar Tech-Sohail commented on July 16, 2024

I am also facing the same issue when I am trying to upgrade my projects from Net6 to Net8.
I can initialize HttpContextAccessor by using services.AddHttpContextAccessor()
But then HttpContextAccessor.HttpContext is null.

Any idea when this issue is going to be fixed?

Note: We didn't need to initialize HttpContextAccessor in Net6. It is automatically getting initialized during each function endpoint call.

from azure-functions-dotnet-worker.

cosminvladutu avatar cosminvladutu commented on July 16, 2024

Same issue like @Tech-Sohail just described, here also :(

As far as I understand, the problem is that I am not using an HTTPTrigger. If you are using an Event trigger or a Timer trigger, you won't have a context... IHttpContextAccessor and HttpContext are not available in these cases. It seems the idea is that if you have information in the context, that info should be in the event itself.

from azure-functions-dotnet-worker.

MarkMilfordMe avatar MarkMilfordMe commented on July 16, 2024

Same issue like @Tech-Sohail just described, here also :(

As far as I understand, the problem is that I am not using an HTTPTrigger. If you are using an Event trigger or a Timer trigger, you won't have a context... IHttpContextAccessor and HttpContext are not available in these cases. It seems the idea is that if you have information in the context, that info should be in the event itself.

No, this issue is purely about the lack of HTTP Context on a http trigger, I would never expect to have a http trigger on event or Timer, in .NET 6 we uses Queue triggers extensively but there is no http context in those circumstances and that is expected behaviour.

Still waiting on anything from MS on this, so far seems to just have been added and removed from various labels, I rechecked the latest nuget packages recently (see sample project) and its still ongoing to latest releases

from azure-functions-dotnet-worker.

patrick-elmore avatar patrick-elmore commented on July 16, 2024

@MarkMilfordMe I don't think that this is a .NET 8 vs .NET 6 issue. It appears to be an in-process vs out-of-process function issue. Your .NET 6 demo project is in-process and your .NET 8 demo project is out-of-process. If you were to change your .NET 6 project to out-of-process, I suspect you would see the same behavior. We are currently seeing the same thing when doing a straight in-process to out-of-process migration in a .NET 6 function application.

from azure-functions-dotnet-worker.

MarkMilfordMe avatar MarkMilfordMe commented on July 16, 2024

@MarkMilfordMe I don't think that this is a .NET 8 vs .NET 6 issue. It appears to be an in-process vs out-of-process function issue. Your .NET 6 demo project is in-process and your .NET 8 demo project is out-of-process. If you were to change your .NET 6 project to out-of-process, I suspect you would see the same behavior. We are currently seeing the same thing when doing a straight in-process to out-of-process migration in a .NET 6 function application.

Thanks, I'll try that, guess still means we can't update to .NET 8 though, as .NET 8 doesn't support in-process to my knowledge.

from azure-functions-dotnet-worker.

patrick-elmore avatar patrick-elmore commented on July 16, 2024

@MarkMilfordMe I don't think that this is a .NET 8 vs .NET 6 issue. It appears to be an in-process vs out-of-process function issue. Your .NET 6 demo project is in-process and your .NET 8 demo project is out-of-process. If you were to change your .NET 6 project to out-of-process, I suspect you would see the same behavior. We are currently seeing the same thing when doing a straight in-process to out-of-process migration in a .NET 6 function application.

Thanks, I'll try that, guess still means we can't update to .NET 8 though, as .NET 8 doesn't support in-process to my knowledge.

You are correct. .NET 8 is not supported for in-proc functions yet. There is a tracking issue that indicates that support for Windows functions should be rolling out sometime this month (June).

from azure-functions-dotnet-worker.

MarkMilfordMe avatar MarkMilfordMe commented on July 16, 2024

@MarkMilfordMe I don't think that this is a .NET 8 vs .NET 6 issue. It appears to be an in-process vs out-of-process function issue. Your .NET 6 demo project is in-process and your .NET 8 demo project is out-of-process. If you were to change your .NET 6 project to out-of-process, I suspect you would see the same behavior. We are currently seeing the same thing when doing a straight in-process to out-of-process migration in a .NET 6 function application.

Thanks, I'll try that, guess still means we can't update to .NET 8 though, as .NET 8 doesn't support in-process to my knowledge.

You are correct. .NET 8 is not supported for in-proc functions yet. There is a tracking issue that indicates that support for Windows functions should be rolling out sometime this month (June).

So just did a quick example (added to original repo) and yes it fails on .net 6 out of proc. Will cross fingers that In-Proc .NET 8 rolls out in plenty of time for us to transistion before november when .net 6 is out of support! Will subscribe to the issue you mentioned. Thanks

from azure-functions-dotnet-worker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.