I have set up dependency injection with Autofac
for my umbracp 7.15.3 app based on this example: https://gosudev.com/blog/software-development/umbraco-cms/umbraco-web-api-and-mvc-dependency-injection-with-autofac/
The example works well with the sample controller the same like here: https://github.com/gosudev/UmbracoCMSDIMixedMode/blob/master/src/UmbracoCMSDIMixedMode/UmbracoCMSDIMixedMode.Web/Controllers/Api/SampleWebApiController.cs
I need to use other services and am trying to use IMediator
(MediatR
) so I modified the above controller to:
public class SampleWebApiController : UmbracoApiController
{
private readonly IMediator _mediator;
private readonly ISampleService _sampleService;
public SampleWebApiController(ISampleService sampleService, IMediator mediator)
{
_sampleService = sampleService;
}
//http://localhost:60580/umbraco/api/SampleWebApi/Get
[System.Web.Http.HttpGet]
public JsonResult Get()
{
return new JsonResult() { Data = new { items = _sampleService.GetItems() }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
and WebModule
(https://github.com/gosudev/UmbracoCMSDIMixedMode/blob/master/src/UmbracoCMSDIMixedMode/UmbracoCMSDIMixedMode.Web/WebModule.cs) to:
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<SampleService>().As<ISampleService>();
builder.RegisterType<Mediator>().As<IMediator>();
builder.Register(c => UmbracoContext.Current).AsSelf();
}
At this point the controller stops working and I am getting an error message:
{
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred when trying to create a controller of type 'SampleWebApiController'. 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__15.MoveNext()",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "An exception was thrown while activating MyDomain.Controllers.SampleWebApiController -> MediatR.Mediator.",
"ExceptionType": "Autofac.Core.DependencyResolutionException",
"StackTrace": " at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters, Object& decoratorTarget)\r\n at Autofac.Core.Resolving.InstanceLookup.Execute()\r\n at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, ResolveRequest request)\r\n at Autofac.Core.Resolving.ResolveOperation.Execute(ResolveRequest request)\r\n at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(ResolveRequest request)\r\n at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)\r\n at Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters)\r\n at Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType) in C:\\projects\\autofac-webapi\\src\\Autofac.Integration.WebApi\\AutofacWebApiDependencyScope.cs:line 76\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)",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MediatR.Mediator' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'Microsoft.Practices.ServiceLocation.ServiceLocatorProvider serviceLocatorProvider' of constructor 'Void .ctor(Microsoft.Practices.ServiceLocation.ServiceLocatorProvider)'.",
"ExceptionType": "Autofac.Core.DependencyResolutionException",
"StackTrace": " at Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(ConstructorInfo[] availableConstructors, IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)\r\n at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters, Object& decoratorTarget)"
}
}
}
Why is it wrong and how to inject another service into this and potentially other controller?
I have installed MediatR.Extensions.Autofac.DependencyInjection
and changed this line:
builder.RegisterType<Mediator>().As<IMediator>();
to
builder.AddMediatR(this.GetType().Assembly);
so the configuration is:
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<SampleService>().As<ISampleService>();
builder.AddMediatR(this.GetType().Assembly); //builder.RegisterType<Mediator>().As<IMediator>();
builder.Register(c => UmbracoContext.Current).AsSelf();
}