Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Justin Moser 20 posts 92 karma points
    Aug 14, 2013 @ 23:06
    Justin Moser
    0

    Umbraco 6.1, UmbracoApiController (Web API) & Dependency Injection (Unity)

    Hi,

    I am using Umbraco 6.1 with an UmbracoApiController which has a IUnitOfWork injected into it's constructor. To inject the dependencies, I am using Unity, like I have in the past with standard Web API projects. Normally, I set unity up in the Global.asax.cs. As Umbraco does not have this I have created my own UmbracoEvents handler, which inherits from IApplicationEventHandler, and has the methods:

    • OnApplicationInitialized
    • OnApplicationStarting
    • OnApplicationStarted
    • ConfigureApi
    In the OnApplicationStarted method I set up my EF database, db initializer etc and call ConfigureApi to set up Unity. My OnApplication Started and ConfigureApi methods looks like this:
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                _applicationContext = applicationContext;
                _umbracoApplication = umbracoApplication;
                _contentService = ApplicationContext.Current.Services.ContentService;
                this.ConfigureApi(GlobalConfiguration.Configuration);
                Database.SetInitializer(null);
                PropertySearchContext db = new PropertySearchContext();
                db.Database.Initialize(true);
            }
    private void ConfigureApi(HttpConfiguration config) { var unity = new UnityContainer(); unity.RegisterType<PropertiesApiController>(); unity.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager()); config.DependencyResolver = new IoCContainer(unity); }
    My controller looks like this:
     public class PropertiesApiController : UmbracoApiController
        {
            private readonly IUnitOfWork _unitOfWork;
            public PropertiesApiController(IUnitOfWork unitOfWork)
            {
                if(null == unitOfWork)
                    throw new ArgumentNullException();
                _unitOfWork = unitOfWork;
            }
            public IEnumerable GetAllProperties()
            {
                return new[] {"Table", "Chair", "Desk", "Computer", "Beer fridge"};
            }
        }
    The GetAllProperties method is just a test to make sure it is all working fine, however, when I try and access this action I get the error:
    "The type IUnitOfWork does not have an accessible constructor"
    Does anyone have experience with using Umbraco 6.1 and it's UmbracoApiController with dependency injection/Unity?
    Also, on an unrelated note, is there a way to return JSON instead of XML in the action? In Web API you would just define the formatter in the WebApi.config but there is none in Umbraco.
    Thanks,
    Justin

     

  • Dan 5 posts 27 karma points
    Feb 04, 2014 @ 07:02
    Dan
    0

    Hi,

    Not sure if this will be any help, but if you want to get DI working for your WebAPI controllers, you will need to change the dependency resolver for WebAPI at application startup, like so:

    GlobalConfiguration.Configuration.DependencyResolver = new YourDependencyResolver(container);

    This worked great for my custom controllers, but it broke the Backoffice application (the Backoffice now uses the WebAPI to fetch data via AngularJs).

    I'm using StructureMap as my DI container. I may try another DI container soon and let you know how it goes.

    Dan.

Please Sign in or register to post replies

Write your reply to:

Draft