Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection in ASP.NET Web API using Unity error

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 }
            );
like image 441
lincx Avatar asked Dec 03 '25 23:12

lincx


1 Answers

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

like image 50
lincx Avatar answered Dec 05 '25 14:12

lincx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!