AddDbContextPool-提高性能

时间:2019-02-14 22:40:30

标签: c# asp.net-core .net-core

我已经在dotnetcore控制台应用程序2.2中使用了“ AddDbContextPool”,看起来它在数据库上创建了正确数量的会话,但似乎并没有利用它们。这是我的代码。我应该在整个控制台中使用相同的上下文还是在每次调用时创建一个新的上下文?

public class Startup
{

    private static IConfiguration _iconfiguration;
    static string appsettingsFile = @"appsettings.json";


    public void ConfigureServices(IServiceCollection services)
    {

        Console.WriteLine("Configuring Services");

        var builder = new ConfigurationBuilder().AddJsonFile(appsettingsFile);
        _iconfiguration = builder.Build();

        services.AddDbContextPool<MyDBContext>(c => c.UseSqlServer(_iconfiguration.GetSection("DBConnectionString").Value));


    }

}


class Program
{


    public static IServiceProvider _serviceProvider = null;

    static async Task Main(string[] args)
    {

        var serviceCollection = new ServiceCollection();
        var su = new Startup();
        su.ConfigureServices(serviceCollection);
        _serviceProvider = serviceCollection.BuildServiceProvider();


        var t = new Test(_serviceProvider);

        var t1 = t.List();
        var t2 = t.List();
        var t3 = t.List();

        await t1;
        await t2;
        await t3;

    }


}

class Test 
{

    private IServiceProvider _serviceProvider = null;

    public Test(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }


    public async Task<List<test_list>> List()
    {

        using (var serviceScope = _serviceProvider.CreateScope())
        {
            using (var context = serviceScope.ServiceProvider.GetService<MyDBContext>())
            {
                return await context.test_list.ToListAsync<test_list>();
            }
        }

    }


}

我应该在整个应用程序中重用作用域和上下文以提高性能吗?我希望同时打开3个与DB的连接,而不是一次打开1个。

编辑:

public class Startup
{

    private static IConfiguration _iconfiguration;
    static string appsettingsFile = @"appsettings.json";


    public void ConfigureServices(IServiceCollection services)
    {

        Console.WriteLine("Configuring Services");

        var builder = new ConfigurationBuilder().AddJsonFile(appsettingsFile);
        _iconfiguration = builder.Build();

        services.AddDbContextPool<MyDBContext>(c => c.UseSqlServer(_iconfiguration.GetSection("DBConnectionString").Value));


    }

}


class Program
{


    public static IServiceProvider _serviceProvider = null;

    static async Task Main(string[] args)
    {

        var serviceCollection = new ServiceCollection();
        var su = new Startup();
        su.ConfigureServices(serviceCollection);
        _serviceProvider = serviceCollection.BuildServiceProvider();

        var serviceScope = _serviceProvider.CreateScope();
        var context = serviceScope.ServiceProvider.GetService<MyDBContext>();

        var t = new Test(context);

        var t1 = t.List();
        var t2 = t.List();
        var t3 = t.List();

        await t1;
        await t2;
        await t3;

    }


}

class Test 
{

    private MyDBContext _context = null;

    public Test(MyDBContext_context)
    {
        context = _context;
    }


    public async Task<List<test_list>> List()
    {
        return await context.test_list.ToListAsync<test_list>();
    }


}

0 个答案:

没有答案