ContentDeliveryApi: System.InvalidOperationException: 'The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized()

Vote:
 

Hello,

I have added ContentDeliveryApi (Cms and Core) to my application, but after that I'm receiving an error:

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

I followed similar topic (https://world.episerver.com/forum/developer-forum/Developer-to-developer/Thread-Container/2017/4/the-object-has-not-yet-been-initialized--ensure-that-httpconfiguration-ensureinitialized/), but none of these solutions did work.

Current configuration

web.config

I have an option to disable MapHttpAttributeRoutes:

<add key="episerver:serviceapi:maphttpattributeroutes" value="false" />

MapHttpAttributeRoutes

I already had one invocation of the MapHttpAttributeRoutes in my application.

That is:

[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class SiteInitialization : IConfigurableModule
{
   // ...
  public void ConfigureContainer(ServiceConfigurationContext context)
  {
    // ...
    GlobalConfiguration.Configure(config =>
    {
    // ...
    config.MapHttpAttributeRoutes();
    });
  }
}

But, after installation of ContentDeliveryApi packages, I'm receiving this error:

A route named 'MS_attributerouteWebApi' is already in the route collection. Route names must be unique.Parameter name: name

So, it seems there's one more invocation of the MapHttpAttributeRoutes in either EPiServer.ContentDeliveryApi.Cms or EPiServer.ContentDeliveryApi.Core.

When I removed the MapHttpAttributeRoutes from my SiteInitialization, I get the original System.InvalidOperationException (the subject of the thread).

EnsureInitialized

I tried to add EnsureInitialized to my Global.asax.cs, but this didn't resolve the problem.

public class Global : EPiServer.Global
{
  // ...
  protected void Application_Start()
  {
    // ...
    WebApiConfig.Register(GlobalConfiguration.Configuration); // this was already here
    GlobalConfiguration.Configuration.EnsureInitialized(); // newly added, but it didn't help
  }
}

Episerver versions

The .NET Framework is 4.8.

  • EPiServer.Azure: 10.0.1
  • EPiServer.CMS: 11.15.0
  • EPiServer.CMS.AspNet: 11.15.0
  • EPiServer.CMS.Core: 11.15.0
  • EPiServer.CMS.TinyMce: 2.10.1
  • EPiServer.CMS.UI: 11.24.1
  • EPiServer.CMS.UI.AspNetIdentity: 11.24.1
  • EPiServer.CMS.UI.Core: 11.24.1
  • EPiServer.Commerce: 13.18.0
  • EPiServer.Commerce.Azure: 14.0.4
  • EPiServer.Commerce.Core: 13.18.0
  • EPiServer.Commerce.UI: 13.18.0
  • EPiServer.Commerce.UI.CustomerService: 13.18.0
  • EPiServer.Commerce.UI.ManagerIntegration: 13.18.0
  • EPiServer.ContentDeliveryApi.Cms: 2.10.0
  • EPiServer.ContentDeliveryApi.Core: 2.10.0
  • EPiServer.Framework: 11.15.0
  • EPiServer.Framework.AspNet: 11.15.0
  • EPiServer.Logging.Log4Net: 2.2.2
  • EPiServer.Personalization: 1.0.0
  • EPiServer.Personalization.Commerce 3.2.12
  • EPiServer.Personalization.Common: 3.1.0
  • EPiServer.Personalization.MaxMindGeolocation: 1.0.0
  • EPiServer.ServiceLocation.StructureMap: 2.0.3
  • EPiServer.Session: 1.0.0
  • EPiServer.Tracking.Commerce 3.2.12
  • EPiServer.Tracking.Core: 1.14.0

Thank you in advance!

#221602
Apr 22, 2020 6:45
Vote:
 

Do you have any GlobalConfiguration.Configure in your code ? 

#221617
Apr 22, 2020 10:07
Vote:
 

@Quan Mai: Yes, single one -- the same as posted above.

#221618
Apr 22, 2020 10:10
Vote:
 

That is probably the problem as ServiceAPI already called it. What do you do in there? Can it be done without using  GlobalConfiguration.Configure?

#221619
Apr 22, 2020 10:20
Vote:
 

@Quan Mai: Thank you for the answer.

When I remove GlogalConfiguration.Configure, the site loads correctly and I'm able to see APIs in Swagger UI.

Now, the question is -- how can I use GlobalConfiguration.Configure now?

#221692
Apr 23, 2020 9:31
Vote:
 

Your application seems to already find the http map.

Since you already have web config entry where you are setting it false. Try removing config.MapHttpAttributeRoutes() and just keep config.Default()

while keeping the globalconfiguration.

#222220
Apr 30, 2020 14:53
Vote:
 

Your suggestions helped me solving the problem, Quan Mai, Dileep D, thank you.

#223193
May 21, 2020 9:25
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.