1

I've used "AddDbContextPool" to an dotnetcore console app 2.2 and it looks like it creates the right amount of sessions on the DB but doesn't seem to be utilizing them. Here is my code. Should I use the same context through the entire console or create a new one with every call?

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>();
            }
        }

    }


}

Should I reuse the scope and context throughout the application to give it better performance? I want that to open up 3 connections to DB at the same time versus 1 at a time.

EDIT:

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>();
    }


}
Chris
  • 336
  • 4
  • 15
  • 1
    DB connection pool should be managed automatically by the driver... this is completely unnecessary. I used to do this in Java, but in .NET this is handled for you automatically. – Rosdi Kasim Feb 14 '19 at 22:48
  • What is unnecessary? So, I should pass the context into the Function and not worry about disposing it? – Chris Feb 14 '19 at 23:24
  • I edited the code above to reflect not managing the context in the console app. Is that correct? – Chris Feb 14 '19 at 23:31
  • I do not know why you are trying this in console app, are you trying dependency injection?, but since you tag `asp.net-core`, this tutorial can explain more than what I could in SO. https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-2.2 – Rosdi Kasim Feb 15 '19 at 02:36
  • Yes I am using a dotnetcore console application but want to figure out why it is only using one connection at a time. This is for data processing not mvc/etc. Should I not be using DbContextPool? – Chris Feb 15 '19 at 04:17
  • I have never used it in any of my projects. Should you use it? this might answer your question: https://stackoverflow.com/questions/48443567/adddbcontext-or-adddbcontextpool – Rosdi Kasim Feb 15 '19 at 04:54
  • Also be wary of limitations, I suggest you NOT to use it unless you can deal with these limitations: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#limitations-1 – Rosdi Kasim Feb 15 '19 at 04:59

0 Answers0