Skip to content

[Blazor][Wasm] Move HttpClient from default services to Program.Main #19119

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ private void InitializeDefaultServices()
Services.AddSingleton<INavigationInterception>(WebAssemblyNavigationInterception.Instance);
Services.AddSingleton<ILoggerFactory, WebAssemblyLoggerFactory>();
Services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(WebAssemblyConsoleLogger<>)));
Services.AddSingleton<HttpClient>(s =>
{
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
var navigationManager = s.GetRequiredService<NavigationManager>();
return new HttpClient
{
BaseAddress = new Uri(navigationManager.BaseUri)
};
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Net.Http;
using Microsoft.AspNetCore.Components;

namespace Microsoft.Extensions.DependencyInjection
{
public static class HttpClientServiceCollectionExtensions
{
/// <summary>
/// Adds a <see cref="HttpClient" /> instance to the <paramref name="serviceCollection" /> that is
/// configured to use the application's base address (<seealso cref="NavigationManager.BaseUri" />).
/// </summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection" />.</param>
/// <returns>The configured <see cref="IServiceCollection" />.</returns>
public static IServiceCollection AddBaseAddressHttpClient(this IServiceCollection serviceCollection)
{
return serviceCollection.AddSingleton(s =>
{
// Creating the URI helper needs to wait until the JS Runtime is initialized, so defer it.
var navigationManager = s.GetRequiredService<NavigationManager>();
return new HttpClient
{
BaseAddress = new Uri(navigationManager.BaseUri)
};
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ private static IReadOnlyList<Type> DefaultServiceTypes
typeof(NavigationManager),
typeof(INavigationInterception),
typeof(ILoggerFactory),
typeof(HttpClient),
typeof(ILogger<>),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace StandaloneApp
{
Expand All @@ -12,6 +13,7 @@ public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddBaseAddressHttpClient();

await builder.Build().RunAsync();
}
Expand Down
1 change: 1 addition & 0 deletions src/Components/test/testassets/BasicTestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static async Task Main(string[] args)

builder.RootComponents.Add<Index>("root");

builder.Services.AddBaseAddressHttpClient();
builder.Services.AddSingleton<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
builder.Services.AddAuthorizationCore(options =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
Expand All @@ -18,7 +18,7 @@ public static async Task Main(string[] args)
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");

// use builder.Services to configure application services.
builder.Services.AddBaseAddressHttpClient();
#if (IndividualLocalAuth)
#if (Hosted)
builder.Services.AddApiAuthorization();
Expand Down