18

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() { UnityContainer DadContainer= new UnityContainer(); Dad newdad = DadContainer.Resolve<Dad>(); newdad.Name = "chris"; Assert.AreEqual(newdad.Name,"chris"); } 

This is the error I am getting

"InvalidOperationException - the type String cannot be constructed. You must configure the container to supply this value" 

How do I configure my DadContainer for this assertion to pass? Thank you

1
  • Take a look at constructor injection. :) Commented Jun 30, 2013 at 14:14

2 Answers 2

28

You should provide a parameterless constructor:

public class Dad { public string Name { get; set; } public Dad() { } public Dad(string name) { Name = name; } } 

If you can't provide a parameterless constructor, you need to configure the container to provide it, either by directly registering it with the container:

UnityContainer DadContainer = new UnityContainer(); DadContainer.RegisterType<Dad>( new InjectionConstructor("chris")); 

or through the app/web.config file:

<configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/> </configSections> <unity> <containers> <container> <register type="System.String, MyProject"> <constructor> <param name="name" value="chris" /> </constructor> </register > </container> </containers> </unity> 
Sign up to request clarification or add additional context in comments.

3 Comments

its as simple as that? LOL ... but what if I can't and the constructor must have a string parameter?( for some reason that I can't think of right now)
This does not answer the question.
You can definitely use an InjectionConstructor to supply the name. However, that will always use the same name for all Dad instances. If you want a different value you can use a ResolverOverride: DadContainer.Resolve<Dad>(new ParameterOverride("name", nameOfDad));
-1

10 Years later and I've just stumbled upon this same challenge. A lot of people ask this question (or a very similar question on SO) but there are few answers and none that are as direct as what I found in the Unity documentation.

Solution Discovery

I too had a class that needed a string passed in upon construction.

The answer is actually quite simple, but somewhat confusing.

Here's the answer from UnityContainer.org * with my alterations to refer to the OP's class (instead of the sample class used by the docs).

Dad is a simple class with [one] public constructors. When Resolve() is called, Unity will evaluate available constructors and select one with longest list of parameters it can satisfy with dependencies. It will create all required dependencies and pass them to selected constructor during initialization.

In this particular case Unity will select [available] constructor with parameter name of type String. However, constructor Dad(string name) has parameter of type String which is a primitive type. Unity can not create primitive types by itself. If you want to make these available for dependency injection you would need to register them with the container.

// Register string instance container.RegisterInstance("Frank"); // Resolve Dad container.Resolve<Dad>(); 

That's it! 🤯🤯

However, I'm not sure what happens if you have more than one type with a constructor which requires a string.
How do you know which string will be used with which constructor?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.