最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

session - How can I retrieve data from a Scoped Service in a Singleton Service? - Stack Overflow

programmeradmin0浏览0评论

We've been facing an issue in our Blazor Server App

  1. User Logs in.
  2. When user is Authenticated is shown a companies list.
  3. User can choose between one of the companies in the list.
  4. In this moment we need to save the CompanyDBName and CompanyUi of the choosen company (The only place we found to store this was ProtectedSessionStorage [Scoped Service], because it's a place with a life cicle of the session, beggining in the user login and ending in user logout).
  5. We have MainService (MUST be Singleton Service, because it contains other application relevant data) with a DbConnectionString property that stores the connection string without any Initial Catalog and a method GetCompanyConnectionString() that adds CompanyDbName to DbConnectionString and returns CompanyDbConnetionString.
  6. CompanyDbConnection string is used in all Factories that works with DataBase.

The Issue:

  • We can´t get the CompanyDbName inside of the GetCompanyConnectionString() method because the ProtectedSessionStorage is a Scoped Service, and MainService is a Singleton Service.
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>();

We've been facing an issue in our Blazor Server App

  1. User Logs in.
  2. When user is Authenticated is shown a companies list.
  3. User can choose between one of the companies in the list.
  4. In this moment we need to save the CompanyDBName and CompanyUi of the choosen company (The only place we found to store this was ProtectedSessionStorage [Scoped Service], because it's a place with a life cicle of the session, beggining in the user login and ending in user logout).
  5. We have MainService (MUST be Singleton Service, because it contains other application relevant data) with a DbConnectionString property that stores the connection string without any Initial Catalog and a method GetCompanyConnectionString() that adds CompanyDbName to DbConnectionString and returns CompanyDbConnetionString.
  6. CompanyDbConnection string is used in all Factories that works with DataBase.

The Issue:

  • We can´t get the CompanyDbName inside of the GetCompanyConnectionString() method because the ProtectedSessionStorage is a Scoped Service, and MainService is a Singleton Service.
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>();
Share Improve this question edited Feb 7 at 6:53 Qiang Fu 8,4511 gold badge6 silver badges16 bronze badges asked Feb 6 at 13:11 Pedro HortaPedro Horta 93 bronze badges 4
  • As asked in Staging ground: how can the company name be stored in a singleton? What if the next user logs in for another company? – Gert Arnold Commented Feb 6 at 13:56
  • What you want to do there is not possible. But why does GetCompanyConnectionString() have to be part of MainService? You could move this function into an injectable factory that uses MainService.DbConnectionString and ProtectedSessionStorage to do the same. – Good Night Nerd Pride Commented Feb 6 at 14:21
  • 2 The simplistic answer is to Inject the singleton service into the scoped service, and set a value in the singleton from the scoped service. However, that is almost certainly not the solution. You need to rethink your design [and almost certainly use a DbcontextFactory]. – MrC aka Shaun Curtis Commented Feb 6 at 17:09
  • Singleton are shared states, similar to static classes. That's why you cannot inject a scoped service into a singleton service, just like a static class cannot have a non static class as a variable. The relation goes one way, instantiated stuff can store reference of static stuff but not the other way around. That's a protection to minimize human mistake and side effects. Considering what you are describing, your MainService seems to have too many purpose or those relevant data should be store elsewhere, maybe in environment variable. – Ashijo Commented Feb 7 at 0:23
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论