MVC, Unity and Dependency attribute

Aug 12, 2012 at 1:31 AM

I've got an MVC app with Unity container configured. All dependencies are resolved in this project. But I have a class library project which is used by MVC app. This class library has classes where I use [Dependency] attribute to resolve public properties. And this doesn't work. The only way I could get it to work is to use container.BuildUp(myClassObject) specifying exact class name (but not an interface which it implements).

So, suppose I have the code in my class library:

public class MyServiceImplementation : IService {
  public ISomeOtherService AnotherService { get; set; }
  public Method()
     AnotherService.Do();   // NULL here

and I register all them like this in MVC app:

container.RegisterType<IService, MyServiceImplementation>();

container.RegisterType<ISomeOtherService, TheOtherService>();

Now, the MyServiceImplementation.AnotherService is always NULL.

Why is this Dependency attribute doesn't work? Why it doesn't resolve anything in the class library?
Any help is appreciated.

Aug 12, 2012 at 4:20 AM

It looks like you are registering the services with the container but it's not clear to me if you are using the container to resolve IService.  Basically, at application startup create a container and register all of the types and then set the container to be the DependencyResolver.  To do this you could leverage Unity.MVC3.  Here are some good articles on how to wire it up:

Randy Levy
Enterprise Library support engineer 

Aug 12, 2012 at 5:17 AM

Thank you Randy for the reply.

I do register services. Here's the code:

		public static void Initialise()
			var container = BuildUnityContainer();

            // set this container for using in Enterprise library
            var configurator = new UnityContainerConfigurator(container);
            EnterpriseLibraryContainer.ConfigureContainer(configurator, ConfigurationSourceFactory.Create());

            // now set the container for MVC
			DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            // register dependency resolver for WebAPI RC     
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

		private static IUnityContainer BuildUnityContainer()
			var container = new UnityContainer();

            container.RegisterType<IPictureService, CloudGalleryAzure.PictureService>();
            container.RegisterType<IGalleryService, CloudGalleryAzure.GalleryService>();
			container.RegisterType<ILogger, Infrastructure.Logging.Logger>();
            container.RegisterType<IImageService, Infrastructure.Imaging.ImageService>();

			return container;

But still nothing is resolver in the class library's code...

Aug 12, 2012 at 5:50 AM
Edited Aug 12, 2012 at 6:11 AM

I'm still not seeing the big picture here.  I would expect that you would have an IService that would possibly be injected into a Controller.  Since BuildUp works that shows that Unity is injecting properly.  I think what you are seeing is that the MyServiceImplementation class is not being created by Unity so the dependency is not being injected.  

I can't see your code so it's hard to know exactly what is happening.  If you could provide a complete code sample or a sample project I could better help pinpoint the issue.


Randy Levy
Enterprise Library support engineer 

Aug 17, 2012 at 9:35 AM

Randy, let me create a test project tomorrow (maybe after tomorrow) and I will post it here.

Sep 7, 2012 at 5:13 PM

Stupid question: are you sure you're using the Unity DependencyAttribute? There's another DependencyAttribute in the System.Runtime.CompilerServices namespace (I think that's where it is) and people sometimes pick that one up by accident instead.