今天我在我的 Web Api 应用程序中遇到了一个奇怪的行为
protected void Application_Start() {
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configuration
.MessageHandlers.Add(new DummyMessageHandler());
}
我的 DelegatingHandler 看起来像这样。
public class DummyMessageHandler : DelegatingHandler {
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken) {
* if (request.Headers.Authorization.Scheme == "Basic")
Thread.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity("Authenticated"), new string[0]);
return base.SendAsync(request, cancellationToken);
}
}
我遇到的问题是委托处理程序没有被执行。我在标有 * 的行中有一个断点,并且我的代码的执行永远不会停在那里。
我的 nuget packages.config 如下:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Razor" version="2.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.Client" version="4.1.0-alpha-120809" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net40" />
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="4.5.8" targetFramework="net40" />
<package id="WebGrease" version="1.1.0" targetFramework="net40" />
</packages>
我看这个很久了,你能指出我缺少的东西吗?谢谢