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 8896439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:03:02+00:00 2026-06-15T00:03:02+00:00

I am using StructureMap for creating instance of DbEntities per request in ASP.NET. ObjectFactory.Initialize(x

  • 0

I am using StructureMap for creating instance of DbEntities per request in ASP.NET.

ObjectFactory.Initialize(x =>
{
    x.For<DbEntities>().HttpContextScoped().Use(CreateNewDbEntities); 
}

I have a bacgkround task running every 5 seconds which is trying to use DbEntities.

timer = new Timer(RunTasks, null, 1000 * 10/*time to wait until the first run*/, 1000 * 5/*time to wait between calls*/);

Now in method RunTasks I get null reference excption when I try to call GetDbEntities

private static void RunTasks(object sender)
{
    var muninService = GetDbEntities(); // Null reference excpetion
}

public static DbEntities GetDbEntities()
{
    return ObjectFactory.GetInstance<DbEntities>();
}

I am guessing this is becauase in a background thread I don’t have access to httpcontextscope. Now I am new to structure map and I don’t know were to start and fix this problem. Any ideas ?

Other methods that I use:

private static DbEntities CreateNewDbEntities()
{
    return new DbEntities();
}
  • 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-15T00:03:05+00:00Added an answer on June 15, 2026 at 12:03 am

    Try:

    ObjectFactory.Initialize(x =>
    {
        x.For<DbEntities>().HybridHttpOrThreadLocalScoped().Use(CreateNewDbEntities); 
    }
    

    This should cache the instance per thread if a HttpContext is not available.

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

Sidebar

Related Questions

I have an asp.net mvc2 application that is using StructureMap 2.6 and NHibernate 3.x.
I am using StructureMap on an ASP.NET MVC project. I have an object that
I'm using StructureMap 2.6.1 This is the code from Bootstrapper.cs: ObjectFactory.Initialize(x => x.For<IFoo>().Use<Foo>()); When
I am building an asp.net MVC2 web app using StructureMap. I have created a
I have an ASP.Net MVC application and I am using StructureMap within MVC to
I'm using StructureMap in ASP .Net MVC 4 and I have got the following
i've been using structuremap since a couple of months. I always use ObjectFactory.GetInstance to
I'm using StructureMap to have an instance of an interface and I wrap it
I have just started using StructureMap, having previously worked with Spring.Net. I love the
We've been using StructureMap for Dependency Injection in our ASP.Net MvC 3 web apps

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.