Null Reference with a combination of ODataController and RegisterWebApiFilterProvider when starting the app in the Owin pipeline.
tillig opened this issue · 1 comments
From @SpiegelSoft on January 24, 2015 19:3
This is similar to Issue autofac/Autofac#561, but I think I have an accurate way to reproduce it.
Using Autofac.WebApi2, Version 3.4.0.
Because of the rules around SignalR integration, it is recommended to set up the container in the Owin startup pipeline.
Create a new web project, and add Microsoft.Owin.Host.SystemWeb.
Add a new file, called Startup.cs, containing the following content:
using System.Reflection;
using System.Web.Http;
using Autofac;
using Autofac.Integration.WebApi;
using Microsoft.Owin;
using Owin;
using SpiegelSoftUsers;
[assembly: OwinStartup(typeof(Startup))]
namespace MyWebApp
{
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var config = GlobalConfiguration.Configuration;
containerBuilder.RegisterWebApiFilterProvider(config);
var container = containerBuilder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
}
Run the web app, and navigate to /odata.
The result is
{
"error":{
"code":"","message":"An error has occurred.","innererror":{
"message":"Object reference not set to an instance of an object.","type":"System.NullReferenceException","stacktrace":" at Autofac.Integration.WebApi.AutofacWebApiFilterProvider.GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)\r\n at System.Web.Http.Controllers.HttpActionDescriptor.b__0(IFilterProvider fp)\r\n at System.Linq.Enumerable.d__142.MoveNext()\r\n at System.Linq.Buffer
1..ctor(IEnumerable1 source)\r\n at System.Linq.OrderedEnumerable
1.d__0.MoveNext()\r\n at System.Linq.Buffer1..ctor(IEnumerable
1 source)\r\n at System.Linq.Enumerable.d__a01.MoveNext()\r\n at System.Web.Http.Controllers.HttpActionDescriptor.<RemoveDuplicates>d__3.MoveNext()\r\n at System.Linq.Buffer
1..ctor(IEnumerable1 source)\r\n at System.Linq.Enumerable.<ReverseIterator>d__a0
1.MoveNext()\r\n at System.Collections.Generic.List1..ctor(IEnumerable
1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)\r\n at System.Web.Http.Controllers.HttpActionDescriptor.InitializeFilterPipeline()\r\n at System.Lazy
1.CreateValue()\r\n at System.Lazy1.LazyInitValue()\r\n at System.Lazy
1.get_Value()\r\n at System.Web.Http.Controllers.HttpActionDescriptor.GetFilterPipeline()\r\n at System.Web.Http.Controllers.HttpActionDescriptor.GetFilterGrouping()\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"
}
}
}
If you move all of that setup code into WebApiConfig, the error goes away.
Copied from original issue: autofac/Autofac#617
This is like a year old, and I'm sorry it's taken so long to look into it.
Given the setup, I see the use of GlobalConfiguration.Configuration
when trying to run things in OWIN. That's a pretty common error and is documented here
If the code otherwise works but fails when switching to a more pure-OWIN situation, I'd guess it has to do with that.
I'm going to close this for now, but if you find that fixing that up doesn't solve the problem, come back and we'll continue looking. I'll probably need a repro if that's the case.