I am trying to resolve an implementation via constructor injection, however keep getting the following:
An error occurred when trying to create a controller of type 'ApiController'. Make sure that the controller has a parameterless public constructor.
We have a mix application which is running sitecore MVC and WebApi side by side in 8.2.
- MVC resolves dependencies just fine
- WebApi fails to do so however
An example of our InitializeAutofacControllerFactory
:
public virtual void Process(PipelineArgs args)
{
var builder = new ContainerBuilder();
// Register All Controllers In The Current Scope, for WebApi
builder.RegisterApiControllers(AppDomain.CurrentDomain.GetAssemblies());
// Register Modules
builder.RegisterModule<ServicesModule>();
// Register all modules in the data access project
builder.RegisterAssemblyModules(typeof(DataAccess.Installer).Assembly);
// Build The Container
var container = builder.Build();
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
WebApi controller, where resolution is failing:
public class ApiController : ServicesApiController
{
private readonly IEntityAccess _entityAccess;
public ApiController(IEntityAccess entityAccess)
{
_entityAccess = entityAccess;
}
[HttpGet]
public IHttpActionResult Create(string title = null, string description = null)
{
// we do some stuff here
}
}
Stacktrace
{
"Message":"An error has occurred.",
"ExceptionMessage":
"An error occurred when trying to create a controller of type 'ApiController'. Make sure that the controller has a parameterless public constructor.",
"ExceptionType":"System.InvalidOperationException",
"StackTrace":
"at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
"InnerException":{"Message":"An error has occurred.",
"ExceptionMessage":
"Type 'NameSpace.ApiController' does not have a default constructor",
"ExceptionType":"System.ArgumentException","StackTrace":
" at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}
}
We've been following this implementation: https://github.com/HedgehogDevelopment/sitecore-chained-dependency-resolver/tree/feature-autofac
Patch
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<initialize>
<processor type=" NameSpace.Ioc.Pipelines.InitializeAutofacControllerFactory, NameSpace.Ioc"
patch:before="*[@type='Sitecore.Mvc.Pipelines.Loader.InitializeControllerFactory, Sitecore.Mvc']" />
</initialize>
</pipelines>
</sitecore>
</configuration>
OWIN
Not sure if this is causing it but it has also come to my attention, that we have OWIN setup following similar approach found on: https://blog.baslijten.com/how-to-add-federated-authentication-with-sitecore-and-owin/ to set up OWIN.
Trying to get an instance of IAppBuilder in the sitecore pipeline now to make the calls AutoFac recommend for using AutoFac with Owin and WebApi.
InitializeAutofacControllerFactory
in the Sitecore pipeline?IEntityAccess
interface? If so, put a breakpoint in theCreate
method of yourApiController
and check thatGlobalConfiguration.Configuration.DependencyResolver
is indeed set toAutofacWebApiDependencyResolver
.