Codemania: Run EPiServer CMS in the Console

...

Back in the happy and fun filled days of EPiServer Day 2009, I was so privileged that I got to give a 1-hour talk entitled “Codemania” together with Mr. EPiServer himself, chief architect Magnus Stråle. At that day, most of my efforts to impress the approx. 150 developers attending the session were put to shame, when Magnus showcased a little Console, resembling the typical windows / DOS command prompt, but in fact being a full-blown EPiServer CMS installation running outside of a web server. Needless to say both I and many others were stunned by the cool geekyness of it, and ever since both Magnus and I have heard a number of requests for the source code. Since Magnus is quite busy at the moment he has graciously allowed me to share the code here on my blog. So, better late than never, here goes: 

Download the solution here.

The solution is a basic windows console application. In order to use it, follow these steps:

  1. Copy all the contents of your web.config (from your regular EPiServer CMS 5 R2 installation) and use it to replace everything in app.config.
  2. Copy the contents of “connectionstrings.config” and use it to replace the tag <connectionStrings configSource="connectionStrings.config" /> in the app.config
  3. If you are using your own assemblies for stuff loaded in the configuration, like page providers, virtual roles, virtual path providers, dynamic content, custom properties, modules, etc. you want to add references to them in the projects
  4. Compile and run, you should be good to go. You should now see a command-prompt like this:
    image
  5. Now you can start browsing your EPiServer installation as if it was your file-system. You can use commands like “CD” to move between pages, “DIR” to see all child-pages + properties on the page, “TYPE” to see the contents of a property, “COPY” – ah well – to copy to the regular file system. If you type “FILE” you switch over from browsing pages, to browsing the virtual path provider file system - “PAGE” switches you back

Obviously this example is a little bit geeky and useless, but nevertheless it shows an important point – you don’t need a webserver to run EPiServer CMS. Just imagine what this means with regards to the testability of your application. Or other potential future uses…. You can basically create any kind of server, to serve any kind of information managed in EPiServer in any kind of format.

 

- “How is this even possible?” I hear you ask. Well, in EPiServer CMS 5 R2, an abstraction layer for the hosting environment, called the EPiServer.Web.Hosting.IHostingEnvironment, has been implemented. That means, that by introducing your own class that implements that interface you can boot up EPiServer in any environment. With that in place you basically just need to initialize EPiServer CMS through these basic steps:

public static void InitializeEPiServer()
{
    // Read config file and initialize EPiServer specific configuration
    Settings.InitializeAllSettings(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None));
 
    // Settings.Instance is usually set on first web request based on host name in incoming request.
    // Force it to be the first <site> section in our config file.
    Settings.Instance = EPiServerSection.Instance.Sites[0].SiteSettings;
 
    // In order for VPP:s to work, we need to use our own hosting environment
    GenericHostingEnvironment.Instance = new EPiServerHostingEnvironment();
 
    // Finally get all the remaining parts of EPiServer set up
    InitializationModule.Initialize(EPiServerSection.Instance, Settings.Instance);
}

The rest of the code – and probably most of Magnus’ development time on this toy-app is spend on getting the console window to look, feel and act like an old-fashioned command-prompt.

Enjoy – all credits (and bugs) goes to Magnus!

Post Comments()

You might also like

You might also like