0

完整的错误信息:

{
    "Message":"An error has occurred.",
    "ExceptionMessage":"Type 'XXXController' does not have a default constructor",
    "ExceptionType":"System.ArgumentException",
    "StackTrace":"   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\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)"
}

我已经做了一些谷歌搜索,现在我坚持下一步要尝试什么。

我已经安装了一个NinjectDependencyResolver通过:

PM> Install-Package Ninject.WebApi.DependencyResolver

并在方法中使用以下代码TestInitialize来创建HttpClient

 var kernel = new StandardKernel(/* ... */);

var config = new HttpSelfHostConfiguration(BaseAddress);

/* ... */

config.DependencyResolver = new NinjectDependencyResolver(kernel);

_client = new HttpClient(new HttpServer(config));

然后在客户端发出PostAsync请求。结果的内容顶部有错误。

提前致谢!

4

1 回答 1

0

在输入这个问题时,我仔细检查并注意到我没有将我的一个NinjectModule's 传递到StandardKernel. 添加注册了传递给构造函数的类型的模块解决了该异常。如果我缺少一个类型的绑定,我希望它会说别的东西(比如“找不到绑定IMyInterface”)。

于 2013-10-20T02:27:00.717 回答