how to get the dbContext is always new when call the grain

18 篇文章 0 订阅
3 篇文章 0 订阅

Q:version info:

Orleans : 2.3.0
EF Core:2.2.3

services.AddDbContextPool<TestContext>(options =>
{
	options.UseSqlServer("");
});
    public class TestGrain : Grain,ITest
    {
        private  TestContext _testContext;

        public TestGrain(TestContext testContext)
        {
            _testContext = testContext;
        }

        public void Test()
        {
            var testContext = ServiceProvider.GetRequiredService<TestContext>();
        }
    }

I used the above 2 ways to get the context, I got the context used in the last call, not a new context.

 

A1:

Grains are not recreated for every call, so constructor injection (public TestGrain(TestContext testContext)) will not work. The second approach (ServiceProvider.GetRequiredService<TestContext>() inside a grain method) should work: each grain activation has its own dependency injection scope.

AddDbContext registers contexts using Scoped lifetime by default. That means that you will have one context per grain activation. You can change this behavior: https://docs.microsoft.com/en-au/dotnet/api/microsoft.extensions.dependencyinjection.entityframeworkservicecollectionextensions.adddbcontext?view=efcore-2.1

I do not see any overload to change the behavior for AddDbContextPool, however: https://docs.microsoft.com/en-au/dotnet/api/microsoft.extensions.dependencyinjection.entityframeworkservicecollectionextensions.adddbcontextpool?view=efcore-2.1

It may be worth opening an issue or PR in the EF Core repo to implement that

 

You can implement the behavior you want using a base class like this:

public class GrainBase : Grain, IIncomingGrainCallFilter
{
    public IServiceProvider RequestServices { get; set; }

    public async Task Invoke(IIncomingGrainCallContext context)
    {
        var scope = this.ServiceProvider.CreateScope();
        try
        {
            this.RequestServices = scope.ServiceProvider;
            await context.Invoke();
        }
        finally
        {
            scope.Dispose();
            this.RequestServices = null;
        }
    }
}

public class MyGrain : GrainBase, IMyGrain
{
    public Task<int> MyMethod()
    {
        var context = this.RequestService.GetRequiredService<MyDbContext>();
        // use db context
    }
}

A2:

Just adding an alternative method...

This registers a bare-bones factory of transient contexts:

.ConfigureServices(services =>
{
    services.AddDbContext<RegistryContext>(options =>
    {
        options.UseInMemoryDatabase("SomeDatabase", _registryContextRoot);
    }, ServiceLifetime.Transient);
    services.AddSingleton<Func<RegistryContext>>(_ => () => _.GetService<RegistryContext>());
})

Then the grain requests the factory:

public class StorageRegistryGrain : Grain, IStorageRegistryGrain
{
    private readonly Func<RegistryContext> _factory;

    public StorageRegistryGrain(Func<RegistryContext> factory)
    {
        _factory = factory;
    }

Then grain methods request a new context as and when required:

public async Task RegisterChannelUserAsync(ChannelUser entity)
{
    using (var context = _factory())
    {
        // do stuff here
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值