[英]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.