Questions tagged [unity-container]

The Unity Application Block (i.e. Unity) is a lightweight, extensible dependency injection container for .NET with support for interception. DO NOT USE THIS TAG TO REFER TO THE UNITY GAME ENGINE! Use unity-game-engine instead (https://stackoverflow.com/tags/unity-game-engine/info)!

The Unity Application Block (i.e. Unity Container) is a lightweight, extensible dependency injection container for .NET. It also supports interception.

Unity targets both .NET CLR and Silverlight.

Installing Unity can most easily be done using its NuGet package:

Install-Package Unity

More information at:

Hello world in c#

interface IMessageWriter 
{
    void WriteMessage(string message);
}

class ConsoleWriter : IMessageWriter 
{
    public void WriteMessage(string message) 
    { 
        Console.WriteLine(message); 
    }
}

class HelloWorldService 
{
    private readonly IMessageWriter _writer;

    public HelloWorldService(IMessageWriter writer) 
    {
       _writer = writer;
    }

    public void Go() 
    {
       _writer.WriteMessage("Hello World!");
    }
}

using (var container = new UnityContainer()) 
{        
    container.RegisterType<IMessageWriter, ConsoleWriter>();

    var helloWorldService = container.Resolve<HelloWorldService>();

    helloWorldService.Go();    
}
3920 questions
319
votes
7 answers

How do the major C# DI/IoC frameworks compare?

At the risk of stepping into holy war territory, What are the strengths and weaknesses of these popular DI/IoC frameworks, and could one easily be considered the best? ..: Ninject Unity Castle.Windsor Autofac StructureMap Are there any other…
ocodo
  • 29,401
  • 18
  • 105
  • 117
164
votes
6 answers

Can someone explain Microsoft Unity?

I've been reading the articles on MSDN about Unity (Dependency Injection, Inversion of Control), but I think I need it explained in simple terms (or simple examples). I'm familiar with the MVPC pattern (we use it here), but I just can't really grasp…
152
votes
5 answers

Is there a pattern for initializing objects created via a DI container

I am trying to get Unity to manage the creation of my objects and I want to have some initialization parameters that are not known until run-time: At the moment the only way I could think of the way to do it is to have an Init method on the…
137
votes
8 answers

Enterprise Library Unity vs Other IoC Containers

What's pros and cons of using Enterprise Library Unity vs other IoC containers (Windsor, Spring.Net, Autofac ..)?
Yoann. B
  • 11,075
  • 19
  • 69
  • 111
128
votes
11 answers

Make sure that the controller has a parameterless public constructor error

I have followed this tutorial which has worked great, until I modified my DbContext to have an additional constructor. I am now having issues with the resolution and not sure what to do to fix this. Is there an easy way to force it to grab the…
scarpacci
  • 8,957
  • 16
  • 79
  • 144
106
votes
5 answers

Ninject vs Unity for DI

We are using ASP.net MVC. Which of these is the best DI framework Ninject or Unity and why?
Miral
  • 5,968
  • 16
  • 57
  • 85
94
votes
7 answers

Can I pass constructor parameters to Unity's Resolve() method?

I am using Microsoft's Unity for dependency injection and I want to do something like this: IDataContext context = _unityContainer.Resolve(); var repositoryA = _unityContainer.Resolve(context); //Same instance of…
NotDan
  • 31,709
  • 36
  • 116
  • 156
84
votes
11 answers

Cannot Inject Dependencies into ASP.NET Web API Controller using Unity

Has anyone had any success running using an IoC container to inject dependencies into ASP.NET WebAPI controllers? I cannot seem to get it to work. This is what I'm doing now. In my global.ascx.cs: public static void…
Oved D
  • 7,132
  • 10
  • 47
  • 69
77
votes
5 answers

IUnityContainer.Resolve throws error claiming it cannot be used with type parameters

Yesterday I've implemented the code: CustomerProductManager productsManager = container.Resolve(); It was compilable and working. Today (probably I've modified something) I am constantly getting the error: The non-generic…
Budda
  • 18,015
  • 33
  • 124
  • 206
75
votes
4 answers

With Unity how do I inject a named dependency into a constructor?

I have the IRespository registered twice (with names) in the following code: // Setup the Client Repository IOC.Container.RegisterType(new InjectionConstructor()); IOC.Container.RegisterType
Vaccano
  • 78,325
  • 149
  • 468
  • 850
71
votes
4 answers

MEF vs. any IoC

Looking at Microsoft's Managed Extensibility Framework (MEF) and various IoC containers (such as Unity), I am failing to see when to use one type of solution over the other. More specifically, it seems like MEF handles most IoC type patterns and…
PureCognition
  • 1,253
  • 2
  • 10
  • 14
65
votes
8 answers

Can Unity be made to not throw SynchronizationLockException all the time?

The Unity dependency injection container has what seems to be a widely known issue where the SynchronizedLifetimeManager will often cause the Monitor.Exit method to throw a SynchronizationLockException which is then caught and ignored. This is a…
Rory MacLeod
  • 11,012
  • 7
  • 41
  • 43
54
votes
2 answers

What is different between and purpose of MEF and Unity?

I just start study DI (I'm working on WPF/Silverlight but I have a plan to move to ASP.NET). After I read some DI articles from internet there are two Frameworks that I'm interested in, MEF and Unity. I want to know what is the real-world different…
Anonymous
  • 9,366
  • 22
  • 83
  • 133
52
votes
4 answers

Use of IsAssignableFrom and "is" keyword in C#

While trying to learn Unity, I keep seeing the following code for overriding GetControllerInstance in MVC: if(!typeof(IController).IsAssignableFrom(controllerType)) { ... } this seems to me a pretty convoluted way of basically…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
52
votes
2 answers

Comparing Castle Windsor, Unity and StructureMap

In a follow up to Krzysztof’s statement that Windsor does a lot more than other IoC’s, I wanted to understand how these IoC’s stack up against each other and the benefits/additional facilities that castle Windsor provides. Are there any…
1
2 3
99 100