0

I have created a Blazor WebAssembly application with ASP Hosted, on the server I preload the blazor application following the microsoft tutorials.

There is a point in the documentation where it tells me that I have to create a new method to map the Blazor services to the server.

Here is my method:

namespace Test.Client
{
    public class CommonServices
    {
        public static void ConfigureCommonServices(IServiceCollection services, Uri uri)
        {
            services.AddScoped(sp => new HttpClient { BaseAddress = uri });
            services.AddBlazoredLocalStorage();          
            services.AddScoped<IAuthService, AuthService>();
            services.AddScoped<IAdvertiserService, AdvertiserService>();
            services.AddScoped<ICategoryService, CategoryService>();
            services.AddScoped<IItemService, ItemService>();
            services.AddScoped<IItemVariantService, ItemVariantService>();
            services.AddScoped<IPriceMonitorService, PriceMonitorService>();
            services.AddFileReaderService(o => o.UseWasmSharedBuffer = true);
            services.AddOptions();
            services.AddAuthorizationCore();
            services.AddScoped<AuthenticationStateProvider, CustomAuthStateProvider>();
        }
    }
}

And I load it on the client like this in Program.cs:

var builder = WebAssemblyHostBuilder.CreateDefault(args);

var uri = new Uri(builder.HostEnvironment.BaseAddress);

CommonServices.ConfigureCommonServices(builder.Services, uri);

await builder.Build().RunAsync();

The problem I have in the server Program.cs, I don't know how I can get the base address in this one, if I map it fixed it works, but when I take it to production logically this configuration stops working...

var uri = new Uri("https://localhost:7278/");

Test.Client.CommonServices.ConfigureCommonServices(builder.Services, uri);

var app = builder.Build();

My question is how can I get the correct url on the server? and if there is a better way to do this?

  • Does [Dynamically configure Http.BaseAddress for Client Side Blazor Deployment Environment](https://stackoverflow.com/questions/54378938/dynamically-configure-http-baseaddress-for-client-side-blazor-deployment-environ) help? – Andrew Morton Feb 26 '22 at 11:03
  • It not work in ASP Hosted – Andrés lb Feb 26 '22 at 16:11

0 Answers0