虽然这里有很多问题围绕着同样的问题而且我正在尝试按照这个帖子多次被告知我无法使其发挥作用
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相同的项目。也许这是一个问题由于
编辑:控制器看起来像这样
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);
}
当它到达返回线
答案 0 :(得分:1)
看起来错误告诉您需要在控制器中创建无参数构造函数,这可能是因为未正确设置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();
}
希望这会有所帮助...