完整的错误信息:
{
"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
请求。结果的内容顶部有错误。
提前致谢!