single instance use in http request

18 views
Skip to first unread message

Geoff Swartz

unread,
Dec 4, 2020, 10:13:03 AM12/4/20
to structuremap-users
I came across this thread which is quite old basically asking how to do what I need to do.   When a controller endpoint is hit, every service given to the controller constructor requires a data context in it's constructor.  I'm wanting to have a single data context created that is given to each service.  When I run my code it's creating a new one for each injection rather than sharing them.  

I upgraded our nuget packages to the latest version of structuremap but I'm not clear on how to accomplish what I need to do.  In the referenced thread there was mention of a pre-canned recipe in StructureMap.MVC5 for using a nested container per HTTP request.  I went to the link but I'm not seeing that.  Unfortunately, I'm pretty much a noob with structuremap, so not really sure what I'm looking for.  Any help would be appreciated.  

Here's some of the code we have that I hope is relevant.

public class DataRegistry : Registry
{
    public DataRegistry()
    {
        Scan(scan =>
        {
            scan.SingleImplementationsOfInterface();
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
            scan.LookForRegistries();
        });
        ConfigureEntityFramework();
    }

    private void ConfigureEntityFramework()
    {
        For<IConDbContext>().HybridHttpOrThreadLocalScoped().Use(() => new ConDbContext(DataSources.ConDataSource));
    }
}

Thanks.
Reply all
Reply to author
Forward
0 new messages