volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More. 

volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More. 

Cant get WebAPI to work + The object has not yet been initialized

Hey Guys,

I have the lastest EPI + Commerce + Epi Find installed. Everything works. 

And i just wanted to configure WebAPi.

But i get the following error. "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."

I've tried several things but either i get this or i get a 404 on the API call.  (http://localhost/api/v1/status)

My code setup now. 

TestController:

using EPiServer.Logging.Compatibility;
using System.Web.Http;

namespace Hyva.Web.Api.V1.Controllers
{
    [RoutePrefix("api/v1")] // Route prefix for this controller, with versioning
    public class TestController : ApiController
    {
        private static readonly ILog log = LogManager.GetLogger("TestController");

        [Route("status")]
        [HttpGet]
        //[EnableCorsByAppSetting]
        public IHttpActionResult Status()
        {   
            return Ok("Hello!");       
        }       
    }
}

My webapiconfig.cs class

using System.Net.Http.Headers;
using System.Web.Http;

namespace Hyva.Web.Api.Config
{
    public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.EnableCors();
            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

            config.Routes.MapHttpRoute(
                name: "DefaultEpiApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

           
        }
    }
}

My initialization class:

using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using Hyva.Web.Api.Config;
using System.Web.Http;

namespace Hyva.Web.Implementation.Initialization
{
    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class RoutingInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            //Add initialization logic, this method is called once after CMS has been initialized
            
            WebApiConfig.Register(GlobalConfiguration.Configuration);           
        }

        public void Preload(string[] parameters) { }

        public void Uninitialize(InitializationEngine context)
        {
            //Add uninitialization logic
        }
    }
}

And ofcourse i call my api by the url : localhost/api/v1/status

This gives me 

{
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() at System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)"
}

I've read some posts about changing the INitialization module to this

using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace Hyva.Web.Implementation.Initialization
{
    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Framework.FrameworkInitialization))]
    public class TestAttributeRouting : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            GlobalConfiguration.Configuration.MapHttpAttributeRoutes();

        }

        public void Preload(string[] parameters) { }

        public void Uninitialize(InitializationEngine context)
        {
            //Add uninitialization logic
        }
    }
}

and remove the config.MapHttpAttributeRoutes(); from the webapiconfig.cs, but that just gives me a 404 on the webapi call.. 

Other posts like this 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/ Just basically gives me the same 404 or the same error as above. 

How do you guys have it working. 

#194872
Edited, Jul 05, 2018 9:57
* 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.