Skip to content

Using the Container

Shannon Deminick edited this page Jul 26, 2019 · 5 revisions

Once you have your desired package installed, it will be active in your solution (unless you disable it via config). At this stage you'll want to add your own bits to the container, so here's how you do that:

  • Create a custom Umbraco ApplicationEventHandler
  • Override ApplicationInitialized – we do this in this phase to bind to the container event before the container is built which occurs in the ApplicationStarted phase
  • Bind to the container event
  • add any custom services you want to the container

Container Events

Each implementation has an event to bind to called ContainerBuilding which is used to modify the container. Here's the different ones for each container:

// Autofac
AutofacStartup.ContainerBuilding  += (sender, args) => {};

// LightInject
LightInjectStartup.ContainerBuilding  += (sender, args) => {};

// Unity
UnityStartup.ContainerBuilding  += (sender, args) => {};

Example

Here’s a full working example showing various techniques and includes the syntax for Autofac. In this example we’re registering a IServerInfoService as a request scoped object since it requires an HttpRequestBase. NOTE: That the basic web objects are already registered in the containers (such as HttpContextBase, HttpRequestBase, etc…)

public class MyUmbracoStartup : ApplicationEventHandler
{
    protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        //Example for using Autofac:
        AutofacStartup.ContainerBuilding += (sender, args) =>
        {
            //add our own services
            args.Builder.RegisterControllers(typeof(TestController).Assembly);
            args.Builder.RegisterType().As().InstancePerRequest();
        };
    }
}

//custom service
public interface IServerInfoService
{
    string GetValue();
}

//implementation of the custom service
public class ServerInfoService : IServerInfoService
{
    private readonly HttpRequestBase _umbCtx;

    //requires a request based object so this must be scoped to a request
    public ServerInfoService(HttpRequestBase umbCtx)
    {
        _umbCtx = umbCtx;
    }

    public string GetValue()
    {
        var sb = new StringBuilder();
        sb.AppendLine("Server info!").AppendLine();
        foreach (var key in _umbCtx.ServerVariables.AllKeys)
        {
            sb.AppendLine($"{key} = {_umbCtx.ServerVariables[key]}");
        }
        return sb.ToString();
    }
}

public class TestController : SurfaceController
{
    private readonly IServerInfoService _serverInfoService;

    public TestController(IServerInfoService serverInfoService, UmbracoContext umbCtx): base(umbCtx)
    {
        _serverInfoService = serverInfoService;
    }

    //see /umbraco/surface/test/index to see the result
    public ActionResult Index()
    {
        return Content(_serverInfoService.GetValue(), "text/plain");
    }
}

More info

See blog post https://shazwazza.com/post/easily-setup-your-umbraco-installation-with-ioc-dependency-injection/

Clone this wiki locally