gpt4 book ai didi

dependency-injection - Umbraco 8 和 IoC

转载 作者:行者123 更新时间:2023-12-03 23:10:48 31 4
gpt4 key购买 nike

我安装了 Umbraco 8.1.3 并尝试在此 post 之后配置依赖注入(inject),但不起作用。
我的代码是:

Controller :

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(Action1 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.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)\r\n at LightInject.ServiceContainer.EmitLifetime(ServiceRegistration serviceRegistration, Action1 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 类型,则从不需要实现类。

为了解决这个问题,我实现了 Umbraco Composition 类的扩展方法
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 配置是:
public class ApplicationComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<IContactService, ContactService>();
}
}

关于dependency-injection - Umbraco 8 和 IoC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57739734/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com