Autofac and PerRequestTask

Jun 2, 2010 at 2:24 PM

Running into a problem with Autofac and PerRequestTask.

First, I downloaded the source code and updated it to use the latest Autofac for .NET 3.5 that was released a few days ago. It compiled and built fine. I was hoping to use the version for .NET 4 for Lazy<T> Support but the build process complained about not having support for .NET 4.

Next, I created a PerRequestTask to open an NHibernate Session on each request. When I ran my application I received an Exception in the AutofacAdapter:

 

protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
     Type type = typeof(IEnumerable<>).MakeGenericType(serviceType);

     object instances = ResolutionExtensions.Resolve(Container, type);  <-- Exception thrown here

     return ((IEnumerable)instances).Cast<object>();
}

 

And the exception is:

"No scope matching the expression 'value(Autofac.Builder.RegistrationBuilder`3+<>c__DisplayClass0[System.Object,Autofac.Builder.ConcreteReflectionActivatorData,Autofac.Builder.SingleRegistrationStyle]).lifetimeScopeTag.Equals(scope.Tag)' is visible from the scope in which the instance was requested."

 

I removed the code from the task just to be sure it wasn't code related and still got the error. Here is an example of the empty PerRequestTask:

 

public class ManageNHibernateSession : PerRequestTask
{
        public override TaskContinuation Execute()
        {
            return TaskContinuation.Continue;
        }
 }

 

Not sure how to get past this error. Any ideas?

 

Regards,

Dave

 

Jun 2, 2010 at 8:32 PM

As mentioned in the other thread, I modified mvcextensions to use InstancePerLifetimeScope instead of InstancePerMatchingLifetimeScope and all works well.

Hopefully the change won't cause unforseen problems.

 

Regards,

Dave

Coordinator
Jun 3, 2010 at 6:23 PM

Pls check the tunk code, it has been fixed.