Unity 2.0 Not Resolving System.Int32

Jul 1, 2011 at 1:35 PM

This is my first attempt at using Unity so forgive me if this is a dumb question. I have a very simple Unity congfiguration file with 4 mappings. When I try to configure the container I get an exception thrown with a description telling me that the container cannot resolve System.Int32 (which is the type of one of the construction parameters on one of the classes being registered). According to the Unity 2.0 documentation, the Unity container should create aliases for all of the typically used built-in types so I am lost as to what else I should be doing to register System.Int32.

Jul 3, 2011 at 7:54 PM

Please share your configuration file, it's not obvious what the problem is from your description.

Jul 4, 2011 at 6:47 PM

Here is the configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="unity"
				 type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
	</configSections>
	<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
		<namespace name="Unity1"/>
		<assembly name="Unity1"/>
		
		<container>
			<register type="Location" mapTo="LocationImpl"/>
		</container>
	</unity>
</configuration>
Here is the interface and implementation class for Location
// 2D location in the game area
    public interface Location
    {
        int X { get; set; }
        int Y { get; set; }
    }
class LocationImpl : Location
    {
        public LocationImpl()
        {
            X = 0;
            Y = 0;
        }

        public LocationImpl( int x, int y )
        {
            X = x;
            Y = y;
        }

        public int X
        {
            get;
            set;
        }

        public int Y
        {
            get;
            set;
        }
    }

The exception n being thrown is a ResolutionFailedException. Exception detail is "Resolution of the dependency failed, type = "BrickBreakCore.Location", name = "(none)".
Exception occurred while: while resolving. Exception is: InvalidOperationException - The type Int32 cannot be constructed. You must configure the container to supply this value.

Jul 4, 2011 at 9:41 PM

And there's your problem, right in the error message:

  "The type Int32 cannot be constructed. You must configure the container to supply this value."

It's not that it can't find the type, the container doesn't know WHICH values you want. Unity by default takes the constructor with the most parameters. In your case, it's the one that take two ints. But you didn't tell the container what two ints you wanted, and Unity doesn't guess. You could want 0. You could want -4. You could want 32769. So it's erroring out, saying "you must configure the container to supply this value".

You'll need to provide more information to the container. If what you've intended is that it call the default constructor instead, tell it by specifying the zero-arg constructor in your XML:

 

<container>
  <register type="Location" mapTo="LocationImpl">
    <constructor />
  </register>
</container>

If, on the other hand, you want to call the two argument constructor with specific values, give the values in the XML:
<container>
  <register type="Location" mapTo="LocationImpl">
    <constructor>
      <param name="x" value="15" />
      <param name="y" value="42" />
    </constructor>
  </register>
</container>

There's a lot more you can do with configuration, but this should give you the basics.

Jul 14, 2011 at 3:39 PM

Thanks for the info. Works fine now.