我有一个web api,我使用Ninject作为我的DI容器。我正在使用VisualStudio for Mac。
考虑这样的控制器:
[RoutePrefix("api/Example")]
public class ExampleController : ApiController
{
protected IExampleService Service { get; private set; }
public ExampleController(IExampleService service)
{
Service = service;
}
[HttpGet]
[Route("Get")]
public int Get()
{
try
{
return 1;
}
catch (Exception ex)
{
throw ex;
}
}
}
现在,一旦我点击api /示例/获取它返回" 1"正如所料,但如果我包含一个服务方法调用,如下所示:
[HttpGet]
[Route("Get")]
public async Task<int> GetAsync()
{
try
{
var test = await Service.GetAsync();
return 1;
}
catch (Exception ex)
{
throw ex;
}
}
该方法被称为&#34; test&#34;具有来自服务的正确值,执行返回,然后响应如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body { background-color: #FFFFFF; font-size: .75em; font-family: Verdana, Helvetica, Sans-Serif; margin: 0; padding: 0; color: #696969; }
a:link { color: #000000; text-decoration: underline; }
a:visited { color: #000000; }
a:hover { color: #000000; text-decoration: none; }
a:active { color: #12eb87; }
p, ul { margin-bottom: 20px; line-height: 1.6em; }
pre { font-size: 1.2em; margin-left: 20px; margin-top: 0px; }
h1, h2, h3, h4, h5, h6 { font-size: 1.6em; color: #000; font-family: Arial, Helvetica, sans-serif; }
h1 { font-weight: bold; margin-bottom: 0; margin-top: 0; padding-bottom: 0; }
h2 { font-size: 1em; padding: 0 0 0px 0; color: #696969; font-weight: normal; margin-top: 0; margin-bottom: 20px; }
h2.exceptionMessage { white-space: pre; }
h3 { font-size: 1.2em; }
h4 { font-size: 1.1em; }
h5, h6 { font-size: 1em; }
#header { position: relative; margin-bottom: 0px; color: #000; padding: 0; background-color: #5c87b2; height: 38px; padding-left: 10px; }
#header h1 { font-weight: bold; padding: 5px 0; margin: 0; color: #fff; border: none; line-height: 2em; font-family: Arial, Helvetica, sans-serif; font-size: 32px !important; }
#header-image { float: left; padding: 3px; margin-left: 1px; margin-right: 1px; }
#header-text { color: #fff; font-size: 1.4em; line-height: 38px; font-weight: bold; }
#main { padding: 20px 20px 15px 20px; background-color: #fff; _height: 1px; }
#footer { color: #999; padding: 5px 0; text-align: left; line-height: normal; margin: 20px 0px 0px 0px; font-size: .9em; border-top: solid 1px #5C87B2; }
#footer-powered-by { float: right; }
.details { font-family: monospace; border: solid 1px #e8eef4; white-space: pre; font-size: 1.2em; overflow: auto; padding: 6px; margin-top: 6px; background-color: #eeeeff; color: 555555 }
.details-wrapped { white-space: normal }
.details-header { margin-top: 1.5em }
.details-header a { font-weight: bold; text-decoration: none }
p { margin-bottom: 0.3em; margin-top: 0.1em }
.sourceErrorLine { color: #770000; font-weight: bold; }
</style>
<script type="text/javascript">
var hideElementsById = new Array ();
window.onload = function () {
if (!hideElementsById || hideElementsById.length < 1)
return;
for (index in hideElementsById)
toggle (hideElementsById [index]);
}
function toggle (divId)
{
var e = document.getElementById (divId);
if (!e)
return;
var h = document.getElementById (divId + "Hint");
if (e.style.display == "block" || e.style.display == "") {
e.style.display = "none";
if (h)
h.innerHTML = " (click to show)";
} else {
e.style.display = "block";
if (h)
h.innerHTML = " (click to hide)";
}
}
</script>
<title>Error 500</title>
</head>
<body>
<div class="page">
<div id="header">
<div id="header-text">Application Exception</div>
</div>
<div id="main">
<h1>System.ArgumentNullException</h1>
<h2 class="exceptionMessage">Value cannot be null.
Parameter name: key</h2>
<p><strong>Description:</strong> HTTP 500.Error processing request.</p><p><strong>Details:</strong> Non-web exception. Exception origin (name of application or object): mscorlib.</p>
<div><strong>Exception stack trace:</strong></div>
<div class="details"> at System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) [0x00008] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs:363
at System.Collections.Generic.Dictionary`2[TKey,TValue].TryGetValue (TKey key, TValue& value) [0x00000] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs:629
at Ninject.Activation.Caching.Cache.Clear (System.Object scope) [0x00011] in <3c265eddca574f5695400bbff61c5b67>:0
at Ninject.Web.Common.OnePerRequestHttpModule+<>c__DisplayClass2.<DeactivateInstancesForCurrentHttpRequest>b__1 (Ninject.IKernel kernel) [0x0000b] in <fc18c9997ab44d4fa75701ae5062feb0>:0
at Ninject.GlobalKernelRegistration.MapKernels (System.Action`1[T] action) [0x00040] in <3c265eddca574f5695400bbff61c5b67>:0
at Ninject.Web.Common.OnePerRequestHttpModule.DeactivateInstancesForCurrentHttpRequest () [0x00019] in <fc18c9997ab44d4fa75701ae5062feb0>:0
at Ninject.Web.Common.OnePerRequestHttpModule.<Init>b__0 (System.Object o, System.EventArgs e) [0x00000] in <fc18c9997ab44d4fa75701ae5062feb0>:0
at (wrapper delegate-invoke) <Module>:invoke_void_object_EventArgs (object,System.EventArgs)
at System.Web.HttpApplication.PipelineDone () [0x00019] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/HttpApplication.cs:1096 </div><div id="footer">
<div style="color:Black;"><strong>Version Information:</strong> <tt>5.4.1.7 (2017-06/e66d9abbb27 Wed Oct 25 12:10:41 EDT 2017)</tt>; ASP.NET Version: <tt>4.0.30319.42000</tt></div>
<div id="footer-powered-by">Powered by <a href="http://mono-project.com/">Mono</a></div>
</div>
</div>
</div>
</body>
</html>
<!--
[System.ArgumentNullException]: Value cannot be null.
Parameter name: key
at System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) [0x00008] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs:363
at System.Collections.Generic.Dictionary`2[TKey,TValue].TryGetValue (TKey key, TValue& value) [0x00000] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs:629
at Ninject.Activation.Caching.Cache.Clear (System.Object scope) [0x00011] in <3c265eddca574f5695400bbff61c5b67>:0
at Ninject.Web.Common.OnePerRequestHttpModule+<>c__DisplayClass2.<DeactivateInstancesForCurrentHttpRequest>b__1 (Ninject.IKernel kernel) [0x0000b] in <fc18c9997ab44d4fa75701ae5062feb0>:0
at Ninject.GlobalKernelRegistration.MapKernels (System.Action`1[T] action) [0x00040] in <3c265eddca574f5695400bbff61c5b67>:0
at Ninject.Web.Common.OnePerRequestHttpModule.DeactivateInstancesForCurrentHttpRequest () [0x00019] in <fc18c9997ab44d4fa75701ae5062feb0>:0
at Ninject.Web.Common.OnePerRequestHttpModule.<Init>b__0 (System.Object o, System.EventArgs e) [0x00000] in <fc18c9997ab44d4fa75701ae5062feb0>:0
at (wrapper delegate-invoke) <Module>:invoke_void_object_EventArgs (object,System.EventArgs)
at System.Web.HttpApplication.PipelineDone () [0x00019] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/System.Web/System.Web/HttpApplication.cs:1096
-->
&#13;
以下是包裹:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="6.2.2" targetFramework="net461" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="Ninject" version="3.2.0.0" targetFramework="net461" />
<package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net461" />
<package id="Ninject.Web.Common.WebHost" version="3.2.0.0" targetFramework="net461" />
<package id="Ninject.Web.WebApi" version="3.2.0.0" targetFramework="net461" />
<package id="WebActivatorEx" version="2.0" targetFramework="net461" />
</packages>
我没有运气就尝试了各种套餐版本。另外,我创建了MVC项目来预先测试一切,MVC项目工作正常(没问题)