Fix: Ensure HttpConfiguration.EnsureInitialized() is called


Story by Abhilash on Umbraco

Share   

Last day, we integrated OWIN into Umbraco v8 website as part of implementing authentication. After implementing the authentication mechanism, the website started throwing error as below : 

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.",
  "ExceptionType": "System.InvalidOperationException",
  "StackTrace": "   at System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes()\r\n   at System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request)\r\n   at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)"
}

 The error says  

"The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code."

If you search for solutions for the error, then you'll be taken to a wild ride with respect to various solutions you see in the internet. Most probably, you'll be trying to rearrange the order of defining codes in your WebApiConfig or AppStartConfig. For me, none of the solution worked! The situation was slightly different and the issue was related to OWIN. 

This is how I fixed it, by invoking the Initializer() of HttpConfiguration, as shown below: 

[assembly: OwinStartup("MyOwinStartup", typeof(MyNamespace.App_Start.OwinStartup))]

namespace MyNamespace.App_Start
{
    public class OwinStartup : UmbracoDefaultOwinStartup
    {
        public override void Configuration(IAppBuilder app)
        {
            base.Configuration(app);
            
		//...
		//...
		//...
			
            HttpConfiguration configuration = GlobalConfiguration.Configuration;

            	//...
		//...
		//...

            configuration.Initializer(configuration);	// last line
        }
    }
}

 Hope this helps.





comments powered by Disqus

Related Story   →

Fix: HTML5 Video not playing when deployed to azure

A post on how to playback HTML5 video when published to Azure.