简体   繁体   English

如何在.Net Core中DI注册FirebaseApp?

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

相关问题 如何使用 .NET CORE DI 为同一接口注册多个实现 - How to register multiple implementation for same interface using .NET CORE DI .NET Core DI,为包注册默认实现 - .NET Core DI, register a default implementation for a package 如何将 map 的两个配置注册到 ASP.NET 内核的 DI 注入器中的相同类型 - How to register two configurations that map to the same type in DI injector in ASP.NET Core 向ASP.NET Core的DI注册NLog目标(具有依赖项) - Register NLog target (with dependencies) with ASP.NET Core's DI 在 .NET Core 中使用不同的构造函数参数(DI 和非 DI)注册多个 IHostedService 实例 - Register multiple instances of IHostedService with different constructor parameters (DI and non-DI) in .NET Core GC如何与.Net Core 2中的DI配合使用 - How does the GC works with DI in .Net Core 2 C#.Net Core 2 DI,如何使用 class 注册接口,该接口也采用与构造函数相同的接口作为参数(装饰器) - C# .Net Core 2 DI, how can I register an interface with a class that also take the same interface as constructor a parameter (Decorator) 带有Autofac DI的.NET Core - .NET Core with Autofac DI .NET 核心中的 DI 问题 - DI issue in .NET Core 关于如何在 .NET 5.0 Worker 服务中使用 DI 最好地注册 EventGridClient 的指南 - Guidance on how to best register the EventGridClient with DI in a .NET 5.0 Worker Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM