Reputation: 4053
We have ASP.NET application where we use Autofac. The following error comes up:
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ConcertRu.ApplicationsCore.Services.SessionWebAppService' can be invoked with the available services and parameters:
Cannot resolve parameter 'System.Web.HttpContext httpContext' of constructor 'Void .ctor(System.Web.HttpContext)'.
Cannot resolve parameter 'System.Web.HttpContextBase httpContext' of constructor 'Void .ctor(System.Web.HttpContextBase)'.
Cannot resolve parameter 'ConcertRu.Infrastructure.Contracts.IConcertHttpContext httpContext' of constructor 'Void .ctor(ConcertRu.Model.Contracts.IConcertDb, ConcertRu.Infrastructure.Contracts.IConcertHttpContext)'.
Global.asax.cs:
// Create the container builder.
var builder = new ContainerBuilder();
// Register the Web API controllers.
builder.RegisterControllers(Assembly.GetExecutingAssembly());
// Register other dependencies.
var services = typeof(AccessTokenService).Assembly;
builder.Register(c => ConcertDb.Current).As<IConcertDb>().SingleInstance();
builder.RegisterAssemblyTypes(services)
.Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces()
.SingleInstance();
// Build the container.
var container = builder.Build();
// Create the depenedency resolver.
var resolver = new AutofacDependencyResolver(container);
// Configure Web API with the dependency resolver.
DependencyResolver.SetResolver(resolver);
The SessionWebAppService class:
public class SessionWebAppService : WebAppServiceBase, ISessionWebAppService
{
public SessionWebAppService(HttpContext httpContext)
: this(new ConcertDb(), new WebFormContext(httpContext))
{
}
public SessionWebAppService(HttpContextBase httpContext)
: this(new ConcertDb(), new MvcContext(httpContext))
{
}
public SessionWebAppService(IConcertDb concertDb, IConcertHttpContext httpContext)
: base(concertDb, httpContext)
{
}
...
}
The WebAppServiceBase class:
public abstract class WebAppServiceBase : ModelServiceBase
{
private readonly IConcertHttpContext _httpContext;
protected WebAppServiceBase(IConcertDb concertDb, IConcertHttpContext httpContext) : base(concertDb)
{
_httpContext = httpContext;
}
...
}
Upvotes: 0
Views: 797
Reputation: 33910
Autofac states quite clearly what is wrong: neither HttpContext
, HttpContextBase
or IConcertHttpContext
can be resolved from the container. Seeing your registration code, this makes sense because only controllers, your IConcertDb
an services are registered.
Upvotes: 1