Closed mdzieg closed 1 week ago
Using CircuitServicesAccessor
link does not work for me. I get null on services collection.
Details:
It works when I first open the main app page (or any other page) and then navigate to the page which results in using HttpClient. But when I navigate to the desired page directly from the address bar, then I do not get services in circuit accessor even though ServicesAccessorCircuitHandler
is called on each request. ServicesAccessorCircuitHandler
gets called after DelegatingHandler
.
public class CircuitServicesAccessor
{
static readonly AsyncLocal<IServiceProvider> blazorServices = new();
public IServiceProvider? Services
{
get => blazorServices.Value;
set => blazorServices.Value = value;
}
}
public class ServicesAccessorCircuitHandler : CircuitHandler
{
readonly IServiceProvider services;
readonly CircuitServicesAccessor circuitServicesAccessor;
public ServicesAccessorCircuitHandler(IServiceProvider services,
CircuitServicesAccessor servicesAccessor)
{
this.services = services;
this.circuitServicesAccessor = servicesAccessor;
}
public override Func<CircuitInboundActivityContext, Task> CreateInboundActivityHandler(
Func<CircuitInboundActivityContext, Task> next)
{
return async context =>
{
circuitServicesAccessor.Services = services;
await next(context);
circuitServicesAccessor.Services = null;
};
}
}
I added repro project here: https://github.com/mdzieg/blazor_circuit_accessor It reflects the way we initialize blazor app in our project. README contains repro steps;
@mdzieg seems that you aren't using Blazor web.
I suspect you need to setup the circuit accessor in these two callbacks too
@javiercn I added two more overrides and tested the app. Although breakpoints in those extra methods are hit before my component code is executed, the context value is not present. The Order of execution when opening /counter page is the following:
Thank you, @MackinnonBuck! Will it be included only in net9 or also in net8?
@mdzieg we are unlikely to port this to 8.0 since only affects the old hosting model based on MVC, unless we receive significant feedback otherwise.
We recommend moving to Blazor Web
with InteractiveServer
components which essentially serves the same purpose.
Is there an existing issue for this?
Describe the bug
I have a problem with getting request scope in blazor server app.
I use
HttpClientFactory
to communitace with one of my services. I wanted to inject dynamic header into each request. To do so I created customDelegatingHandler
. BecauseHttpClient
handlers are created in a separate scope (https://andrewlock.net/understanding-scopes-with-ihttpclientfactory-message-handlers/) I took the approach with injectingIHttpContextAccessor
.Here is draft of the handler:
IMyProvider
(registered as scoped service in DI) instance has some data set during request processing. I need to access that data.The problem is that the instance I get from
IHttpContextAccessor
fromMyHeaderHandler
is different from the instance I get in blazor components or other services used there directly (not viaDelegatingHandler
).I tried to set SuppressHandlerScope option to true, but no luck:
AFAIK this approach works in WebApi projects and the problem is with Blazor apps. To me it looks like a BUG. If this method of getting request scope is not correct, I would be more than happy to get some advice here.
Expected Behavior
IHttpContextAccessor.HttpContext.RequestServices gives access to request scope.
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
8.0.304
Anything else?
No response