Inject framework HttpClient to support prerendering behavior on server instead of baking in the HttpClient on the client project
This commit is contained in:
@@ -3,9 +3,9 @@
|
||||
public class TrackEntity
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string MediaPath { get; set; }
|
||||
public string TrackName { get; set; }
|
||||
public string Artist { get; set; }
|
||||
public required string MediaPath { get; set; }
|
||||
public required string TrackName { get; set; }
|
||||
public required string Artist { get; set; }
|
||||
public string? Album { get; set; }
|
||||
public string? Genre { get; set; }
|
||||
public DateOnly? ReleaseDate { get; set; }
|
||||
|
||||
@@ -3,12 +3,18 @@ using DeepDrftModels.Models;
|
||||
using NetBlocks.Models;
|
||||
using System.Text.Json;
|
||||
using System.Web;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace DeepDrftWeb.Client.Clients;
|
||||
|
||||
public class TrackClient : ApiClient<ClientConfig>
|
||||
public class TrackClient
|
||||
{
|
||||
public TrackClient(ClientConfig config) : base(config) { }
|
||||
private readonly HttpClient _http;
|
||||
|
||||
public TrackClient(HttpClient http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public async Task<ApiResult<PagedResult<TrackEntity>>> GetPage(
|
||||
int pageNumber,
|
||||
@@ -16,24 +22,20 @@ public class TrackClient : ApiClient<ClientConfig>
|
||||
string? sortColumn = null,
|
||||
bool sortDescending = false)
|
||||
{
|
||||
var uriBuilder = new UriBuilder(http.BaseAddress!)
|
||||
{
|
||||
Path = "api/track/page"
|
||||
var queryArgs = new Dictionary<string, string?>(){
|
||||
["pageNumber"] = pageNumber.ToString(),
|
||||
["pageSize"] = pageSize.ToString()
|
||||
};
|
||||
|
||||
var query = HttpUtility.ParseQueryString(string.Empty);
|
||||
query["pageNumber"] = pageNumber.ToString();
|
||||
query["pageSize"] = pageSize.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(sortColumn))
|
||||
query["sortColumn"] = sortColumn;
|
||||
queryArgs["sortColumn"] = sortColumn;
|
||||
|
||||
if (sortDescending)
|
||||
query["sortDescending"] = "true";
|
||||
queryArgs["sortDescending"] = "true";
|
||||
|
||||
uriBuilder.Query = query.ToString();
|
||||
string query = QueryString.Create(queryArgs).ToString();
|
||||
|
||||
var response = await http.GetAsync(uriBuilder.Uri);
|
||||
var response = await _http.GetAsync($"api/track/page{query}");
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
var dto = JsonSerializer.Deserialize<ApiResultDto<PagedResult<TrackEntity>>>(json, new JsonSerializerOptions
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.*" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.0" />
|
||||
<PackageReference Include="MudBlazor" Version="8.*" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -4,8 +4,11 @@ using MudBlazor.Services;
|
||||
|
||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||
|
||||
Console.WriteLine(builder.HostEnvironment.BaseAddress);
|
||||
builder.Services.AddScoped<HttpClient>(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
|
||||
|
||||
builder.Services.AddMudServices();
|
||||
|
||||
Startup.ConfigureDomainServices(builder.Services, builder.HostEnvironment.BaseAddress);
|
||||
Startup.ConfigureDomainServices(builder.Services);
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
|
||||
@@ -7,13 +7,9 @@ namespace DeepDrftWeb.Client;
|
||||
|
||||
public static class Startup
|
||||
{
|
||||
public static void ConfigureDomainServices(IServiceCollection services, string baseAddress)
|
||||
public static void ConfigureDomainServices(IServiceCollection services)
|
||||
{
|
||||
// HTTP Client for Server API
|
||||
services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(baseAddress) });
|
||||
|
||||
// Track Client
|
||||
services.AddSingleton(new ClientConfig(baseAddress));
|
||||
services.AddScoped<TrackClient>();
|
||||
services.AddScoped<TrackGalleryViewModel>();
|
||||
}
|
||||
|
||||
@@ -24,9 +24,4 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.*" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Components\Pages\Auth\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,18 +1,20 @@
|
||||
using DeepDrftWeb;
|
||||
using MudBlazor.Services;
|
||||
using DeepDrftWeb.Components;
|
||||
using DeepDrftWeb.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add MudBlazor services
|
||||
builder.Services.AddMudServices();
|
||||
|
||||
// Add HttpClient services for prerendering
|
||||
builder.Services.AddHttpClient("DeepDrft.API", client => client.BaseAddress = new Uri(Startup.GetKestrelUrl(builder)));
|
||||
builder.Services.AddScoped(sp =>
|
||||
sp.GetRequiredService<IHttpClientFactory>().CreateClient("DeepDrft.API"));
|
||||
|
||||
Startup.ConfigureDomainServices(builder);
|
||||
|
||||
var hostUrl = builder.Configuration.GetValue<string>("ASPNETCORE_URLS")?.Split(';').First() ?? throw new Exception("ASPNETCORE_URLS undefined");
|
||||
DeepDrftWeb.Client.Startup.ConfigureDomainServices(builder.Services, hostUrl);
|
||||
DeepDrftWeb.Client.Startup.ConfigureDomainServices(builder.Services);
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
|
||||
@@ -16,6 +16,32 @@ public static class Startup
|
||||
// Add Track services
|
||||
builder.Services.AddScoped<TrackRepository>();
|
||||
builder.Services.AddScoped<TrackService>();
|
||||
}
|
||||
|
||||
public static string GetKestrelUrl(this WebApplicationBuilder builder)
|
||||
{
|
||||
// Check all the places Kestrel URL can be configured
|
||||
var urls = builder.Configuration["ASPNETCORE_URLS"]
|
||||
?? builder.Configuration["urls"];
|
||||
|
||||
if (!string.IsNullOrEmpty(urls))
|
||||
{
|
||||
return urls.Split(';')[0].Trim();
|
||||
}
|
||||
|
||||
// Check Kestrel endpoints configuration
|
||||
var kestrelSection = builder.Configuration.GetSection("Kestrel:Endpoints");
|
||||
var firstEndpoint = kestrelSection.GetChildren().FirstOrDefault();
|
||||
var endpointUrl = firstEndpoint?["Url"];
|
||||
|
||||
if (!string.IsNullOrEmpty(endpointUrl))
|
||||
{
|
||||
return endpointUrl;
|
||||
}
|
||||
|
||||
// ASP.NET Core defaults
|
||||
return builder.Environment.IsDevelopment()
|
||||
? "https://localhost:5001"
|
||||
: "http://localhost:5000";
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,6 @@
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
},
|
||||
"DetailedErrors": true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user