Specifying System.Int32 in typeConfig

Apr 30, 2008 at 7:59 PM
In typeConfig I am specifiying Int32 in typeConfig and Unity is complaining cast not valid.

Unity config file:

<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<unity>
<typeAliases>
<!-- Lifetime manager types -->
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
Microsoft.Practices.Unity" />
<typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,
Microsoft.Practices.Unity" />
<typeAlias alias="NameServiceConfiguration"
type="UnityExample.NameServiceConfiguration,UnityExample" />

</typeAliases>
<containers>
<container name="Unity">
<types>
<type type="NameServiceConfiguration" mapTo="NameServiceConfiguration">
<lifetime type="singleton" />
<typeConfig extensionType = "Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
Microsoft.Practices.Unity.Configuration" >
<constructor>
<param name="env" parameterType="System.String">
<value value =""/>
</param>
<param name="iparam" parameterType="System.Int32">
<value value="1000000"/>
</param>
</constructor>
</typeConfig>
</type>
</types>
</container>
</containers>
</unity>
</configuration>


namespace UnityExample
{
public class NameServiceConfiguration
{
private string _environment;
private int _i = 0;

public NameServiceConfiguration(string env)
{
this._environment = env;
}


I am loading the Unity.xml before this. it is read ok.


NameServiceConfiguration ns = unity.Resolve<NameServiceConfiguration>();
if (ns != null)
{
Trace.WriteLine("ok");
}
else
{
Trace.WriteLine("not ok");
}


public NameServiceConfiguration(string env, int iparam)
{
this._environment = env;
_i = iparam;
}

}
}




May 1, 2008 at 12:29 AM
Try the following in code and it should work

int expectedInt = 37;
string expectedString = "Hey there";
double expectedDouble = Math.PI;

IUnityContainer container = new UnityContainer()
.Configure<InjectedMembers>()
.ConfigureInjectionFor<GuineaPig>(
new InjectionConstructor(expectedInt, expectedString, expectedDouble))
.Container;

GuineaPig pig = container.Resolve<GuineaPig>();
Assert.IsTrue(pig.ThreeArgumentConstructorCalled);
Assert.AreEqual(expectedInt, pig.I);
Assert.AreEqual(expectedDouble, pig.D);
Assert.AreEqual(expectedString, pig.S);

        public class GuineaPig
        {
            public bool DefaultConstructorCalled = false;
            public bool OneArgumentConstructorCalled = false;
            public bool ThreeArgumentConstructorCalled = false;
 
            public object O;
            public int I;
            public string S;
            public double D;
 
            public GuineaPig()
            {
                DefaultConstructorCalled = true;
            }
 
            public GuineaPig(object o)
            {
                OneArgumentConstructorCalled = true;
                O = o;
            }
 
            public GuineaPig(int i, string s, double d)
            {
                ThreeArgumentConstructorCalled = true;
                I = i;
                S = s;
                D = d;
            }
 
            public int IntProperty
            {
                get { return I; }
                set { I = value; }
            }
 
            public object ObjectProperty
            {
                get { return O; }
                set { O = value; }
            }
 
            public void InjectMeHerePlease(string s)
            {
                S = s;
            }
        }
May 1, 2008 at 10:48 AM
Hello there,

I have tried it with the config and I am getting the same error as you. I had a look on the web and could not see an example using an int, it was failing with doubles as well. I also made iparam a string and it worked.

Cheers
Yaz
May 1, 2008 at 1:29 PM
Thanks yazidareski. Looks like it is a bug in Unity.
Or we have to specify int in special way ?
May 2, 2008 at 2:47 AM
Change this:

<value value="1000000"/>

to this:

<value value="1000000" type="System.Int32" />

Yes, it's annoying. The "parameterType" attribute is used to match the method signature. The "type" attribute in the value is used to figure out which type converter to use.
May 2, 2008 at 10:10 AM


I have tried this

UnityConfigurationSection section = GetUnitySection("Unity");
 
 private UnityConfigurationSection GetUnitySection(string baseName)
 {
     SysConfiguration config = OpenConfigFile(baseName);
     return (UnityConfigurationSection)config.GetSection("unity");
 }
 
<constructor>
	<param name="env" parameterType="System.String">
<value value =""/>
</param>
	<param name="iparam" parameterType="System.Int32">
	<value value="1000000"  type="System.Int32"/>
</param>
</constructor>

I get an error
Unrecognized attribute 'Type'. Note that attribute names are case-sensitive
May 2, 2008 at 10:17 AM
Sorry Missed something:
I have tried this:

SysConfiguration = System.Configuration.Configuration;
 
 UnityConfigurationSection section = GetUnitySection(configFileName);
 
 private UnityConfigurationSection GetUnitySection(string baseName)
 {
     SysConfiguration config = OpenConfigFile(baseName);
     return (UnityConfigurationSection)config.GetSection("unity");
 }
 
private SysConfiguration OpenConfigFile(string baseName)
{
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = baseName + ".config";
            return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
 
 
<constructor>
	<param name="env" parameterType="System.String">
<value value =""/>
</param>
	<param name="iparam" parameterType="System.Int32">
	<value value="1000000"  type="System.Int32"/>
</param>
</constructor>

I get an error
Unrecognized attribute 'Type'. Note that attribute names are case-sensitive
May 2, 2008 at 1:11 PM
My mistake it does works now, I think the config was cached because it had an error.
May 2, 2008 at 1:47 PM
after specifying type=System.Int32 it works.

<container name="Unity">
<types>
<type type="NameServiceConfiguration" mapTo="NameServiceConfiguration">
<lifetime type="singleton" />
<typeConfig extensionType = "Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
Microsoft.Practices.Unity.Configuration" >
<constructor>
<param name="env" parameterType="System.String">
<value value ="test"/>
</param>
<param name="iparam" parameterType="System.Int32">
<value value="100000" type="System.Int32"/>
</param>
</constructor>
</typeConfig>
</type>
</types>
</container>