Questions tagged [unity2.0]

The Unity Application Block 2.0 (i.e. Unity Container) is a lightweight, extensible dependency injection container for .NET with support for interception.

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

Unity Container targets both .NET CLR and Silverlight.

Installing Unity Container 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();    
}
32 questions
73
votes
3 answers

Unity 2.0: How to use Resolve with ResolverOverride?

I started doing more and more work with Unity. I notice that Resolver method takes a params argument ResolverOverride. Can someone give me an example how I can use ResolverOverride or point me some other source where I can get more clues.
Vadim
  • 21,044
  • 18
  • 65
  • 101
14
votes
1 answer

NServiceBus with Unity 2.0?

Anyone using NServiceBus 2.0 successfully with Unity 2.0? I've tried to compile sources of NServiceBus.ObjectBuilder.Unity.dll against Unity 2.0 assemblies but got several compile-time errors because of changed/deleted signatures of many object…
Dmitry Schetnikovich
  • 1,752
  • 17
  • 26
10
votes
4 answers

Custom object factory extension for Unity

I am using the Unity IoC container, and I need to intercept any calls to Resolve for a certain base interface, and run my own custom code to construct those types. In other words, in the sample code below, when I call container.Resolve(), if…
Mark Heath
  • 48,273
  • 29
  • 137
  • 194
9
votes
2 answers

Unity RegisterInstance of IDisposable objects

Unity 2.0: By default RegisterInstance uses the ContainerControlledLifetimeManager. When the Unity container is disposed, it calls Dispose on the instance (if IDisposable). In my case that's not what I want. The instance is owned by and disposed by…
TrueWill
  • 25,132
  • 10
  • 101
  • 150
8
votes
2 answers

Unity Application Block, How pass a parameter to Injection Factory?

Here what I have now Container.RegisterType(); Container.RegisterType( new InjectionFactory( (c) => c.Resolve().GetUser("John"))); and get…
Arseny
  • 7,251
  • 4
  • 37
  • 52
4
votes
1 answer

Unity - how to use multiple mappings for the same type and inject into an object

I'm using Unity 2.0 and in the following code I'm trying to inject a specific tool in the Worker object. I would like to use the following code. But ofcourse there is an error "Resolution of the dependency failed". I believe I should be able to do…
3
votes
1 answer

Using Unity in WPF

I have Unity 2.0 working well within the App.xaml.cs to register and resolve within that class. The question I have is regarding a best practice. I have a number of User Controls and other classes that also need to resolve some of the same and new…
IUnknown
  • 2,596
  • 4
  • 35
  • 52
3
votes
2 answers

Type name could not be resolved. Please check config file

Trying to unit test my application using moq and unity, and I'm getting errors where my service is an interface and cannot be resolved: Error message: Test method Ecommerce_Test.Tests.LoginSuccessfulRedirectToActionLoad threw exception:…
litterbugkid
  • 3,534
  • 7
  • 36
  • 54
2
votes
1 answer

Android development requires JDK 8(1.8)64-bit

I use unity 2020.3.13, i start to work with android game. I try to set the JDK with JDK that I got from Android Studio, but i get this error: Incompatible Java version '16' Android development requires JDK 8(1.8)64-bit. Having Java Runtime…
Flitzcore
  • 166
  • 1
  • 11
1
vote
1 answer

Using Unity, how do I resolve a name value to a static class property?

I have a project which has multiple Resource (resx) files along with the accompanying .designer.cs generated files. Each of these classes has a public static property of type System.Resources.ResourceManager. What I can't seem to figure out is how…
David Savage
  • 760
  • 5
  • 15
1
vote
1 answer

Parsing XML metadata containing conditional statements with C# in Unity

I have an XML file full of events like this one with multiple statements to check. How is this action even called? I'm so lost that I don't even know where to start looking.
mllhild
  • 67
  • 7
1
vote
2 answers

Unity BuildUp fails for singleton

I am experiencing the following problem with Unity framework. We have a singleton classes in our project. They have some properties that should be injected by Unity container. Here is the code: private static SomeClass m_Instance; private…
Andrei
  • 55,890
  • 9
  • 87
  • 108
1
vote
1 answer

Injecting UI in ASP.NET- MVC using UNITY 2.0

I am using UNITY 2.0 with MVC 3. I have injected types using the container into MVC.net solution. All this while the types were non-UI component.How can I inject a type which has UI ? The solution i can think of is to write UI rendering Code…
Pushpendra
  • 820
  • 7
  • 11
1
vote
2 answers

Unity 2.0 loose dependencies - dll copying

I have a simple question that I think someone will answer very fast, but I tried to find the answer myself and couldn't (maybe just bad luck). The thing is, that I have a .NET 4 solution in VS2010 Ultimate in which I want to take advantage of Unity…
Paweł Motyl
  • 1,304
  • 10
  • 16
0
votes
1 answer

Setup Exception Handling Application Block via Unity Interception and inject exception policies

I am trying to setup Exception Handling Block for my application and would like to inject exception handling policies (defined in configuration file) at the point where I am registering modules and interception with unity in my IoC factory. For…
MuazzamAli
  • 201
  • 2
  • 11
1
2 3