Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to configure HttpClient base address in Blazor Server using IHttpClientFactory #25758

Closed
mihaimyh opened this issue Sep 10, 2020 · 8 comments
Assignees
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation feature-blazor-server Priority:1 Work that is critical for the release, but we could probably ship without severity-major This label is used by an internal tool
Milestone

Comments

@mihaimyh
Copy link

mihaimyh commented Sep 10, 2020

I am trying to configure HttpClient's base address in a Blazor Server using IHttpClientFactory but I am getting a runtime exception:

services.AddHttpClient("ApiClient", (provider, client) =>
{
    var uriHelper = provider.GetRequiredService<NavigationManager>();
    client.BaseAddress = new Uri(uriHelper.BaseUri);
});

Create an instance of this HttpClient using IHttpClientFactory and the following exception will be thrown:

_httpClient = httpClientFactory.CreateClient("ApiClient");

System.InvalidOperationException: 'Cannot resolve scoped service 'Microsoft.AspNetCore.Components.NavigationManager' from root provider.'

image

NET SDK (reflecting any global.json):
 Version:   5.0.100-preview.8.20417.9
 Commit:    fc62663a35

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-preview.8.20417.9\

Host (useful for support):
  Version: 5.0.0-preview.8.20407.11
  Commit:  bf456654f9

.NET SDKs installed:
  3.1.401 [C:\Program Files\dotnet\sdk]
  5.0.100-preview.8.20417.9 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-preview.8.20414.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-preview.8.20407.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.8.20411.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Anyone knows what might be the issue here?

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Sep 10, 2020
@mihaimyh
Copy link
Author

Tested it with both 3.1 and 5.0-preview8, same result.

@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Sep 10, 2020
@ghost
Copy link

ghost commented Sep 10, 2020

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@mkArtakMSFT
Copy link
Member

Thanks for contacting us.
We will investigate how this can be resolved in the future.

@SteveSandersonMS SteveSandersonMS added affected-few This issue impacts only small number of customers enhancement This issue represents an ask for new feature or an enhancement to an existing one severity-major This label is used by an internal tool labels Oct 7, 2020 — with ASP.NET Core Issue Ranking
@aredfox
Copy link

aredfox commented Oct 21, 2020

Is there a workaround, or other solution meanwhile that can be promoted whilst it is in process of enhancement @SteveSandersonMS / @mkArtakMSFT? Thanks!

@arivera12
Copy link

I would just simply use the default http client factory services.AddHttpClient();

I did this kind of overriding the base http client address and it was a bad idea once when I started with blazor wasm and server.

I falled back to just include the HttpClient using it's default base root address where the web server is running.

What happens if you want to get a resource which is on the wwwroot of your blazor server/wasm application?

You will lose this main address in your http client...

If you are on native application then this would make sense at all to do it.

I would suggest to change approach and make a HttpClientService per specific API controllers instead and inside set up you base http client address.

I know this doesn't answer the asked question but it's my experience based on this when I did this approach before this should not be done at all.

I even think it should be considered a bad practice at all.

@agilenut
Copy link

I would also like to see this addressed. I need to be able to add a user's access token to a named HTTP client. But I'm getting the same exception when I try to resolve the AuthenticationStateProvider to figure who the user is. This should be a supported scenario.

@mkArtakMSFT mkArtakMSFT added Priority:1 Work that is critical for the release, but we could probably ship without triaged labels Nov 5, 2021
@mkArtakMSFT mkArtakMSFT modified the milestones: Backlog, .NET 7 Planning Nov 30, 2021
@ghost
Copy link

ghost commented Nov 30, 2021

Thanks for contacting us.
We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Feb 21, 2022

When fixing this, verify that it also fixes #40336

@mkArtakMSFT mkArtakMSFT added Docs This issue tracks updating documentation and removed enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Oct 20, 2022
@mkArtakMSFT mkArtakMSFT modified the milestones: .NET 8 Planning, 8.0-MQ Oct 20, 2022
@mkArtakMSFT mkArtakMSFT added enhancement This issue represents an ask for new feature or an enhancement to an existing one and removed enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Dec 14, 2022
@mkArtakMSFT mkArtakMSFT changed the title How to configure HttpClient base address in Blazor Server using IHttpClientFactory Document how to configure HttpClient base address in Blazor Server using IHttpClientFactory Dec 14, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-few This issue impacts only small number of customers area-blazor Includes: Blazor, Razor Components Docs This issue tracks updating documentation feature-blazor-server Priority:1 Work that is critical for the release, but we could probably ship without severity-major This label is used by an internal tool
Projects
None yet
Development

No branches or pull requests

8 participants