FirebaseApp have static dictionary for initialize. If you register one time named instance you are not able to create again same instance. The key is GetInstance method. Dont forget every firebase feature (messaging, firedb etc.) sharing instances with firebaseapp class in .Net.
If you initialize parameterless firebaseapp taking it for default instance. But if you use keyed onces there is instance register problem. I solved this problem with this static method. You can implement it anywhere.
static FirebaseApp CreateApplication(AppOptions? appOptions, string? name)
{
if (FirebaseApp.DefaultInstance is null)
{
if (name.IsNullOrEmptyOrWhiteSpace())
{
return appOptions switch
{
not null when name.IsNullOrEmptyOrWhiteSpace() => FirebaseApp.Create(appOptions),
null when name.IsNullOrEmptyOrWhiteSpace() => FirebaseApp.Create(),
_ => FirebaseApp.Create(),
};
}
else
{
FirebaseApp alreadyExistInstance = FirebaseApp.GetInstance(name) ??
appOptions switch
{
not null when name.IsNotNullOrEmptyOrWhiteSpace() => FirebaseApp.Create(appOptions, name),
not null when name.IsNullOrEmptyOrWhiteSpace() => FirebaseApp.Create(appOptions),
null when name.IsNotNullOrEmptyOrWhiteSpace() => FirebaseApp.Create(name),
null when name.IsNullOrEmptyOrWhiteSpace() => FirebaseApp.Create(),
_ => FirebaseApp.Create(),
};
return alreadyExistInstance;
}
}
else
{
return FirebaseApp.DefaultInstance;
}
}