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

  • Home
  • SEARCH
  • 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 8024489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:53:05+00:00 2026-06-04T22:53:05+00:00

Trying to set up Dapper with StructureMap to utilize a per request type of

  • 0

Trying to set up Dapper with StructureMap to utilize a “per request” type of scenario. In my Global.asax I have the following (modified from an old post of Ayende’s involving NHibernate, and one I found discussing the BuildUp() method in StructureMap):

protected static IDbConnection CreateConnection() { 
    var settings = ConfigurationManager.ConnectionStrings["MyConnectionString"];
    var connection = DbProviderFactories.GetFactory(settings.ProviderName).CreateConnection();
    if (connection == null) { 
        throw new ArgumentNullException("connection");
    }
    connection.ConnectionString = settings.ConnectionString;
    return connection;
}

public static IDbConnection CurrentConnection { 
    get { return (IDbConnection)HttpContext.Current.Items["current.connection"]; }
    set { HttpContext.Current.Items["current.connection"] = value; }
}

public Global() { 
    BeginRequest += (sender, args) => { 
        CurrentConnection = CreateConnection();
        CurrentConnection.Open(); 
    };

    EndRequest += (sender, args) => { 
        if (CurrentConnection == null) return;
        CurrentConnection.Close();
        CurrentConnection.Dispose();
    }
}

void Application_Start(object sender, EventArgs e) { 
    ObjectFactory.Initialize(x => { 
        x.For<IDbConnection>().Singleton().Use(CreateConnection());
        x.For<ICustomerRepository>().Use<CustomerRepository>();
        x.SetAllProperties(y => y.OfType<ICustomerRepository>());
    });
}

// BasePage.cs
public class BasePage : System.Web.UI.Page { 
    public IDbConnection CurrentConnection { get; set; }

    public BasePage() { 
        ObjectFactory.BuildUp(this);
    }
}

Every time I try to call this, the actual query fails with an error stating the Connection’s current state is closed, although a breakpoint on the BeginRequest handler shows that Open() is being called on the connection.

It seems to work if I manually call Open and Close on the IDbConnection inside each repository method, but I’m trying to avoid having to do that if at all possible.

  • 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-04T22:53:07+00:00Added an answer on June 4, 2026 at 10:53 pm

    You’re creating the connection as a singleton. That means that there will only be one connection object used for the entire applications pages. The connection that you new up in the Application_Start handler is never used by the pages, since they will get the connection from the container.

    You would be better off using something like this:

    void Application_Start(object sender, EventArgs e) { 
        ObjectFactory.Initialize(x => { 
            x.For<IDbConnection>().HttpContextScoped().Use(() => CreateConnection());
            ...
        }
    }
    
     public Global() { 
        EndRequest += (sender, args) => { 
            ObjectFactory.GetInstance<IDbConnection>.Dispose();
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying set the single table inheritance model type in a form. So i
I need help trying set-up Foreign Keys after having difficulty following Music library MySQL
Currently trying to set up and deploy a Sencha Touch 2 application. I'm following
I have a stored procedure that I'm trying to execute using Dapper that is
Trying to set-up a git server on my local dev machine and have been
Im trying to set my tabs as a scrollbar. I have 6 tabs but
Im trying to set the name of a radio button that I generated from
Trying to set up a menu so that I have border-left on each menu
I have following two classes and corresponding db tables. I am trying to insert
I have following classes and db schema. I am trying to query this data

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.