Enum value in constructor injection

Nov 5, 2008 at 3:47 PM
Hi,
I try to create an object of type "Socket". Therefore I  defined this in the configuration file:

<

 

type name="MySocket" type="System.Net.Sockets.Socket, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <
typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
        <
constructor>
            <
param name="addressFamily" parameterType="System.Net.Sockets.AddressFamily, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <
value value="InterNetwork" type="System.Net.Sockets.AddressFamily, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
            </
param>
            <
param name="socketType" parameterType="System.Net.Sockets.SocketType, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <
value value="Stream" type="System.Net.Sockets.SocketType, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
            <  /
param>
            <
param name="protocolType" parameterType="System.Net.Sockets.ProtocolType, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                    <
value value="Tcp" type="System.Net.Sockets.ProtocolType, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
            </
param>
         </
constructor>
...

The injection for parameter addressFamily fail. It's said: "The type AddressFamily does not have an accessible constructor."
Has anyone an idea?

 Thanks Jaspis

 

Nov 5, 2008 at 5:59 PM
I just took your config and put it into a simple console app, as follows:

################# Program.cs #################

using System;
using System.Configuration;
using System.Net.Sockets;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace InjectingEnums
{
    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();

            var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);

            var socket = container.Resolve<Socket>("MySocket");

        }  
    }
}

################# App.config #################

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity>
    <containers>
      <container>
        <types>
          <type name="MySocket" type="System.Net.Sockets.Socket, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <typeConfig>
              <constructor>
                <param name="addressFamily" parameterType="System.Net.Sockets.AddressFamily, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                  <value value="InterNetwork" type="System.Net.Sockets.AddressFamily, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
                </param>
                <param name="socketType" parameterType="System.Net.Sockets.SocketType, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                  <value value="Stream" type="System.Net.Sockets.SocketType, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
                </param>
                <param name="protocolType" parameterType="System.Net.Sockets.ProtocolType, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                  <value value="Tcp" type="System.Net.Sockets.ProtocolType, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
                </param>
              </constructor>
            </typeConfig>
          </type>
        </types>
      </container>
    </containers>
  </unity>
</configuration>

################################################

This includes exactly your config file.

Running against Unity 1.2, it just worked. We did make some changes around how TypeConverters were handled in 1.2; perhaps we've fixed the issue?

If my program isn't representative of what you're actually doing, could you please supply a small, complete program that demonstrates the problem?

Thanks,

-Chris

Nov 6, 2008 at 7:44 AM
Hi Chris,

just updated and now it's working fine.

Thanks a lot
Jaspis