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
19
votes
1 answer

How to resolve dependency in static class with Unity?

I have the following extension method, which exists (naturally) in a static class. public static class MyExtensions { [Dependency] private static IMyDataContext _myDataContext { get; set; } public static void MyExtensionMethod(this…
DaveDev
  • 41,155
  • 72
  • 223
  • 385
19
votes
4 answers

How do I correctly use Unity to pass a ConnectionString to my repository classes?

I've literally just started using the Unity Application Blocks Dependency Injection library from Microsoft, and I've come unstuck. This is my IoC class that'll handle the instantiation of my concrete classes to their interface types (so I don't have…
18
votes
4 answers

What does this mean in Prism/Unity: Container.Resolve()

(from the StockTraderRIBootstrapper.cs file in the Prism V2 StockTrader example app) What is the difference between this: ShellPresenter presenter = new ShellPresenter(); and this: ShellPresenter presenter = Container.Resolve(); I…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
18
votes
4 answers

Configure Unity DI for ASP.NET Identity

I'm using Unity successfully for all regular constructor injection such as repositories etc., but I can't get it working with the ASP.NET Identity classes. The setup is this: public class AccountController : ApiController { private…
17
votes
6 answers

Is there TryResolve in Unity?

How can I make Unity not to throw ResolutionFailedException if Resolve fails? Is there something like TryResolve? var container = new UnityContainer(); var foo = container.TryResolve(); Assert.IsNull(foo);
Vadim
  • 21,044
  • 18
  • 65
  • 101
17
votes
7 answers

Using ASP.NET Session for Lifetime Management (Unity)

I am considering using Unity to manage the lifetime of a custom user class instance. I am planning on extending the LifetimeManager with a custom ASP.NET session manager. What I want to be able to do is store and retrieve the currently logged in…
Sigray
17
votes
3 answers

Resolving classes without registering them using Castle Windsor

Take the following useless program: class Program { static void Main(string[] args) { IUnityContainer unityContainer = new UnityContainer(); IWindsorContainer windsorContainer = new WindsorContainer(); Program…
James Thurley
  • 2,650
  • 26
  • 38
17
votes
3 answers

How to debug Unity resolution?

In a WPF project(with prism) we are using Unity as DI framework. Recently, after we merged two big branches, we were not able to start our application, we were having StackOverflowException. Due to the nature of the exception, we were not able to…
J4N
  • 19,480
  • 39
  • 187
  • 340
17
votes
1 answer

How to configure unity container to provide string constructor value?

This is my dad class public class Dad { public string Name { get;set; } public Dad(string name) { Name = name; } } This is my test method public void TestDad() …
17
votes
1 answer

Getting unity to resolve multiple instances of the same type

I want to do a simple resolve of multiple type registrations (ultimately constructor injected, but using .Resolve to see if Unity is even capable of such things. In every case below, Unity resolves 0 items where it should be resolving 2. Is there…
S. Hebert
  • 808
  • 1
  • 8
  • 18
17
votes
2 answers

Entity Framework returning old data

I have a problem with EF not returning the newest data in a 3 layered WPF application, and I suspect it has something to do with how I handle the lifetime of my context. This is the scenario: There are several repositories wrapped inside a…
17
votes
3 answers

Use Unity to intercept all calls to IMyInterface.SomeMethod

I am trying to learn Unity Interceptors and I am having a hard go of it. Say I have an interface like this: public interface IMyInterface { void SomeMethod(); } And I have an unknown number of classes that implement that interface like…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
17
votes
3 answers

Where to place and configure IoC container in a WPF application?

I am working on a middle sized WPF application (MVVM) that should be extensible and maintainable in the future. Thus I decided to use an IoC container (Unity in this case) to keep things flexible. However I am not sure where to place and configure…
matori82
  • 3,669
  • 9
  • 42
  • 64
16
votes
3 answers

Should Unity be configured in code or configuration file?

Microsoft's Unity dependency injection framework can be configured either through code or through the applications configuration file (app.config). Code example: IUnityContainer container = new UnityContainer() .RegisterType
16
votes
2 answers

What is Unity InjectionConstructor Attribute?

What is Unity InjectionConstructor Attribute and how it works ?
Yoann. B
  • 11,075
  • 19
  • 69
  • 111