[英]How to DI register FirebaseApp in .Net Core?
How should I register FirebaseApp instance in Net core?我应该如何在 Net core 中注册 FirebaseApp 实例? As a singleton like作为 singleton 之类的
//startup.cs
services.AddSingleton<FirebaseApp>(sp =>
{
FirebaseApp.Create(new AppOptions
{
Credential = GoogleCredential.FromJson(settings)
});
});
Or as scoped similarly或类似的范围
//startup.cs
services.AddScoped<FirebaseApp>(sp =>
{
FirebaseApp.Create(new AppOptions
{
Credential = GoogleCredential.FromJson(settings)
});
});
Or some third option?还是第三种选择?
So right after the builder in .net 6 I just instantiate it using the following:因此,在 .net 6 中的构建器之后,我只需使用以下命令对其进行实例化:
var builder = WebApplication.CreateBuilder(args);
// after the builder so that I can get the configurations for google credentials
// check if the instance is already loaded, if not build it up
if(FirebaseApp.DefaultInstance == null)
{
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromJson(AuthConfig.GetGoogleCredentialObject(builder.Configuration).ToString())
});
}
Then, you don't need to inject it into the constructor of your classes.然后,您不需要将其注入到类的构造函数中。
You just call up the instance and load up the other firebase modules like so:您只需调用实例并加载其他 firebase 模块,如下所示:
...
// this is a singleton and the props are static so available anytime
var customToken = FirebaseApp.DefaultInstance;
// use the instance from the singleton
await FirebaseAuth.GetAuth(customToken).SetCustomUserClaimsAsync(uid, additionalClaims);
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.