Reputation: 9
We've been facing an issue in our Blazor Server App
The Issue:
public class MainService
{
private readonly ProtectedSessionStorage _protectedSessionStorage;
public string DbConnectionString{ get; set; } = "Data Source=localhost\SQLEXPRESS;Initial Catalog=;Integrated Security=True";
public SessionDataService(ProtectedSessionStorage protectedSessionStorage)
{
// ***The Error Happens Here *** - in the Injection of ProtectedSessionStorage
_protectedSessionStorage = protectedSessionStorage;
}
async Task<string> GetCompanyConnectionString()
{
// Get CompanyDbName from ProtectedSessionStorage
var companyDbName = await _protectedSessionStorage.GetAsync<string>("UserDb");
// Add to CompanyDbName to DbConnectionString (that is common to all sessions)
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(DbConnectionString);
builder.InitialCatalog = companyDbName;
// returns ConnectionString that we use by A session in the DataBaseFactory calls
return builder.ToString();
}
}
builder.Services.AddSingleton<MainService>();
Upvotes: 0
Views: 61
Reputation: 8811
MainService (MUST be Singleton Service, because it contains other application relevant data)
From my understanding, you need it to be singleton because it stores/initiates some data even when user is not login. Then you could use it as scoped and store/retrive data from protected LocalStorage
.
If it must be singleton anyway, then GetCompanyConnectionString
hasn't to be a method in MainService. You could create another scope service for it.
Upvotes: 0