John S
John S

Reputation: 8351

"Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'

I have written a simple custom Authorize attribute that is applied to several WebAPI methods. When I run in my localhost everything works as expected but when I move the binaries to a true web server I start getting the following error message:

"Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'

If I remove my custom Authorize attribute everything works fine on both servers.

The full stack trace is:

{"Message":"An error has occurred.","ExceptionMessage":"Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()'.","ExceptionType":"System.MissingMethodException","StackTrace":" at MyApp.CustomAuthorizeAttribute.OnAuthorization(HttpActionContext actionContext) at System.Web.Http.Filters.AuthorizationFilterAttribute.OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.AuthorizationFilterAttribute.d__3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()"}

The Custom Attribute (stripped down but behavior is the same) is:

 public class CustomAuthorizeAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(HttpActionContext actionContext)
    {
        try
        {
            var authValue = actionContext.Request.Headers.Authorization;
            return;
        }
}
}

The problem seems to be centered on the HttpActionContext Request property. If I try to get the value from that property it throws the above error. If I remark out that line it runs fine.

Any ideas?

* Additional info * I am able to remove all code from the OnAuthorization method and simply return and it will throw the same error.

* More * The error happens when I try to access the Request method of HttpActionContext.

Upvotes: 0

Views: 1376

Answers (1)

Hien Nguyen
Hien Nguyen

Reputation: 18973

This exception throw because some case your actionContext is null.

Change to

var authValue = actionContext != null ? actionContext.Request.Headers.Authorization : null;

Upvotes: 0

Related Questions