Skip to content
This repository was archived by the owner on Jul 9, 2024. It is now read-only.
This repository was archived by the owner on Jul 9, 2024. It is now read-only.

Blazor WASM client unable to send web requests through HttpClient BaseAddress #109

@gridlocdev

Description

@gridlocdev

This issue occurs for Blazor WASM applications consuming an OpenAPI spec that does not supply a base URL. Blazor WASM does not support the System.Net.Http.HttpClientHandler class, so those apps must supply an HttpClient to the HttpClientRequestAdapter to work.

The issue is that the BaseAddress property on the supplied HttpClient object does not infer that as the HttpClientRequestAdapter's BaseUrl, and it must be set separately for requests to be sent appropriately.

How to reproduce:

  1. Create a Blazor WASM application with .NET 7 using the empty template (For the sake of example, named "KiotaTestApp.FrontEnd")

  2. Generate a client from an API spec that does not supply a Base URL. E.g. (kiota generate -d http://<url>/swagger/v1/swagger.json -n KiotaTestApp.FrontEnd.Client -o ./Client -l CSharp -c ApiClient

    Note: The dotnet-aspnet-codegenerator tool can be used to scaffold an API with an OpenAPI spec that reproduces the above scenario.

  3. Note the following warning that appears in the console output:

    warn: Kiota.Builder.KiotaBuilder[0]
          OpenAPI warning: #/ - A servers entry (v3) or host + basePath + schemes properties (v2) was not present in the OpenAPI description. The root URL will need to be set manually with the request adapter.
    warn: Kiota.Builder.KiotaBuilder[0]
          No server url found in the OpenAPI document. The base url will need to be set when using the client.
    
  4. Add the Kiota API client service setup in a Blazor WASM application's Program.cs file like so:

    // ...
    var authProvider = new AnonymousAuthenticationProvider();
    var client = new HttpClient { BaseAddress = new Uri("http://<url>") };
    var requestAdapter = new HttpClientRequestAdapter(authProvider, httpClient: client );
    builder.Services.AddScoped<ApiClient>(_ => new ApiClient(requestAdapter));
    // ...
  5. In the Index.razor (or any other page) inject the client and create a method that sends an HTTP request with it

  6. Launch the application, and view that requests are not sent to the HttpClient's BaseAddress

Workaround

Explicitly setting the BaseUrl after adding an HttpClient applies the parameter value, allowing the client to send requests.

// ...
var authProvider = new AnonymousAuthenticationProvider();
var client = new HttpClient { BaseAddress = new Uri("http://<url>") };
var requestAdapter = new HttpClientRequestAdapter(authProvider, httpClient: client );
requestAdapter.BaseUrl = "http://<url>";
builder.Services.AddScoped<ApiClient>(_ => new ApiClient(requestAdapter));
// ...

Metadata

Metadata

Assignees

Labels

CSharpStandard GitHub labelIssue caused by core project dependency modules or libraryenhancementNew feature or request

Type

No type

Projects

Status

Done ✔️

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions