Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 777463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:36:36+00:00 2026-05-14T19:36:36+00:00

I have a simple console application where I have the following setup: public interface

  • 0

I have a simple console application where I have the following setup:

public interface ILogger
{
   void Log(string message);
}

class NullLogger : ILogger
{
   private readonly string version;

   public NullLogger()
   {
      version = "1.0";
   }
   public NullLogger(string v)
   {
      version = v;
   }
   public void Log(string message)
   {
     Console.WriteLine("NULL> " + version + " : " + message);
   }
}

The configuration details are below:

<type type="UnityConsole.ILogger, UnityConsole" mapTo="UnityConsole.NullLogger, UnityConsole">
 <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
   <constructor>
     <param name="message" parameterType="System.String" >
        <value value="2.0" type="System.String"/>
     </param>
   </constructor>
 </typeConfig>

My calling code looks as below:

IUnityContainer container = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);
ILogger nullLogger = container.Resolve<ILogger>();
nullLogger.Log("hello");

This works fine, but once I give a name to this type something like:

<type type="UnityConsole.ILogger, UnityConsole" mapTo="UnityConsole.NullLogger, UnityConsole" name="NullLogger">
 <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
   <constructor>
     <param name="message" parameterType="System.String" >
       <value value="2.0" type="System.String"/>
     </param>
   </constructor>
 </typeConfig>

The above calling code does not work even if I explicitly register the type using

container.RegisterType<ILogger, NullLogger>();

I get the error:

{“Resolution of the dependency failed, type = \”UnityConsole.ILogger\”, name = \”\”. Exception message is: The current build operation (build key Build Key[UnityConsole.NullLogger, null]) failed: The parameter v could not be resolved when attempting to call constructor UnityConsole.NullLogger(System.String v). (Strategy type BuildPlanStrategy, index 3)”}

Why doesn’t unity look into named instances? To get it to work, I’ll have to do:

ILogger nullLogger = container.Resolve<ILogger>("NullLogger");

Where is this behavior documented?

Arun

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:36:37+00:00Added an answer on May 14, 2026 at 7:36 pm

    As you know – there are two types of instances: named(can be many of them) and default(only one can be, and it does not have names)

    Resolve( ) – does not look into named instances – it looks for default only, and if there is no default instance

    • it returns instance of T if it can be
      constructed or
    • exception if it can’t
      • in case of interfaces
      • or when class
        does not have constructor,
      • or unity can’t resolve parameter for
        constructor of this type

    So let’s look at your example. First question, simple one – Why did code stoped work when you pointed name in configuration.
    Answer is becous now there is no default instance registered for ILogger and Resolve( ) – does not look into named instances – it looks for default only.

    Second question Why does it not work after container.RegisterType<ILogger, NullLogger>();
    Answer is becouse unity is greedy when it choose constructors for the type. Generaly speaking it always takes the constructor where the number of parameters is bigger. So it took this one public NullLogger(string v) could not created string(you can see it in inner exception). There is no information about wich constructor to choose for default resolution. All information is about named. And that is why

    ILogger nullLogger = container.Resolve<ILogger>("NullLogger");
    

    works.

    This behavior documented in help wich can be downloaded fon here http://unity.codeplex.com/releases/view/18855. Or you can take a look here http://msdn.microsoft.com/en-us/library/ff649334.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function //simple function with parameters and variable function thirdfunction(a,b,c,d){ console.log(the
I have the following projects: MVC Console application Class library Windows forms application COM
I have a simple C# Console application in which I use FileSystemWatcher and Moving
i have a simple .EXE console application, written in C# via Visual Studio 2008.
I have written very very simple console application which supports some command line options.
I have a simple test application (C# console application) that does an HTTP GET
I have a batch file which starts multiple instances of simple console application (Hello
I have a simple console application that outputs a menu and waits for user
I am new to visual studio.I have created a simple console application and then
I have a simple application with the following code: FileInfo[] files = (new DirectoryInfo(initialDirectory)).GetFiles();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.