Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:53:10+00:00 2026-06-15T02:53:10+00:00

I have a problem managing the lifetime of open database connections with StructureMap scoped

  • 0

I have a problem managing the lifetime of open database connections with StructureMap scoped to HttpContext when there are persistent HTTP connections in my ASP.NET MVC application, like SignalR hubs.

My DI container, StructureMap, injects an open IDbConnection into several services. To ensure that these database connections are closed and properly disposed of, I call ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects() on the EndRequest event.

This works great for MVC controllers until a service requiring a database connection is injected into a SignalR hub, which keeps a persistent HTTP connection open for each client and eventually saturates the connection pool.

If I scope IDbConnection to a singleton, only one connection is ever opened per-application and the pool doesn’t saturate, but this is a bad idea in case the connection is ever locked or times out.

So maybe there is a way to customise the scope of database connections for my SignalR hubs? I tried resolving a service instance in each Hub method, but this still instantiates a database connection at the HttpContext scope and keeps it open for the duration of the calling client’s hub connection.

How should I manage the lifetime of database connections with StructureMap in an HTTP-scoped context when there are persistent HTTP connections around?

Example Code

Typical Service

public class MyService
{
    private IDbConnection _con;
    public MyService(IDbConnection con)
    {
        _con = con;
    }

    public IEnumerable<string> GetStuff()
    {
        return _con.Select<string>("SELECT someString FROM SomeTable").ToList();
    }
}

Typical SignalR Hub

public class MyHub : Hub
{
    private MyService _service;
    public MyHub(MyService service)
    {
        _service = service; // Oh Noes! This will open a database connection
                            // for each Client because of HttpContext scope
    }

    public Task AddMessage()
    {
        var result = _service.GetStuff();
        // ...
    }
}

StructureMap Configuration

For<IDbConnection>()
    .HybridHttpOrThreadLocalScoped()
    .Use(() => BaseController.GetOpenConnection(MyConnectionString));

Global.asax.cs

public class GlobalApplication : System.Web.HttpApplication
{
    public GlobalApplication()
    {
        EndRequest += delegate
        {
            ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
        };
    }
    // ...
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:53:11+00:00Added an answer on June 15, 2026 at 2:53 am

    Solution using transient database connection and nested StructureMap container

    First, configure a named, transient database connection instance in StructureMap:

    For<IDbConnection>()
        .Transient() // scope
        .Add(x => BaseController.GetOpenConnection(connectionString, IsDebugging()))
        .Named("Transient");
    

    Make sure you configure this before your default instance, or it will override the default instance.

    Secondly, inject an IContainer into your SignalR hub so you can build a nested StructureMap container:

    public class JobHub : Hub
    {
        private readonly IContainer _container;
    
        public JobHub(IContainer container)
        {
            _container = container;
        }
    
        public Task DoStuff(string input)
        {
            // ...
    

    Instantiate a nested container in your SignalR method and resolve your named transient database connection:

            using (var httpRequestScope = _container.GetNestedContainer())
            {
                var transientConnection =
                        httpRequestScope.GetInstance<IDbConnection>("Transient");
    

    Use .With<IDbConnection>(transientConnection) to ensure services and repositories instantiated by your nested container use this connection:

                var myService = httpRequestScope
                    .With<IDbConnection>(transientConnection)
                    .GetInstance<MyService>();
    
                var result = myService.DoStuff(input);
    
                return Clients.addResult(result);
            }
        }
    }
    

    Finally, the scoped using (...) statement will ensure that your nested container cleans up after itself, including the database connection.

    The downside here is that you are opening and closing a database connection for every SignalR method call, but since connections are pooled, releasing early may not be so bad. Your mileage should depend on your SignalR request volume.

    You may be able to ditch the nested container and just ask DependencyResolver.Current for the named connection instance, but then you may have to remember to explicitly close each connection to prevent a leak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have built ASP.NET app. I need to utilize a third party COM object
that's my first post here. I have problem with managing activities. I have my
I have a problem where I insert a database item using a SQLAlchemy /
I have written complex library for managing network communication based on iocp mechanism. Problem
I seem to have a problem with spring annotation-driven transaction managing and tomcat. These
So I have a very weird problem. I am writing some code in VB.Net
There is a very simple problem. I have a locale identifier, en, en_US, cs_CZ
Everyone managing open-source-software runs into the problem, that with the time the process of
I have problem with my query on C, I’m using the oci8 driver. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.