我如何在Blazor Server中将服务注入实体框架adddbContextFactory lambda函数?

发布于 2025-01-30 16:08:30 字数 642 浏览 3 评论 0原文

在Blazor Server中,如何将范围的服务注入下面的lambda,以便我可以阅读经过身份验证的用户并根据用户选择一个SQL连接字符串。

builder.Services.AddDbContextFactory<GlueDbContext>((provider, options) =>
    {
        var AuthenticationStateProvider = provider.GetService<AuthenticationStateProvider>();
        // *** Compiles but FAILS because AuthenticationStateProvider is not a Singleton ***
        var user = _authenticationStateProvider.GetAuthenticationStateAsync().Result.User;
        //
        string sqlConnectString = SomeFunctionDerivingTheConnectionFromTheUser(user);
        options.UseMySql(connectionString);
    });

In Blazor server, how can I inject a scoped service into the lambda below so that I can read the authenticated user and select a SQL connection string based on the user.

builder.Services.AddDbContextFactory<GlueDbContext>((provider, options) =>
    {
        var AuthenticationStateProvider = provider.GetService<AuthenticationStateProvider>();
        // *** Compiles but FAILS because AuthenticationStateProvider is not a Singleton ***
        var user = _authenticationStateProvider.GetAuthenticationStateAsync().Result.User;
        //
        string sqlConnectString = SomeFunctionDerivingTheConnectionFromTheUser(user);
        options.UseMySql(connectionString);
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

断舍离 2025-02-06 16:08:30

以下 this IDE因此可能包含一些错别字):

var serviceScopeFactory = provider.GetService<IServiceScopeFactory>(); //IServiceScopeFactory is a singleton, so you can easily get it here
using var scope = serviceScopeFactory.CreateScope();
var authenticationStateProvider = scope.GetService<AuthenticationStateProvider>();
//...

Following this link, it should look something like this (I'm typing without IDE so it may contain some typos):

var serviceScopeFactory = provider.GetService<IServiceScopeFactory>(); //IServiceScopeFactory is a singleton, so you can easily get it here
using var scope = serviceScopeFactory.CreateScope();
var authenticationStateProvider = scope.GetService<AuthenticationStateProvider>();
//...
奶气 2025-02-06 16:08:30

你不能那样做。 Services.Add ....只是将类/接口添加到集合中。直到后来,服务容器才开始初始化,直到您使用DI对象之前,该对象的实例才会初始化。

为了说明过程,这是测试中设置服务容器的代码。

        var services = new ServiceCollection();
        services.AddDbContextFactory<InMemoryWeatherDbContext>(options => options.UseInMemoryDatabase("WeatherDatabase"));
        services.AddSingleton<IDataBroker, ServerDataBroker>();
        var serviceProvider = services.BuildServiceProvider();

无论您设计什么,都需要重新思考。在温泉会话初始化之前,您无法获得用户。

You can't do that. Services.Add.... simply adds classes/interfaces to a collection. It's not till later that the services container gets initialised, and until you use a DI object that an instance of that object gets initialised.

To illustrate the process, here's the code to set up a services container serviceProvider in a test.

        var services = new ServiceCollection();
        services.AddDbContextFactory<InMemoryWeatherDbContext>(options => options.UseInMemoryDatabase("WeatherDatabase"));
        services.AddSingleton<IDataBroker, ServerDataBroker>();
        var serviceProvider = services.BuildServiceProvider();

Whatever you design, it needs rethinking. You can't get a user until an SPA session has initialized.

橘虞初梦 2025-02-06 16:08:30

AddDbContextFactory在Singleton上具有默认参数集。
添加 servicelifetime.scoped

builder.Services.AddDbContextFactory<GlueDbContext>((provider, options) =>
    {
        var AuthenticationStateProvider = provider.GetService<AuthenticationStateProvider>();
        var user = _authenticationStateProvider.GetAuthenticationStateAsync().Result.User;
        string sqlConnectString = SomeFunctionDerivingTheConnectionFromTheUser(user);
        options.UseMySql(connectionString);
    }, ServiceLifetime.Scoped);

AddDbContextFactory has a defaulted parameter set to Singleton.
add ServiceLifetime.Scoped

builder.Services.AddDbContextFactory<GlueDbContext>((provider, options) =>
    {
        var AuthenticationStateProvider = provider.GetService<AuthenticationStateProvider>();
        var user = _authenticationStateProvider.GetAuthenticationStateAsync().Result.User;
        string sqlConnectString = SomeFunctionDerivingTheConnectionFromTheUser(user);
        options.UseMySql(connectionString);
    }, ServiceLifetime.Scoped);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文