我安装了 Umbraco 8.1.3,我正在尝试在这篇文章之后配置依赖注入,但不起作用。我的代码是:
控制器:
public class ContactController : UmbracoApiController
{
private readonly IContactService contactService;
public ContactController(IContactService contactService)
{
this.contactService = contactService;
}
[HttpPost]
public void Post([FromBody]ContactDto request)
{
this.contactService.Register(request);
}
}
服务:
public interface IContactService
{
void Register(ContactDto contact);
}
public class ContactService : IContactService
{
public ContactService()
{
}
public void Register(ContactDto contact)
{
//Implement
}
}
DI配置:
public class ApplicationComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.RegisterFor<IContactService, ContactService>();
}
}
错误是:
“Message”:“发生错误。”,“ExceptionMessage”:“尝试创建'ContactController'类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。”,“ExceptionType”:“ System.InvalidOperationException", "StackTrace": " 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,类型 controllerType)\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext ()", "InnerException": { "Message": "发生错误。", "ExceptionMessage": "无法解析类型:MySite.Controllers.ContactController,服务名称:", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4748\r \n 在 LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError) 在 C:\projects\lightinject\src\LightInject\LightInject.cs:line 4705\r\n 在 LightInject.ServiceContainer.TryGetInstance(Type serviceType) 在 C: \projects\lightinject\src\LightInject\LightInject.cs:line 3493\r\n 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,类型 controllerType,FuncCreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4748\r\n at LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError) in C:\ projects\lightinject\src\LightInject\LightInject.cs:line 4705\r\n at LightInject.ServiceContainer.TryGetInstance(Type serviceType) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3493\r\n在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,类型 controllerType,FuncCreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4748\r\n at LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError) in C:\ projects\lightinject\src\LightInject\LightInject.cs:line 4705\r\n at LightInject.ServiceContainer.TryGetInstance(Type serviceType) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3493\r\n在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,类型 controllerType,Func\projects\lightinject\src\LightInject\LightInject.cs:line 4705\r\n at LightInject.ServiceContainer.TryGetInstance(Type serviceType) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3493\r\ n 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,类型 controllerType,Func\projects\lightinject\src\LightInject\LightInject.cs:line 4705\r\n at LightInject.ServiceContainer.TryGetInstance(Type serviceType) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3493\r\ n 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,类型 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": "Unresolved dependency [Target Type: MySite.Controllers.ContactController], [Parameter: contactService(IServices.Contacts.IContactService)], [Requested dependency: ServiceType:IServices.Contacts.IContactService, ServiceName:]", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at LightInject.ServiceContainer.EmitConstructorDependency(IEmitter emitter, Dependency dependency) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4163\r\n at LightInject.ServiceContainer.EmitConstructorDependencies(ConstructionInfo constructionInfo, IEmitter emitter, Action
1 decoratorTargetEmitter) 在 C:\projects\lightinject\src\LightInject\LightInject.cs:line 4120\r\n 在 LightInject.ServiceContainer.EmitNewInstanceUsingImplementingType(IEmitter 发射器, ConstructionInfo constructionInfo, Action1 decoratorTargetEmitMethod) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4081\r\n at LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter emitter) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4038\r\n at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action
1 serviceEmitter) 在 C:\projects\lightinject\ src\LightInject\LightInject.cs:C:\projects\lightinject\src\LightInject\LightInject 中 LightInject.ServiceContainer.CreateInstanceDelegateIndex(Action1 emitMethod) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4693\r\n at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func2 valueFactory)\r\n at LightInject.ServiceContainer.EmitLifetime(ServiceRegistration serviceRegistration, Action
1 emitMethod, IEmitter 发射器) 的第 3777 行\r\n。 c:\projects\lightinject\src\LightInject\LightInject.cs:LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms) 中的第 4664 行\r\n:LightInject.ServiceContainer.CreateDynamicMethodDelegate 中的第 3856 行\r\n (行动1 serviceEmitter) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 3777\r\n at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 4743", "InnerException": { "Message": "An error has occurred.", "ExceptionMessage": "Missing public constructor for Type: IServices.Contacts.IContactService", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at LightInject.MostResolvableConstructorSelector.Execute(Type implementingType) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 5558\r\n at LightInject.TypeConstructionInfoBuilder.Execute(Registration registration) in C:\\projects\\lightinject\\src\\LightInject\\LightInject.cs:line 5719\r\n at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func`2 valueFactory)\r\n 在 C:\projects\lightinject\src\LightInject\LightInject.cs:line 4028 中的 LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter 发射器)\r\ n 在 LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms) 在 C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856\r\n 在 LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms ) 在 C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856\r\n 在 C:\projects\lightinject\src\LightInject\LightInject 中的 LightInject.ServiceContainer.EmitConstructorDependency(IEmitter 发射器,依赖依赖项)。 cs:第 4158 行" } }
有谁能够帮我?