Asp.net core 错误:';在SetAuthenticationState之前调用了GetAuthenticationStateAync;
我有一个blazor项目,其中我使用依赖项注入来使用AuthenticationStateProvider 我收到错误:Asp.net core 错误:';在SetAuthenticationState之前调用了GetAuthenticationStateAync;,asp.net-core,dependency-injection,asp.net-identity,blazor,telerik-reporting,Asp.net Core,Dependency Injection,Asp.net Identity,Blazor,Telerik Reporting,我有一个blazor项目,其中我使用依赖项注入来使用AuthenticationStateProvider 我收到错误: '在SetAuthenticationState之前调用了GetAuthenticationStateAync 我有两项服务: 返回用户标识的ServiceClass IReportServiceConfiguration使用另一个服务从数据库(xml字符串)返回报告 报表解析程序间接依赖于ServiceClass,这是startup.cs中的注册: services.A
'在SetAuthenticationState之前调用了GetAuthenticationStateAync 我有两项服务:
services.AddScoped<IServiceClass, ServiceClass>();
services.AddScoped<IReportServiceConfiguration>(sp =>
new ReportServiceConfiguration
{
ReportingEngineConfiguration = sp.GetService<IConfiguration>(),
HostAppId = "Html5DemoAppCore",
Storage = new FileStorage(),
ReportSourceResolver = sp.GetService<CustomReportSourceResolver>(),
}
); ;
为了通过数据库获取报告,它使用datacontext,从上面的代码可以看出。Datacontext具有动态连接字符串,这就是为什么执行转到服务类中的getIdentity()方法,然后我们在主题中得到消息。请发布ServiceClass服务的代码…@enet我已将代码添加到帖子中。其中还有其他方法,但我刚刚发布了有问题的方法。@enet在github上托管此方法很困难,因为项目中使用了一些付费订阅/源,除非您购买它们,否则无法编译。还有别的方法吗?以前从来没有这样做过。我想还有其他网站可以让你下载代码。我只是想看看相关的代码。如果你找不到上传应用程序的方法,只需在这里发布相关代码,稍后你可以删除它。添加更多代码以进一步解释
public class ServiceClass : IServiceClass
{
public ServiceClass(AuthenticationStateProvider authenticationStateProvider)
{
_authenticationStateProvider = authenticationStateProvider;
}
private readonly AuthenticationStateProvider _authenticationStateProvider;
private string authuser = "";
public async Task<string> GetIdentity()
{
var result = "";
try
{
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
IEnumerable<Claim> claims = user.Claims;
if (authState != null)
{
result = claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").Value;
authuser = result;
}
else
result = authuser;
}
catch (Exception ex)
{ System.Diagnostics.Debug.WriteLine(ex.ToSafeString()); }
return result;
}
}
<ReportViewer ViewerId="rv1"
ServiceUrl="/api/reports"
ReportSource="@(new ReportSourceOptions()
{
Report = "reportAccountingGeneralLedger",
Parameters= new Dictionary <string,object>
{}
})"
Parameters="@(new ParametersOptions { Editors = new EditorsOptions { MultiSelect = EditorType.ComboBox, SingleSelect = EditorType.ComboBox } })"
ScaleMode="@(ScaleMode.Specific)"
Scale="1.0" />
class CustomReportSourceResolver : IReportSourceResolver
{
public CustomReportSourceResolver(IServiceClass sc, IReportConnectionStringManager csm)
{
userClass = sc;
conStrMan = csm;
//_scopeFactory = sc;
}
private readonly IServiceClass userClass;
private readonly IReportConnectionStringManager conStrMan;
public Telerik.Reporting.ReportSource Resolve(string reportName, OperationOrigin operationOrigin, IDictionary<string, object> currentParameterValues)
{
var report = string.Empty;
if (!string.IsNullOrEmpty(reportName))
{
using (var context = new DataContext(userClass))
{
report = (from table in context.Reports where table.Name == reportName select table.Definition).First();
}
}
ReportSource updatedReport;
if (string.IsNullOrEmpty(report))
{
throw new System.Exception("Unable to load a report with the specified ID: " + reportName);
}
else
{
XmlReportSource retreivedReport = new Telerik.Reporting.XmlReportSource { Xml = report };
updatedReport = conStrMan.UpdateReportSource(retreivedReport);
}
return updatedReport;
}
}
public class DataContext : DbContext
{
public DataContext(IServiceClass sc)
{
serviceClass = sc;
}
private readonly IServiceClass serviceClass;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(serviceClass.getUserConnectionString().Result);
}
object1..object2.. object3....
}