dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.38k stars 10k forks source link

Use IConfiguration from Program.cs in Blazor WebAssembly #20483

Closed yudielcurbelo closed 4 years ago

yudielcurbelo commented 4 years ago

I'm trying to access wwwroot/appsettings.json from Program.cs in Blazor WebAssembly 3.2.0 Preview 3.

Content of Program.cs var configuration = builder.Services.BuildServiceProvider().GetRequiredService<IConfiguration>(); var ApiBaseAddress = configuration["ApiBaseAddress"];

Content of appsettings.json { "ApiBaseAddress": "https://localhost:5001/api/" }

Error No service for type 'Microsoft.Extensions.Configuration.IConfiguration' has been registered.

benhysell commented 4 years ago

I was able to get access to the configuration while setting up https://github.com/sounj142/HLSoft.BlazorWebAssembly.Authentication.OpenIdConnect

in Program.cs

builder.Services.AddAuthorizationCore(options => { })
               .AddBlazoredOpenIdConnect(async (provider) =>
               {
                   var options = new OpenIdConnectOptions();                  
                   var configuration = provider.GetRequiredService<IConfiguration>();
                   options.Authority = configuration["SpaOptions:Authority"].ToString(); 
                   options.ClientId = configuration["SpaOptions:SpaClientId"].ToString();
                   options.ResponseType = "token id_token";
                   options.Scopes.Add("openid");
                   options.Scopes.Add("profile");
                   options.Scopes.Add("email");
                   options.Scopes.Add(configuration["SpaOptions:Scope"].ToString());
                   return options;
               })
            ;
benhysell commented 4 years ago

Looks like this is also being tracked over here: https://github.com/dotnet/aspnetcore/issues/20231

mkArtakMSFT commented 4 years ago

Thanks @benhysell, this is indeed a dupe of the referenced issue.