Setting life span ranges in autofac when used as a ServiceStack IoC

I am currently using AutoFac as a DI container for our ServiceStack web service. I can configure the wiring and that’s all, but after reading the section in Scopes I don’t understand which range is best used when registering my components. In our particular case, I think the PerHttpRequest scope will be fine, as (please correct me if I am wrong). I would like to get rid of dependencies as soon as the request ends.

My question is: how do I install this in a container? I can't seem to find the scope of PerHttpRequest within the included methods in autofac. I'm also not sure if ServiceStack is making any kind of machine for this behind the scenes.

I am using Autofac 3.0.1 in ServiceStack 3.9.35 on .Net 4 (works as a regular ASP host, not MVC). I also use the described class here as an IContainer adapter.

+3
source share
4 answers

I think I figured out how to do this (using Autofac 2.6, which I'm stuck on right now.) It includes using the following adapter and the Autofac.Mvc3 package:

public class AutofacIocAdapter : IContainerAdapter
{
    private readonly IContainer _autofacRootContainer;
    private readonly Container _funqContainer;

    public AutofacIocAdapter(IContainer autofacRootContainer, Container funqContainer)
    {
        // Register a RequestLifetimeScopeProvider (from Autofac.Integration.Mvc) with Funq
        var lifetimeScopeProvider = new RequestLifetimeScopeProvider(autofacRootContainer,null);
        funqContainer.Register<ILifetimeScopeProvider>(x => lifetimeScopeProvider);
        // Store the autofac application (root) container, and the funq container for later use
        _autofacRootContainer = autofacRootContainer;
        _funqContainer = funqContainer;
    }

    public T Resolve<T>()
    {           
        return ActiveScope.Resolve<T>();
    }

    public T TryResolve<T>()
    {
        T result;
        if (ActiveScope.TryResolve(out result))
        {
            return result;
        }
        return default(T);
    }

    private ILifetimeScope ActiveScope
    {
        get
        {
            // If there is an active HttpContext, retrieve the lifetime scope by resolving
            // the ILifetimeScopeProvider from Funq.  Otherwise, use the application (root) container.
            return HttpContext.Current == null
                        ? _autofacRootContainer
                        : _funqContainer.Resolve<ILifetimeScopeProvider>().GetLifetimeScope();
        }
    }
}

Steps to implement:

  • Add the Autofac.Mvc3 NuGet package to your web project (NOTE: not that your project does not use MVC. The solution may be slightly different from Autofac 3, which cannot use Mvc3 integration.)
  • ServiceStack IoC IContainerAdapter Autofac,
+4

MVC, .

Funq PerRequest ILifetimeScope ILifetimeScope ConatinerAdaptor, .

public class AutofacLifetimeScopeIocAdapter : IContainerAdapter
{
    private readonly Container _requestContainer;

    public AutofacLifetimeScopeIocAdapter(Funq.Container requestContainer)
    {
        _requestContainer = requestContainer;
    }


    public T Resolve<T>()
    {
        var currentContainer = _requestContainer.Resolve<ILifetimeScope>();

        return currentContainer.Resolve<T>();
    }

    public T TryResolve<T>()
    {
        var currentContainer = _requestContainer.Resolve<ILifetimeScope>();

        T result;

        if (currentContainer.TryResolve<T>(out result))
        {
            return result;
        }

        return default(T);
    }

}

_autofacContainerRoot = builder.Build();
        IContainerAdapter adapter = new AutofacLifetimeScopeIocAdapter(container);

        container.Register<ILifetimeScope>((c) => _autofacContainerRoot.BeginLifetimeScope())
            .ReusedWithin(ReuseScope.Request);

        container.Adapter = adapter;

public override void OnEndRequest()
    {
        var currentContainer = _container.Resolve<ILifetimeScope>();
        currentContainer.Dispose();

        base.OnEndRequest();
    } 

, -, Autofac - SingleInstance, InstancePerDependency InstancePerLifetimeScope, perRequest.

Mythz HostContext.Instance.Items, ,

var currentContainer = _container.Resolve<ILifetimeScope>();

.

+5

RequestScope ServiceStack IOC ServiceStack Funq IOC.

RequestScope IOC, AutoFac, AutoFac , , . ServiceStack AppHostBase.OnEndRequest() hook, , .

, AutoFac, :

, ServiceStack, , HostContext.Instance.Items , HostContext.Instance.TrackDisposable, .

+2

2015-11-25. , . ServiceStack V3 V4 , nuget.

I solved this problem by opening a new scope for Application_BeginRequest and placing it in Application_EndRequest. In the container adapter, I check if this area exists and its use; if not, I use the container. This allows you to use the registration area .InstancePerRequest ().

The gists are described here .

+2
source

All Articles