Random "Error: The circuit failed to initialize" with Blazor app
我有同样的问题,当我发布站点时,将发生此错误。除了重新进行项目之外,还有其他解决方案吗?
加载页面时出现此错误。
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)
使用此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>();
}
当我禁用自己的AuthenticationStateProvider时,它可以工作,但是我希望在我的应用程序中使用它。
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)));
}
}
和_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
答案 0 :(得分:0)
我终于使它起作用了,但是我不确定这是否能解决问题。但是我像这样在Startup.cs中更改了一些初始化服务。
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();
}