Search code examples
c#asp.netasp.net-mvcowinepiserver

Remove Global.asax and Run EPiServer CMS MVC from Owin Startup.Configuration


I'm required to write some custom middleware to work with EPiServer CMS. I'd like to remove Application_Start() and run everything from StartUp.Configuration()

This seems possible with a baseline MVC 4 application since you can register routes and do all of the necessary start up tasks by calling methods within Configuration().

However, EPiServer's methods for registering routes and etc are protected (and I don't know all of the necessary ones).I anticipate the following benefits by running the app with Startup instead of Global.asax

  1. If I can control when initialization modules are ran, then I can use service location (ServiceLocator) in my custom middleware
  2. I decrease complexity by not having two application starting points for the same app

    These are both important to me. I would prefer to not use Application_Start() unless I absolutely have to.

    My research has revealed one potential method of doing this by using EPiServer.ServiceAPi - but the core purpose of ServiceApi would not be used within my app. It seems wasteful to bring it in purely for OWIN compatibility. Also it has an OAuth provider dependency which I do not wish to fill at this time as my app does not require it. Has anyone done this successfully? Can anyone offer guidance? I appreciate your time and I'm happy to provide clarification on my question where necessary. Thanks

    Here are code snippets: Note the namespace on this first one - seems to be inheriting some functionality and then running typical ASP.NET MVC stuff.

    public class EPiServerApplication : EPiServer.Global
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();            
            var formatters = GlobalConfiguration.Configuration.Formatters;
            var jsonFormatter = formatters.JsonFormatter;
            var settings = jsonFormatter.SerializerSettings;
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );            
        }
    }
    

Here we have the typical OWIN Startup configuration. I'd like the above to be removed and only use the below. Can it be done?

public class Startup
{                 
    public void Configuration(IAppBuilder app)
    {
        AreaRegistration.RegisterAllAreas();
        new EPiServer.ServiceApi.Startup().Configuration(app);
        app.Use(typeof(CustomMiddleware));
    }
}

Solution

  • I'd recommend looking into InitializableModule for things to run on Episerver startup:

    [InitializableModule]
    [ModuleDependency((typeof(EPiServer.Web.InitializationModule)))]
    public class MyInitializationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
           // Your code here
        }
        public void Uninitialize(InitializationEngine context)
        {
        }
    }
    

    The ModuleDependency attribute is used to specify that another initialization module must have been loaded before your code is executed. For example, a dependency to EPiServer.Web.InitializationModule ensures you can call Episerver APIs to work with content in your code.