3

Im trying to follow this blog for implementing dependency injection with Unity but I'm getting an error in the boostrapper.cs (I use Unity.MVC4 library). Can someone point me what's I am missing here?

ERROR: IoCContainer does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator. Parameter name: commonServiceLocator

 public static class Bootstrapper
    {
    public static IUnityContainer Initialise()
    {
      var container = BuildUnityContainer();

      DependencyResolver.SetResolver(new UnityDependencyResolver(container));

      // this throws "does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.... error"
      DependencyResolver.SetResolver(new IoCContainer(container)); 

      return container;
    }

    ....
  }

this is the IoCContainer class

public class ScopeContainer : IDependencyScope
    {
        protected IUnityContainer container;

        public ScopeContainer(IUnityContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            this.container = container;
        }

        public object GetService(Type serviceType)
        {
            if (container.IsRegistered(serviceType))
            {
                return container.Resolve(serviceType);
            }
            else
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (container.IsRegistered(serviceType))
            {
                return container.ResolveAll(serviceType);
            }
            else
            {
                return new List<object>();
            }
        }

        public void Dispose()
        {
            container.Dispose();
        }
    }

    class IoCContainer : ScopeContainer, IDependencyResolver
    {
        public IoCContainer(IUnityContainer container)
            : base(container)
        {
        }

        public IDependencyScope BeginScope()
        {
            var child = container.CreateChildContainer();
            return new ScopeContainer(child);
        }
    }

ADDITIONAL! Im calling the bootrapper in the app_start which I am not sure it its correct

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            Bootstrapper.Initialise();
        }

I also have a feeling that it might related to the routing, I actually added the classic MVC routing in the config as I read that webapi and mvc uses different "activator" stuff or somehing like that

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
              name: "RestApi",
              routeTemplate: "api/{controller}/{action}/{id}",
              defaults: new { id = RouteParameter.Optional }
            );
2
  • 1
    And remove all directly not related code to make the question more clear, please. Commented Jul 4, 2013 at 1:14
  • // this throws "does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.... error" Commented Jul 4, 2013 at 1:15

1 Answer 1

5

Found the answer, seems like the blog is outdated as I dont even have to create the IoCContainer as I just need to install Unity.WebAPI and just set the DependencyResolver to new Unity.WebApi.UnityDependencyResolver(container);

public static class Bootstrapper
  {
    public static IUnityContainer Initialise()
    {
      var container = BuildUnityContainer();

      DependencyResolver.SetResolver(new UnityDependencyResolver(container));

      GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

      return container;
    }

credit: http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html

Sign up to request clarification or add additional context in comments.

2 Comments

Also worth looking at from the Unity team: Unity bootstrapper for ASP.NET MVC and Unity bootstrapper for ASP.NET Web API.
so, is it acceptable to use GlobalConfiguration.Configuration.DependencyResolver in your controller?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.