Search code examples
c#asp.net-core.net-6.0blazor-webassemblymediatr

HttpClient configured in Program.cs is not being passed to MediatR RequestHandler by dependency injection container


I'm working on a Blazor WebAssembly application in .NET 6.0.

I'm using MediatR requests and handlers.

public class DummyRequest : IRequest<string>
{
    public Guid Test { get; } = new Guid("e9f41a5d-5da6-4aad-b118-83476b7f40f4");
}


public class DummyHandler : IRequestHandler<DummyRequest, string>
{
    private readonly HttpClient _httpClient;

    public DummyHandler(HttpClient httpClient)
    {
        _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
    }

    public async Task<string> Handle(DummyRequest request, CancellationToken cancellationToken)
    {
      // This should be the value configured in Program.cs
        string baseAddress = _httpClient.BaseAddress?.AbsoluteUri ?? string.Empty;
        // But it's always blank, so we can't make any calls with the HttpClient

        await Task.CompletedTask;
        return "foobar";
    }
}

I'm configuring a different HttpClient for each request handler in Program.cs, then I'm adding MediatR:

builder.Services.AddHttpClient<DummyHandler>((client) => { client.BaseAddress = new Uri("https://api.somewhere.com"); });
builder.Services.AddMediatR(Assembly.GetExecutingAssembly());

I have also tried reversing those calls, so that I add MediatR first, and register the HttpClient for the DummyHandler type afterwards.

At runtime, after that Handler has been instantiated, it should have an _httpClient with a BaseAddress property set to "https://api.somewhere.com".

However, it always gets an HttpClient with a null BaseUri, so the Handler can't use the HttpClient in any operations.

Can anybody see what's gone wrong please?


Solution

  • It seems that MediatR registers interface-implemetation pair so you need to follow the same pattern for the typed client registration. Try the following:

    services.AddHttpClient<IRequestHandler<DummyRequest, string>, DummyHandler>((client) => { client.BaseAddress = new Uri("https://api.somewhere.com"); });
    

    Gist with full test code.