Umbraco 8 和 IoC
Umbraco 8 and IoC
我安装了 Umbraco 8.1.3,我正尝试在此 post 之后配置依赖注入,但没有用。
我的代码是:
控制器:
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": "An error has occurred.",
"ExceptionMessage": "An error occurred when trying to create a controller of type 'ContactController'. 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.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Unable to resolve type: MySite.Controllers.ContactController, service name: ",
"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 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 at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func1& 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) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4120\r\n at LightInject.ServiceContainer.EmitNewInstanceUsingImplementingType(IEmitter emitter, 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) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3777\r\n at 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 emitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4664\r\n at LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856\r\n at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action1 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 at LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter emitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4028\r\n at LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856\r\n at LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856\r\n at LightInject.ServiceContainer.EmitConstructorDependency(IEmitter emitter, Dependency dependency) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4158"
}
}
有人可以帮我吗?
检查 UmbracoCore 代码后,更准确地说 LightInjectContainer.cs 我在 RegisterFor(Lifetime lifetime = Lifetime.Transient) 方法中发现了问题:
public void RegisterFor<TService, TTarget>(Lifetime lifetime = Lifetime.Transient)
where TService : class
=> RegisterFor<TService, TTarget>(typeof(TService), lifetime);
永远不会使用 TTarget 类型,因此永远不需要实现 class。
为了解决这个问题,我实现了 Umbraco Composition 的扩展方法 class
public static class CompositionExtension
{
public static void Register<TService, TTarget>(this Composition composition, Lifetime lifetime = Lifetime.Transient) where TTarget : class
{
composition.Register(typeof(TService), typeof(TTarget), lifetime);
}
}
那么我的DI Config是:
public class ApplicationComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<IContactService, ContactService>();
}
}
我安装了 Umbraco 8.1.3,我正尝试在此 post 之后配置依赖注入,但没有用。 我的代码是:
控制器:
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": "An error has occurred.", "ExceptionMessage": "An error occurred when trying to create a controller of type 'ContactController'. 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.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()", "InnerException": { "Message": "An error has occurred.", "ExceptionMessage": "Unable to resolve type: MySite.Controllers.ContactController, service name: ", "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 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 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": "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) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4120\r\n at LightInject.ServiceContainer.EmitNewInstanceUsingImplementingType(IEmitter emitter, 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) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3777\r\n at 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 emitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4664\r\n at LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856\r\n at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action1 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 at LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter emitter) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4028\r\n at LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856\r\n at LightInject.ServiceContainer.<>c__DisplayClass153_0.b__0(IEmitter ms) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 3856\r\n at LightInject.ServiceContainer.EmitConstructorDependency(IEmitter emitter, Dependency dependency) in C:\projects\lightinject\src\LightInject\LightInject.cs:line 4158" } }
有人可以帮我吗?
检查 UmbracoCore 代码后,更准确地说 LightInjectContainer.cs 我在 RegisterFor(Lifetime lifetime = Lifetime.Transient) 方法中发现了问题:
public void RegisterFor<TService, TTarget>(Lifetime lifetime = Lifetime.Transient)
where TService : class
=> RegisterFor<TService, TTarget>(typeof(TService), lifetime);
永远不会使用 TTarget 类型,因此永远不需要实现 class。
为了解决这个问题,我实现了 Umbraco Composition 的扩展方法 class
public static class CompositionExtension
{
public static void Register<TService, TTarget>(this Composition composition, Lifetime lifetime = Lifetime.Transient) where TTarget : class
{
composition.Register(typeof(TService), typeof(TTarget), lifetime);
}
}
那么我的DI Config是:
public class ApplicationComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<IContactService, ContactService>();
}
}