The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized...

Vote:
 

Hi!

After doing a test upgrade to the very latest EPiServer version, along with 'EPiServer.Marketing.Testing 2.2.3', I'm now facing the following error when calling my own Web API:

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

Does anyone have a way of resolving this issue?

The start of my Global.asax.cs looks like this:

public class Global : EPiServer.Global
    {
        protected void Application_Start()
        {
			// Enabling bundling and minification
			BundleConfig.RegisterBundles(BundleTable.Bundles);

			// Enable Web API routing
			GlobalConfiguration.Configure(config =>
			{
				// Activate CORS
				config.EnableCors();

				// Attribute routing
				config.MapHttpAttributeRoutes();

				// Convention-based routing
				config.Routes.MapHttpRoute(
					name: "ActionApi",
					routeTemplate: "webapi/{controller}/{action}/{id}",
					defaults: new { id = System.Web.Http.RouteParameter.Optional }
				);
			});

I've tried adding GlobalConfiguration.Configuration.EnsureInitialized(); to a couple of places but it still won't work.

Thanks in advance!

#178045
Edited, Apr 27, 2017 16:09
Vote:
 

I think this is missing from your

Application_Start

GlobalConfiguration.Configure(WebApiConfig.Register);

#178064
Apr 28, 2017 11:15
Vote:
 

If I try

GlobalConfiguration.Configure(WebApiConfig.Register);

it means I have to move the configuration code to its own class, right?

I tried this:

public static class WebApiConfig
	{
		public static void Register(HttpConfiguration config)
		{
			// Activate CORS
			config.EnableCors();

			// Attribute routing
			config.MapHttpAttributeRoutes();

			// Convention-based routing
			config.Routes.MapHttpRoute(
				name: "...",
				routeTemplate: "webapi/{controller}/{action}/{id}",
				defaults: new { id = System.Web.Http.RouteParameter.Optional }
			);
		}
	}

And adding

GlobalConfiguration.Configure(WebApiConfig.Register);

But it's still not working.

Seems weird that my own WebAPI stops working just because I upgrade EPiServer, adding 'EPiServer.Marketing.Testing 2.2.3''.

#178065
Apr 28, 2017 11:39
Vote:
 

Without trying, I think it should look like this 

// Enable Web API routing
			GlobalConfiguration.Configure(config =>
			{
                                WebApiConfig.Register();

				// Activate CORS
				config.EnableCors();

				// Attribute routing
				config.MapHttpAttributeRoutes();

				// Convention-based routing
				config.Routes.MapHttpRoute(
					name: "ActionApi",
					routeTemplate: "webapi/{controller}/{action}/{id}",
					defaults: new { id = System.Web.Http.RouteParameter.Optional }
				);
			});
#178079
Edited, Apr 28, 2017 15:25
Vote:
 
<p>But won't the code<br /><br />WebApiConfig.Register(...)<br /><br />do the same thing as the rest of the code below it? Registering the same stuff twice will result in an exception.</p>
#178112
May 02, 2017 10:26
Vote:
 

I managed to solve it by using the following code:

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Framework.FrameworkInitialization))]
public class MyInitializationModule : IInitializableModule
{
	public void Initialize(InitializationEngine context)
	{
		// Enable Web API routing
		GlobalConfiguration.Configure(config =>
		{
			// Activate CORS
			config.EnableCors();

			// Attribute routing
			config.MapHttpAttributeRoutes();

			// Convention-based routing
			config.Routes.MapHttpRoute(
				name: "ActionApi",
				routeTemplate: "webapi/{controller}/{action}/{id}",
				defaults: new { id = System.Web.Http.RouteParameter.Optional }
			);
		});
	}
}
#178125
May 02, 2017 15:43
Vote:
 
<p>I've implemnted the above method but get this error:</p> <p></p> <h2>A route named 'MS_attributerouteWebApi' is already in the route collection. Route names must be unique.<br />Parameter name: name</h2> <p>we already have a&nbsp;WebApiConfiguration</p> <p></p> <pre class="brush:csharp;auto-links:false;toolbar:false" contenteditable="false"> public static class WebApiConfiguration { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( "DefaultApi", "api/{controller}/{id}", new {id = RouteParameter.Optional} ); config.EnableCors(new CorsAccessPolicy()); config.EnsureInitialized(); var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t =&gt; t.MediaType == "application/xml"); config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType); }</pre> <h2>and also a&nbsp;WebApiConfig&nbsp;</h2> <pre class="brush:html;auto-links:false;toolbar:false" contenteditable="false"> public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // This handler is for cors // removing CORS can be controlled from Azure //config.MessageHandlers.Add(new CrossDomainHandler()); config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler()); config.Routes.MapHttpRoute( name: "WebApiRoute", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); </pre> <p>I tried several time by removing MapHttpRout statement from one and the both files but still have the same issue.</p> <p>We use Episerver &nbsp;ServiceAPI in our project as well</p>
#182924
Oct 02, 2017 7:26
Vote:
 

Try removing  config.MapHttpAttributeRoutes(); in your WebApiConfiguration.Register - ServiceAPI already calls this 

#182932
Oct 02, 2017 8:38
Vote:
 

I had removed MapHttpAttributeRoutes as well but got the following message even when I add 

 

 config.EnsureInitialized();

to the config file and initialize module

The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is 
called in the application's startup code after all other initialization code.
#182987
Oct 03, 2017 2:24
Vote:
 

I already add this line to Application_Start , but got same message: 

      GlobalConfiguration.Configuration.EnsureInitialized();

#182992
Oct 03, 2017 8:00
Vote:
 

Adding this line to web.config fix the problem: 

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

#183523
Oct 17, 2017 5:00
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.