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>();
}
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.