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

Get IOC container in a popup

I am using PRISM 5 in my WPF application. And the Shell view in my application has two regions, consider it as A and B.The region A contains a POPUP (PRISM 5 interactivity feature is used to show popup). The application is working when i create an…
Prasanth V J
  • 1,126
  • 14
  • 32
14
votes
5 answers

Assembly.CreateInstance to resolve IoC Container

I am trying to create an instance of a class (at runtime via a string) using the following code: Assembly assembly = Assembly.GetAssembly(typeAssembly); object instance = assembly.CreateInstance(typeName); //throws MissingMethodException Type…
user3117252
  • 389
  • 1
  • 5
  • 11
14
votes
1 answer

How to use Unity.RegisterType with Moq?

I have a running code with unity. Now I want to use Moq to do my unit testing for ASP-MVC. In the global.asax's code, I have the following: IUnityContainer container = new UnityContainer(); container.RegisterType(new…
user1702269
14
votes
1 answer

How to inject dependency property using Ioc Unity

I have the following classes: public interface IServiceA { string MethodA1(); } public interface IServiceB { string MethodB1(); } public class ServiceA : IServiceA { public IServiceB serviceB; public string MethodA1() { …
13
votes
3 answers

How to inject constructor argument from config file with Unity

Imagine we have a class public class MyClass { private string _val; public MyClass(string val) { _val = val; } } and app.config (or web.config) Is…
STO
  • 10,390
  • 8
  • 32
  • 32
13
votes
2 answers

Unity: Change default lifetime manager for implicit registrations and/or disable them

The Unity container will automatically resolve any type that it can figure out on its own without the need for manual registration. That's good in some ways, but the problem I have is that it uses a TransientLifetimeManager for this type of…
dlf
  • 9,045
  • 4
  • 32
  • 58
13
votes
1 answer

Unity Lifetime Managers & EF Data Context --> Best Practice

All, There has been a lot of posts about Unity Lifetime Managers but I have yet to find someone state a good rule of thumb for "in these cases you should always use X". Let me describe my application, I have an ASP.NET MVC 4 Web Application. I…
13
votes
4 answers

Creating objects using Unity Resolve with extra parameters

I'm using Prism, which gives be the nice Unity IoC container too. I'm new to the concept, so I haven't gotten my hands all around it yet. What I want to do now is to create an object using the IoC container, but passing an extra parameter too. Allow…
stiank81
  • 25,418
  • 43
  • 131
  • 202
13
votes
2 answers

How to configure HttpClient via Unity container?

I'm trying to register an instance of HttpClient object with the unity container so that it can be used throughout the app, but running into the error - "The type HttpMessageHandler does not have an accessible constructor." Here is the code I use…
camelCaseWarrior
  • 369
  • 2
  • 8
  • 19
13
votes
2 answers

WebApi.UnityDependencyResolver does not implement Microsoft.Practices.ServiceLocation.IServiceLocator. Parameter : commonServiceLocator

I try to run the following code: using System.Web.Http; using System.Web.Mvc; using Conduit.Mam.ClientSerivces.Dal.Configuration; using MamInfrastructure.Common.Dal; using MamInfrastructure.Common.Logger; using MamInfrastructure.Logger; using…
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
13
votes
3 answers

UnityContainer() LoadConfiguration not found

I have this code: 1: IUnityContainer container = new UnityContainer(); 2: container.LoadConfiguration(); Line 1 works, but line 2 not. LoadConfiguration is not found as a member. I think, i have register all unity-dlls. Why is LoadConfiguration()…
Jens O
  • 131
  • 1
  • 1
  • 3
12
votes
2 answers

Configuring Unity to resolve a type that takes a decorated dependency that has a parameter that varies with the type into which it is injected

This is a fairly straight forward decorator pattern scenario, with the complication that the decorated type has a constructor parameter that is dependent on the type into which it is being injected. I have an interface like this: interface IThing { …
12
votes
3 answers

Using Unity Dependency Injection with WCF services

I have the following after doing some research on other questions: MyServiceHost: public class MyServiceHost : ServiceHost { public MyServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses) :…
Elim99
  • 663
  • 3
  • 17
  • 34
12
votes
1 answer

Avoiding Service Locator Antipattern with legacy app not designed for IOC

I have read often that Service Locators in IOC are an anti-pattern. Last year we introduced IOC (Ninject specifically) to our application at work. The app is legacy, it's very big and it's fragmented. There are lots of ways a class, or a chain of…
12
votes
4 answers

UnityContainer and internal constructor

I have a class with internal constructor and want to Resolve it from Unity (2.0). public class MyClass { internal MyClass(IService service) { } } then I'm doing _container.Resolve(); when I do so I have an exception Exception is:…
Shaddix
  • 5,901
  • 8
  • 45
  • 86