随机"错误:电路未能初始化";使用Blazor应用
我也有同样的问题,当我发布网站时,会出现这个错误。除了重新制作项目,还有其他解决方案吗?
当我加载页面时,我得到了这个错误。
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");
});
}
配置服务
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)));
}
}
和导入剃刀(_I(
@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
我终于成功了,但我不确定这是否奏效了。但我改变了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();
}