1

Note: I have already read this article: Autofac - The request lifetime scope cannot be created because the HttpContext is not available - due to async code?, and it's not duplicate.

As of the recently update, I register a Autofac module for AutoMapper:

protected override void Load(A.ContainerBuilder builder)
{
    var mapper = this.Config.CreateMapper();

    builder.RegisterInstance(this.Config).As<IConfigurationProvider>().SingleInstance();
    builder.RegisterInstance(mapper).SingleInstance();
}

EDIT: According to Travis's comment, I update my current code. All codes are within a Controller Action. This code works well:

var config = DependencyResolver.Current.GetService<IConfigurationProvider>();

However, this does not work:

var mapperConfig = this.MapperConfig;

Here are the definitions:

public class BaseController : Controller
{
    public IConfigurationProvider MapperConfig
    {
        get
        {
            return this.Service<IConfigurationProvider>();
        }
    }
}

public static class ControllerExtension
{

    public static T Service<T>(this Controller controller) where T : class
    {
        return DependencyUtils.Resolve<T>();
    }

}

public static class DependencyUtils
{

    public static T Resolve<T>() where T : class
    {
        return DependencyResolver.Current.GetService<T>();
    }

}

OLD CONTENT:

And I have the following code (I have tried both async/non-async methods):

    var modelProducts = await service.GetActive()
        .ProjectTo<ProductViewModel>(this.MapperConfig)
        .ToListAsync();
    model.AppendLine("Select new products and project into ViewModel async successfully:");

MapperConfig definition:

public MapperConfiguration MapperConfig
{
    get
    {
        return this.Service<MapperConfiguration>();
    }
}

and the Service method:

    public static T Service<T>(this Controller controller) where T : class
    {
        return DependencyResolver.Current.GetService<T>();
    }

This exception is thrown: enter image description here

I am sure that DependencyResolver.Current works well (I also changed to AutofacDependencyResolver.Current, it is the same object), because on the upper lines, I got the IProductService instance successfully with the same resolver (I am using Repository pattern). I am also used the newest package of AutoMapper, Autofac, Entity Framework 6.1 (to support SQL Server 2008). These code works fine, and if I comment out the ProjectTo line, there is no more exception:

    var model = new StringBuilder();

    var service = this.Service<IProductService>();
    model.AppendLine("Service resolved successfully");

    var products = service.Get().ToList();
    model.AppendLine("Get all products successfully");

    products = await service.Get().ToListAsync();
    model.AppendLine("Get all products async successfully");

What did I do wrong?

EDIT: I seperated the calls, so it can be clearer:

    var mapperConfig = this.MapperConfig;
    var modelProducts = await service.GetActive()
        .ProjectTo<ProductViewModel>(mapperConfig)
        .ToListAsync();
    model.AppendLine("Select new products and project into ViewModel async successfully:");

The exception occurs right at the first line, var mapperConfig = this.MapperConfig;, it is not related to the service call.

Community
  • 1
  • 1
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • Whether or not `DependencyResolver.Current` works, the thing that will require there to be a request lifetime is when you call `GetService` on the resolver. It is hard to tell whether the code you're running is inside a controller (if so, which code?) and what order the calls are happening in. If you are in a controller action, try - right in the action - calling `DependencyResolver.Current.GetService()` without all the extra indirection. Does it work? – Travis Illig Feb 16 '16 at 00:19
  • @TravisIllig You're right. Calling that line directly in the Controller Action works. I have no idea why the `HttpContext` when I call the property is `null`. I will update the question accordingly. – Luke Vo Feb 16 '16 at 02:24
  • @TravisIllig I have updated the question for the new code. I really do not understand, the flow is synchronous, but why HttpContext suddenly disappear? I used Mark Object Id in the debugger, `DependencyResolver.Current` in both working and non-working call is the same object. – Luke Vo Feb 16 '16 at 02:30
  • So, to be clear, if you put both of those lines - the one directly calling `DependencyResolver.Current.GetService` and the one calling `this.MapperConfig` - literally right next to each other in the code, one the `DependencyResolver` line succeeds and the `MapperConfig` one fails? – Travis Illig Feb 16 '16 at 02:32
  • 1
    @TravisIllig Ok, I think I found the problem. It's not the dependency error. I don't know why after an `await` operator, my HttpContext is null. If I move both lines to the top, it works fine! So yes, last time I checked I did not put these two lines near each other, my bad. – Luke Vo Feb 16 '16 at 02:39
  • Well, thank you very much. It's funny that adding `` helped! It was indeed a problem with the await/async call. – Luke Vo Feb 16 '16 at 02:46

0 Answers0