Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpControllerContext.Configuration is sometimes null during Content negotiation

We are setting up a ASP.NET 4.6.2 Web Api host and noticing that some requests fail because of the following error:

HttpControllerContext.Configuration must not be null.

I've been unable to reproduce the issue and wanted to ask if anybody could steer me in the right direction?

This is the stack trace:

[0] System.InvalidOperationException "HttpControllerContext.Configuration must not be null."
   at System.Web.Http.Results.NegotiatedContentResult`1.ApiControllerDependencyProvider.EnsureResolved()
   at System.Web.Http.Results.NegotiatedContentResult`1.ApiControllerDependencyProvider.get_ContentNegotiator()
   at System.Web.Http.Results.BadRequestErrorMessageResult.Execute()
   at System.Web.Http.Results.BadRequestErrorMessageResult.ExecuteAsync(CancellationToken cancellationToken)
   at System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()
like image 818
Sigurbjörn Avatar asked Mar 09 '26 19:03

Sigurbjörn


1 Answers

A similar error occurred in my tests when adding headers to a BadRequestErrorMessageResult. The fix only required to initialize the controller's Configuration. ...and subsequently initialize the Request.

So my test's setup ended up looking something like

public class FooControllerTest
{
    private FooController _sut;

    [Setup]
    public void Setup()
    {
        _sut = new FooController();
        _sut.Configuration = new System.Web.Http.HttpConfiguration();
        _sut.Request = new System.Net.Http.HttpRequestMessage();
    }
}

Hope that helps.

like image 161
Erik Lallemand Avatar answered Mar 12 '26 09:03

Erik Lallemand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!