Comments (11)
Thanks, the workaround helped. Hope it will be fixed soon.
from fluxor.
Hi
It's actually a bug in .NET that I reported recently here dotnet/aspnetcore#23847
I'd recommend having your own IHttpClientFactory service for creating HttpClient objects so you can inject your auth token into the header.
Make sure you remember to Dispose the HttpClient instance when you've finished with it.
from fluxor.
Could you try registering your handler as Transient, does that solve the problem?
from fluxor.
That was my first thought, I've already tried it, as I mentioned it in my question. Now I tried it again to be sure, but accessToken is still null (if I inject the state in my service, it works fine).
I post my code here:
DelegatingHandler:
public class AuthenticationHandler : DelegatingHandler
{
private readonly IState<AuthState> _authState;
public AuthenticationHandler(IState<AuthState> authState)
{
_authState = authState;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var accessToken = _authState.Value.AccessToken;
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return base.SendAsync(request, cancellationToken);
}
}
Registration:
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddTransient<AuthenticationHandler>();
builder.Services
.AddHttpClient("AuthenticatedClient", client =>
{
client.BaseAddress = new Uri("https://localhost:45299/");
})
.AddHttpMessageHandler<AuthenticationHandler>();
var currentAssembly = typeof(Program).Assembly;
builder.Services.AddFluxor(options => options.ScanAssemblies(currentAssembly));
await builder.Build().RunAsync();
}
And here's how I use it in a service:
protected HttpClient AuthenticatedClient
{
get
{
var client = HttpClientFactory.CreateClient("AuthenticatedClient");
return client;
}
}
from fluxor.
Ah yes of course, thanks!
from fluxor.
What class type do you receive for IHttpClientFactory? Can you use your original code and then register that class as Scoped?
from fluxor.
The default HttpClientFactory is an internal class, so that's not very helpful. I've reported another bug here https://github.com/dotnet/extensions/issues/3352
from fluxor.
Yes, I've just checked, and it's a DefaultHttpClientFactory.
from fluxor.
@petyusa As an experiment, would you try this?
var type = typeof(IHttpClientFactory).Assembly.DefinedTypes.Single(t => t.Name.Contains("DefaultHttpClientFactory"));
services.AddScoped(typeof(IHttpClientFactory), type);
from fluxor.
Should I just simply add these lines? I have to use AddHttpClient to be able to add a message handler, doesn't it register another IHttpClientFactory?
By the way, I added these lines, and nothing changed.
from fluxor.
Let's leave it then. MS has a bug to fix.
from fluxor.
Related Issues (20)
- Subscribing external events HOT 2
- Blazor server app in used Redux(Fluxor) in asp .net core error Unable to resolve service for type 'Fluxor.IFeature` while attempting to activate 'Fluxor.State` HOT 2
- Dispatch from Polly HOT 4
- Dispatching action in effect - running orders HOT 1
- Fluxor does not seem to work with the new .NET 8 Blazor Web App template HOT 2
- Deadlock when dispatching from store/feature events HOT 3
- Reducer not getting invoked in .NET 8 Blazor HOT 4
- Test case for dispatcher failure in .NET8 with new render modes HOT 6
- 🤑 Add a sponsor button
- Fluxor not updating UI after state change HOT 4
- Getting DisposableCallback after app has been running for a while HOT 5
- .NET MAUI Hybrid App Demo HOT 2
- Enabling Fluxor for lazy-loaded assemblies HOT 3
- Blazor dot net 8 server interactive component does not re-render when state changes via redux tools time travel HOT 1
- GoAction using relative path doesn't work anymore (LocationChangeException) HOT 2
- Can't Upgrade to v6.0.0 HOT 4
- Having troubles with Fluxor in Blazor auto HOT 1
- Guidance on AsyncDisposable in 6.0.0 HOT 4
- How should one deal with multiple of the same state on the same page? HOT 2
- ReduxDevTools throwing exception when initial state contains Double.NaN
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fluxor.