Eric Herlitz
Eric Herlitz

Reputation: 26307

Extending global.asax in Umbraco 4.7 for dependency injection

Im using umbraco v 4.7.1 (Assembly version: 1.0.4281.20201) and have a project where I must extend the global.asax file.

Please not the following

Here is my implementation,

using Project.Umbraco.DependencyInjection;
using Project.Umbraco.IoC;
using Microsoft.Practices.Unity;
using System;
using System.Diagnostics;
using umbraco;

namespace Project.Umbraco.App_Start
{
    public class MyGlobal : Global, IContainerAccessor
    {
        /// 

        /// Returns the IoC container
        /// IContainerAccessor
        /// 

        public IUnityContainer Container
        {
            get
            {
                return MvcUnityContainer.Instance.Container;
            }
        }

        protected override void Application_Start(object sender, EventArgs e)
        {
            base.Application_Start(sender, e);
            Debug.WriteLine("Application start");
        }

        protected override void Application_BeginRequest(object sender, EventArgs e)
        {
            base.Application_BeginRequest(sender, e);
            Debug.WriteLine("Application start");
        }
        //protected void Session_Start(object sender, EventArgs e) {}
        //protected void Application_AuthenticateRequest(object sender, EventArgs e) {}
        //protected void Application_Error(object sender, EventArgs e) {}
        //protected void Session_End(object sender, EventArgs e) {}
        //protected void Application_End(object sender, EventArgs e) {}
    }
}

The implementation seems as if should work, maybe I've just placed this in the wrong namespace or something?

Thanks for any help

T

Upvotes: 0

Views: 1606

Answers (1)

sebastiaan
sebastiaan

Reputation: 5917

From Umbraco 4.8.0 and onwards, the App_global.asax.dll is no longer needed, so you might want to consider upgrading to a newer version.

In 4.7 though, you could simply use the PreApplicationStart method by creating a class that looks a little something like this:

using System.Linq;
using System.Web.Routing;
using System.Web.Http;
using CustomerName.Extensions;

[assembly: System.Web.PreApplicationStartMethod(typeof(AppStart), "PreStart")]
namespace CustomerName.Extensions
{
    public static class AppStart
    {
        public static void PreStart()
        {
            RouteTable.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{id}",
               defaults: new { id = RouteParameter.Optional }
           );
        }
    }
}

Of course, instead of defining WebAPI routes, you could insert your DI code.

Upvotes: 2

Related Questions