Ivan Porto Carrero

IO(thoughts) flatMap (_.propagandize)

12

Oct
2006

Hosting Base4 Inside Your Web Application

Following up on the post of yesterday.  To use base4 with asp.net often you do not have the possibility to host base4 as a windows service. But to host base4 in the same appdomain as you website isn’t all that hard.

I’ll just share my code and how to use it. The way to go is to create a class that extends the HttpApplicatin and handle the application start and end events.

I’ll also post the code i have to integrate this with castle.

This is my base HttpApplication, every web app inherits of this one.

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using Base4.Storage.Server;

using System.Text.RegularExpressions;

using System.Reflection;

using System.Data.SqlClient;

using System.Configuration;

using Base4.Storage;

using System.Xml;

using System.IO;

 

namespace Flanders.Library.Base4Integration

{

    public class Base4HttpApplication : HttpApplication

    {

        ServerConfiguration config;

        IServerProxy proxy;

 

 

        public Base4HttpApplication()

        {

 

        }

 

        public virtual void Application_Start(object sender, EventArgs e)

        {

            //Build the base4 server configuration from the connection strings settings in the web.config

            config = new ServerConfiguration();

            config.Store.Root = Server.MapPath(“~/”);

            config.Store.Root = config.Store.Root.EndsWith(“") ? config.Store.Root

                + “App_Data\Base4” : config.Store.Root + “\App_Data\Base4”;

 

            //Set the application name of the store. In my case I define a title for my application in a

            //solution info file and use that as my app name

            //For this to work you will need to name your database the same as your applicationname

            if (Application[“AppName”] == null)

            {

                Assembly assembly = Assembly.GetCallingAssembly();

                Application[“AppName”] =

                    (AssemblyTitleAttribute.GetCustomAttribute(assembly, typeof(AssemblyTitleAttribute)) as AssemblyTitleAttribute).Title;

            }

 

            config.Store.Name = Application[“AppName”].ToString();

            config.Store.Provider = “SQL2005”;

 

            //Set the port for base4 to listen on.  This is defined in the web.config app settings

            if (Application[“AppPort”] == null)

                Application[“AppPort”] = string.IsNullOrEmpty(ConfigurationManager.AppSettings[“Base4.Port”]) ? 8809 :

                    int.Parse(ConfigurationManager.AppSettings[“Base4.Port”]);

 

            config.Store.Port = (int)Application[“AppPort”];

            //We are hosting base4 in the same process as the website. so localhost will do :)

            config.Store.MachineName = “localhost”;

 

            //Set the connection string for the base4 store

            string connectionStringName = ConfigurationManager.AppSettings[“DefaultConnection”];

            if (string.IsNullOrEmpty(connectionStringName) && ConfigurationManager.ConnectionStrings != null

                && ConfigurationManager.ConnectionStrings.Count > 0)

                connectionStringName = ConfigurationManager.ConnectionStrings[0].Name;           

 

            config.Store.ConnectionString = string.IsNullOrEmpty(connectionStringName) ?

                ConfigurationManager.AppSettings[“Store.ConnectionString”] :

                ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

 

            //Set the master connection string

            SqlConnectionStringBuilder connStrBuilder = new SqlConnectionStringBuilder(config.Store.ConnectionString);

 

            connStrBuilder.InitialCatalog = “master”;

            config.MasterConnectionString = connStrBuilder.ToString();

 

            //Check if we have a directory to store the files generated by base4

            if(!Directory.Exists(config.Store.Root))

                Directory.CreateDirectory(config.Store.Root);

 

            //Actually start the server

            proxy = ServerFactory.StartServer(config, false);

 

            //Set the context for base4

            string base4Context = string.Format(“tcp://Server:@localhost:{0}/{1}”, Application[“AppPort”], Application[“AppName”]);

 

            Application[“Base4Context”] = base4Context;

 

            //Set the default storage context for base4

            StorageContext.SetDefault(base4Context);

 

        }

 

        public virtual void Application_End(object sender, EventArgs e)

        {

            proxy.Stop();

        }

 

    }

}

Now to integrate this with monorail then this is you application class in your website :

 

using System.Web;

using Castle.Windsor;

using Base4.Storage;

using System.Configuration;

using Flanders.Library.Base4Integration;

 

namespace NBlogr.Presentation

{

    public class ASPGlobal : Base4HttpApplication, IContainerAccessor

    {

        private static MonoRailContainer container;

 

        public override void Application_Start(object sender, System.EventArgs e)

        {

            base.Application_Start(sender, e);

            container = new MonoRailContainer();

        }

 

        public override void Application_End(object sender, System.EventArgs e)

        {

            container.Dispose();

            base.Application_End(sender, e);

        }

 

 

        #region IContainerAccessor Members

 

        public IWindsorContainer Container

        {

            get { return container; }

        }

 

        #endregion

    }

}

The global.asax should only contain one line :

<span class="asp"><%@ Application Inherits="NBlogr.Presentation.ASPGlobal" Language="C#" %></span>

To top