today i encountered a strange behavior in my Web Api application
protected void Application_Start() {
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configuration
.MessageHandlers.Add(new DummyMessageHandler());
}
And my DelegatingHandler looks like this.
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);
}
}
The problem I encountered was that the delegating handlers are not being executed. I have a breakpoint in the line marked with a * and the execution of my code never stops there.
My nuget packages.config is the following:
<?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>
I'm looking at this for a long time, can you point me to something I am missing ? Thank you
What you have done is correct. The problem might be happening because the DelegatingHandler
only runs when you call a Web API Controller action. For example:
This will invoke your Message Handler, because it's an ApiController.
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
This will NOT, because it's just a Controller.
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Title = "Home Page";
return View();
}
}
Make sure you are calling a Web API Controller action, otherwise the debugger will not hit your break point.
you should register your handler in the WebApiConfig file, not in the global.asax
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.MessageHandlers.Add( new DummyMessageHandler());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With