Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WEB API calls giving 500 error after upgrading to sitecore 8

Recently, we upgraded to Sitecore 8.0 from Sitecore 7.5. After the upgrade, the Custom Web API calls began returning 500 error codes with the below exception:

Message:"An error has occurred."
ExceptionMessage:"ValueFactory attempted to access the Value property of this instance."
ExceptionType:"System.InvalidOperationException"
StackTrace:"   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping()
   at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider)
   at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<>c__DisplayClass4.<MapAttributeRoutes>b__1()
   at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer)
   at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<MapAttributeRoutes>b__0(HttpConfiguration config)
   at System.Web.Http.CorsHttpConfigurationExtensions.<>c__DisplayClass3.<AddCorsMessageHandler>b__0(HttpConfiguration config)
   at System.Web.Http.HttpConfiguration.ApplyControllerSettings(HttpControllerSettings settings, HttpConfiguration configuration)
   at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)
   at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping()
   at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider)
   at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<>c__DisplayClass4.<MapAttributeRoutes>b__1()
   at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer)
   at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<MapAttributeRoutes>b__0(HttpConfiguration config)
   at System.Web.Http.CorsHttpConfigurationExtensions.<>c__DisplayClass3.<AddCorsMessageHandler>b__0(HttpConfiguration config)
   at System.Web.Http.HttpConfiguration.ApplyControllerSettings(HttpControllerSettings settings, HttpConfiguration configuration)
   at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)
   at Sitecore.Services.Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.InitializeControllerDictionary()
   at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
   at System.Lazy`1.get_Value()
   at Sitecore.Services.Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.SelectController(HttpRequestMessage request)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"

Routes are registered in global.asax.cs:

protected void Application_Start(object sender, EventArgs e)
        {
            GlobalConfiguration.Configure(config =>
            {
                config.MapHttpAttributeRoutes();

                //config.Routes.MapHttpRoute(
                //    name: "DefaultApi",
                //    routeTemplate: "api/{controller}/{id}",
                //    defaults: new { id = RouteParameter.Optional }
                //);
            });
            SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
        }
like image 348
Kirtan Bhatt Avatar asked Oct 23 '25 02:10

Kirtan Bhatt


1 Answers

This solution worked for us when attempting the upgrade. You should read the post in full to understand what it's doing but in short you will need to create a custom HttpControllerSelector and swap it out with Sitecore's after Sitecore has added all of the routes. Note: None of this summary is originally mine. I gleaned all of this from the linked article.

Just as a side note I highly recommend avoiding an in place upgrade to Sitecore 8. I would say it's almost always a better bet to use a fresh install of Sitecore 8 and then migrate your content via a package. This blog is a great look at the two options.

like image 148
Casey Avatar answered Oct 26 '25 20:10

Casey