Giter Club home page Giter Club logo

Comments (11)

petyusa avatar petyusa commented on August 29, 2024 1

Thanks, the workaround helped. Hope it will be fixed soon.

from fluxor.

mrpmorris avatar mrpmorris commented on August 29, 2024

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.

mrpmorris avatar mrpmorris commented on August 29, 2024

Could you try registering your handler as Transient, does that solve the problem?

from fluxor.

petyusa avatar petyusa commented on August 29, 2024

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.

mrpmorris avatar mrpmorris commented on August 29, 2024

Ah yes of course, thanks!

from fluxor.

mrpmorris avatar mrpmorris commented on August 29, 2024

What class type do you receive for IHttpClientFactory? Can you use your original code and then register that class as Scoped?

from fluxor.

mrpmorris avatar mrpmorris commented on August 29, 2024

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.

petyusa avatar petyusa commented on August 29, 2024

Yes, I've just checked, and it's a DefaultHttpClientFactory.

from fluxor.

mrpmorris avatar mrpmorris commented on August 29, 2024

@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.

petyusa avatar petyusa commented on August 29, 2024

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.

mrpmorris avatar mrpmorris commented on August 29, 2024

Let's leave it then. MS has a bug to fix.

from fluxor.

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.