gpt4 book ai didi

c# - 仍然在 web api 中注入(inject)

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:07 25 4
gpt4 key购买 nike

虽然这里有很多关于相同的问题,但我正在尝试按照已多次建议的帖子进行操作,但我无法使其正常工作

http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/

我遇到了这个错误

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
An error occurred when trying to create a controller of type 'ProfileController'. Make sure that the controller has a parameterless public constructor.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Tracing.Tracers.HttpControllerActivatorTracer.<>c__DisplayClass2.<System.Web.Http.Dispatcher.IHttpControllerActivator.Create>b__0() at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace) at System.Web.Http.Tracing.Tracers.HttpControllerActivatorTracer.System.Web.Http.Dispatcher.IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.<>c__DisplayClass2.<CreateController>b__0() at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace) at System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'LoanBook.Api.Controllers.ProfileController' does not have a default constructor
</ExceptionMessage>
<ExceptionType>System.ArgumentException</ExceptionType>
<StackTrace>
at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
</StackTrace>
</InnerException>
</Error>

我不知道这篇文章是否仍然相关,或者是否有其他方法可以使它发挥作用。我正在使用 2012,但在 4.5 中使用 web api2。 Ninject.Web.WebApi 包刚刚发布了不稳定的版本,所以我不确定它是否是一个选项。

编辑:顺便说一句,该解决方案与 MVC 解决方案不在同一个项目中。也许这是个问题

谢谢

编辑: Controller 看起来像这样

public class ProfileController : BaseController
{
private readonly IPersonService _personService;

public ProfileController(IPersonService personService)
{
_personService = personService;
}

//.. Methods

}

错误在我提到的帖子中显示的 NinjectDependencyResolver 中,在这一点中

public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");

return resolver.GetAll(serviceType);
}

当它到达返回线时

最佳答案

看起来错误告诉您需要在 Controller 中创建无参数构造函数,这可能是由于 ninject RegisterServices() 设置不正确。

您需要以下内容来对错误进行排序:

public ProfileController () { }

因此,我假设您拥有所需的 ioc 构造函数:

public ProfileController (IPersonService personService) 
{
_personService = personService;
}

[edit] - 所以我希望您的 NinjectWebCommon.cs(位于 App_Start)包含以下内容以使一切正常:

private static void RegisterServices(IKernel kernel)
{
/* other kernel.bind<>() setups */
// assumes that your concrete class implementation
// of IPersonService is called PersonService
kernel.Bind<IPersonService>().To<PersonService>().InRequestScope();
}

希望这有助于...

关于c# - 仍然在 web api 中注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20140585/

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