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
50
votes
9 answers

MVC, EF - DataContext singleton instance Per-Web-Request in Unity

I have a MVC 3 web application, where I am using the Entity Framework for the data access. Furthermore, I have made a simple use of the repository pattern, where e.g. all Product related stuff is handled in the "ProductRepository" and all User…
Nima
  • 937
  • 1
  • 13
  • 20
49
votes
4 answers

Is it better to create a singleton to access unity container or pass it through the application?

I am dipping my toe into using a IoC framework and I have choosen to use Unity. One of the things that I still don't fully understand is how to resolve objects deeper into the application. I suspect I just haven't had the light bulb on moment that…
btlog
  • 4,760
  • 2
  • 29
  • 38
48
votes
6 answers

Register IAuthenticationManager with Unity

I'm using Unity for Dependencies Injection and using Identiy Provider to manage the user login, register, email confirmation, etc. When I try to register a user, I have this problem: The current type, Microsoft.Owin.Security.IAuthenticationManager,…
chemitaxis
  • 13,889
  • 17
  • 74
  • 125
47
votes
6 answers

Unity Register two interfaces as one singleton

how do I register two different interfaces in Unity with the same instance... Currently I am using _container.RegisterType(new…
Christian Ruppert
  • 3,749
  • 5
  • 47
  • 72
46
votes
7 answers

Exception is: InvalidOperationException - The current type, is an interface and cannot be constructed. Are you missing a type mapping?

In my bootstrapper: namespace Conduit.Mam.ClientServices.Common.Initizliaer { public static class Initializer { private static bool isInitialize; private static readonly object LockObj = new object(); private static…
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
45
votes
3 answers

Singleton Per Call Context (Web Request) in Unity

A few days ago, I had an issue with ASP.Net threading. I wanted to have a singleton object per web request. I actually need this for my unit of work. I wanted to instantiate a unit of work per web request so that identity map is valid through out…
Mehdi Khalili
  • 927
  • 1
  • 11
  • 18
44
votes
4 answers

Specify constructor for the Unity IoC container to use

I'm using the Unity IoC container for resolving my objects. However, I've run into an issue. When I have more than one constructor - how does Unity know which one to use? It seems to use the one with parameters when I have one with and one without.…
stiank81
  • 25,418
  • 43
  • 131
  • 202
44
votes
3 answers

Unity InjectionConstructor for multiparam constructor overriding only single one

I have a class with constructor like this: public class Bar { public Bar(IFoo foo, IFoo2 foo2, IFoo3 foo3, IFooN fooN, String text) { } } I want to register Bar in Unity and provide a value for text: unity.RegisterType(new…
Alex Burtsev
  • 12,418
  • 8
  • 60
  • 87
42
votes
7 answers

How do you reconcile IDisposable and IoC?

I'm finally wrapping my head around IoC and DI in C#, and am struggling with some of the edges. I'm using the Unity container, but I think this question applies more broadly. Using an IoC container to dispense instances that implement IDisposable…
Mr. Putty
  • 2,276
  • 1
  • 20
  • 20
42
votes
8 answers

Resolving IEnumerable with Unity

Can Unity automatically resolve IEnumerable? Let's say I have a class with this constructor: public CoalescingParserSelector(IEnumerable parserBuilders) and I configure individual IParserBuilder instances in the…
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
41
votes
4 answers

Unity 2.0 and handling IDisposable types (especially with PerThreadLifetimeManager)

I know that similar question was asked several times (for example: here, here,here and here) but it was for previous versions of Unity where the answer was dependent on used LifetimeManager class. Documentation says: Unity uses specific types that…
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
41
votes
9 answers

Cannot get rid of "physical connection is not usable" exception

I am about to shoot myself. Spent few weeks now trying to solve this issue. We have an ASP.NET MVC 4 web app that uses SQL Server 2012 and Entity Framework as ORM and Unity for IoC. Web app is hosted on Amazon EC2. I started getting "Physical…
39
votes
7 answers

How do I use the Decorator Pattern with Unity without explicitly specifying every parameter in the InjectionConstructor

This helpful article from David Haydn (EDIT: scam link removed, it could have been this article) shows how you can use the InjectionConstructor class to help you set up a chain using the decorator pattern with Unity. However, if the items in your…
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
39
votes
1 answer

MVVM Light + Unity or Prism?

I am a little out-of-date in WPF right now and would be interested to hear peoples opinions on the latest version of Prism (which I used a couple of versions ago) vs an MVVM Light + Unity approach (which I have never done - decent examples URLs…
David
  • 682
  • 2
  • 9
  • 15
39
votes
6 answers

How to remove(unregister) registered instance from Unity mapping?

I meet one problem that i can't solve now. I have the following: UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance); where UnityHelper.DefaultContainer is my helper for getting unity container with loaded…
bug0r
  • 613
  • 1
  • 8
  • 19