Random "Error: The circuit failed to initialize" with Blazor app
I have the same problem, when I publish the site, this error will occur. Is there any other solution than making project again?
When i load the page i get this error.
Error: The circuit failed to initialize.
e.log
e.invokeClientMethod
e.processIncomingData
connection.onreceive
i.onmessage
blazor.server.js:1 [2020-07-10T13:59:08.305Z] Information: Connection disconnected.
blazor.server.js:1 Uncaught (in promise) Error: Invocation canceled due to the underlying connection being closed.
at e.connectionClosed (blazor.server.js:1)
at e.connection.onclose (blazor.server.js:1)
at e.stopConnection (blazor.server.js:1)
at e.transport.onclose (blazor.server.js:1)
at e.close (blazor.server.js:1)
at e.stop (blazor.server.js:1)
at e.<anonymous> (blazor.server.js:1)
at blazor.server.js:1
at Object.next (blazor.server.js:1)
at a (blazor.server.js:1)
With this Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UsePathBase("/");
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor().AddCircuitOptions(o =>
{
o.DetailedErrors = true;
});
services.AddBlazoredSessionStorage();
services.AddSingleton<INRPClientApiClient>(x =>
new NRPClientApiClient(new Microsoft.Rest.TokenCredentials("default", "token")));
services.AddOptions();
services.AddAuthorizationCore();
// this line makes the problem
services.AddScoped<AuthenticationStateProvider, NrpAuthenticationProvider>();
}
When I disable my own AuthenticationStateProvider it works, but I want it in my application.
public class NrpAuthenticationProvider : AuthenticationStateProvider
{
private ISessionStorageService _sessionStorageService;
public NrpAuthenticationProvider(ISessionStorageService sessionStorageService)
{
_sessionStorageService = sessionStorageService;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var emailAddress = await _sessionStorageService.GetItemAsync<string>("emailAddress");
var role = await _sessionStorageService.GetItemAsync<string>("role");
ClaimsIdentity identity;
if (emailAddress != null)
{
identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, emailAddress),
new Claim(ClaimTypes.Role, role),
}, "apiauth_type");
}
else
{
identity = new ClaimsIdentity();
}
var user = new ClaimsPrincipal(identity);
return await Task.FromResult(new AuthenticationState(user));
}
public void MarkUserAsAuthenticated(string emailAddress, string role)
{
var identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, emailAddress),
new Claim(ClaimTypes.Role, role),
}, "apiauth_type");
var user = new ClaimsPrincipal(identity);
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(user)));
}
public void MarkUserAsLoggedOut()
{
_sessionStorageService.RemoveItemAsync("emailAddress");
_sessionStorageService.RemoveItemAsync("role");
var identity = new ClaimsIdentity();
var user = new ClaimsPrincipal(identity);
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(user)));
}
}
and _Imports.razor
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using NRPBlazor
@using NRPBlazor.Shared
@using BlazorInputFile
@using System.IO
@using AuthenticationProvider
I finally make it to work, but I am not sure if this did the trick. But I changed sort of initalizing services in Startup.cs like this.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor().AddCircuitOptions(o =>
{
o.DetailedErrors = true;
});
services.AddSingleton<INRPClientApiClient>(x =>
new NRPClientApiClient(new Microsoft.Rest.TokenCredentials("default", "token")));
services.AddOptions();
services.AddAuthorizationCore();
services.AddBlazoredSessionStorage(config => config.JsonSerializerOptions.WriteIndented = true);
services.AddScoped<AuthenticationStateProvider, NrpAuthenticationProvider>();
//services.AddBlazoredSessionStorage();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.