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
30
votes
2 answers

ResolveAll not working

I have a code which look something like this. When trying to do a ResolveAll I expected every type registered with the IParserType to yielded from the container instance. But I didn't get any instance from Unity. Am I assuming or doing something…
Arshad Badar Khan
  • 942
  • 1
  • 12
  • 32
30
votes
4 answers

Prism v4: Unity or MEF?

I downloaded Prism v4 and ran the installer. I went into the directory and ran the two following batch files: Desktop only - Open Modularity With Mef QuickStart.bat Desktop only - Open Modularity With Unity QuickStart.bat When I compile these…
myermian
  • 31,823
  • 24
  • 123
  • 215
30
votes
3 answers

Where is Microsoft.Practices.Unity package?

An hour ago I updated my nuget packages for the solution I'm working on and I get the error message, thrown by Unity, that The type 'IUnityContainer' is defined in an assembly that is not referenced. You must add a reference to assembly …
AndrasCsanyi
  • 3,943
  • 8
  • 45
  • 77
30
votes
5 answers

how to implement IOC without a global static service (non-service locator solution)?

we want to use Unity for IOC. All i've seen is the implementation that there is one global static service (let's call it the the IOCService) which holds a reference to the Unity container, which registers all interface/class combinations and every…
Michel
  • 23,085
  • 46
  • 152
  • 242
30
votes
3 answers

Disposing needed in Unity?

Probably a Unity beginner's question: when using Unity, would you still need to implement Dispose methods on the objects you have injected? Or is even this not needed (so, done automatically by Unity)? This is in the context of a web application.
hetnet
  • 301
  • 1
  • 3
  • 3
29
votes
8 answers

The type 'IUnityContainer' is defined in an assembly that is not referenced

I just upgraded my ASP.NET MVC/WebApi project from Microsoft.Practices.Unity 3.5.1404 to 3.5.1406 (via nuget, just released). Afterwards, I'm getting this compile error: Error CS0012 The type 'IUnityContainer' is defined in an assembly that is not…
Ken Smith
  • 20,305
  • 15
  • 100
  • 147
28
votes
3 answers

Could not load file or assembly System.Web.WebPages.Razor, , Version=3.0.0.0 or one of its dependencies

I am using MVC 5, WCF and Unity framework in my application. I am getting below error when I run WCF service: Server Error in '/' Application. Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral,…
user2934829
  • 667
  • 1
  • 7
  • 10
27
votes
3 answers

Why is a type registered twice when lifetime manager is specified?

I'm using Unity's Register by convention mechanism in the following scenario: public interface IInterface { } public class Implementation : IInterface { } Given Implementation class and its interface I'm running RegisterTypes in the following…
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
26
votes
6 answers

Best Practices for IOC Container

I'm using the Unity IOC container and I'm just wondering what is the best best way to access the container for multiple classes. Should every class have an IUnityContainer member and then pass the container in by constructor? Should there be a…
Ryu
  • 8,641
  • 10
  • 65
  • 98
25
votes
2 answers

Unity with ASP.NET Core and MVC6 (Core)

Update 09.08.2018 Unity is being developed here but I haven't had the time to test how it plays with the ASP.NET Core framework. Update 15.03.2018 This solution is for the specific problem of using ASP.NET Core v1 with Unity while using the .NET…
D4rth B4n3
  • 1,268
  • 2
  • 17
  • 26
25
votes
2 answers

Why shouldn't I use Unity?

I'm using the Unity IoC container. It really wasn't a decision I made, it just came with Prism, and I've just stuck with it. I've never used any other IoC frameworks, and I must admit I'm quite happy with Unity. However, the satisfaction may come…
stiank81
  • 25,418
  • 43
  • 131
  • 202
25
votes
1 answer

Strategy Pattern and Dependency Injection using Unity

I am finally getting my feet wet with Dependency Injection (long overdue); I got started playing with Unity and run into an issue with the strategy pattern. I can use the container to return to me specific implementations of a strategy based on a…
24
votes
8 answers

Setter / property injection in Unity without attributes

I am working on a project where the Unity framework is used as the IoC container. My question relates to injecting an optional dependency (in this case a logger) into several classes using property- or setter injection. I do not want to clutter the…
LittleBoyLost
  • 518
  • 2
  • 4
  • 11
24
votes
2 answers

Can not install NuGet package

I am trying to add the Unity package to my solution, but I keep receiving the listed message: Attempting to resolve dependency 'Unity (≥ 3.5.1404.0)'. 'Unity' already has a dependency defined for 'CommonServiceLocator'. Any Idea how to fix this?
Jaster
  • 8,255
  • 3
  • 34
  • 60
23
votes
3 answers

Is there a good/proper way of solving the dependency injection loop problem in the ASP.NET MVC ContactsManager tutorial?

If you don't know what I'm talking about either go through the tutorial and try to add dependency Injection yourself or try your luck with my explanation of the problem. Note: This problem isn't within the scope of the original tutorial on ASP.NET.…