[英]How to register multiple implementation for same interface using .NET CORE DI
[英]How to DI register FirebaseApp in .Net Core?
我应该如何在 Net core 中注册 FirebaseApp 实例? 作为 singleton 之类的
//startup.cs
services.AddSingleton<FirebaseApp>(sp =>
{
FirebaseApp.Create(new AppOptions
{
Credential = GoogleCredential.FromJson(settings)
});
});
或类似的范围
//startup.cs
services.AddScoped<FirebaseApp>(sp =>
{
FirebaseApp.Create(new AppOptions
{
Credential = GoogleCredential.FromJson(settings)
});
});
还是第三种选择?
因此,在 .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())
});
}
然后,您不需要将其注入到类的构造函数中。
您只需调用实例并加载其他 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.