1
votes

This is kind of a follow to my other post about Unity (http://stackoverflow.com/questions/3998559/irepository-iservice-unity-in-an-asp-net-mvc-application-reference-question). Basically I'm trying to register a generic type in the web.config. I've read a few posts and this SEEMS like it is configured correctly.


<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
 <alias alias="IService" type="Porject.Service.IService`1, Porject.Service" />
 <alias alias="PropertyService" type="Porject.Service.PropertyService, Porject.Service"/>

 <container>
  <register type="IService[Property]" mapTo="PropertyService" />
 </container>
</unity>

But I get this error:

Server Error in '/' Application.

The type name or alias IService[Property] could not be resolved. Please check your configuration file and verify this type name. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The type name or alias IService[Property] could not be resolved. Please check your configuration file and verify this type name.

Source Error:

Line 33: Line 34: UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); Line 35: section.Configure(_container); Line 36: Line 37:

Source File: D:\Projects\Ex2\NCI\TREB\src\WebUI\UnityControllerFactory.cs Line: 35

3

3 Answers

2
votes

I think you need to specify which type is the generic using when declaring the alias. I've done a sample for How to inject a generic IList<> of objects using Unity 2 XML config? like the following.

List Suppose you want to use resolve from "IList" to "List".

1 First you will have to prepare your configuration file, in this case it's using signed references to IList as well as List.


<?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">
    <alias alias="StringListContract" type="System.Collections.Generic.IList`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <alias alias="ConcreteStringList" type="System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <container>
      <register type="StringListContract" mapTo="ConcreteStringList">
        <lifetime type="ContainerControlledLifetimeManager" />
        <constructor />
      </register>
    </container>
  </unity>
</configuration>

2 Use the container to resolve the dependency ;-)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
namespace UnityTerminal
{


    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            container.LoadConfiguration();
            {
                var concreteStringList = container.Resolve<System.Collections.Generic.IList<System.String>>();
                concreteStringList.Add("Hello World");
            }
            {
                var concreteStringList = container.Resolve<System.Collections.Generic.IList<System.String>>();
                Console.WriteLine("{0}", concreteStringList[0]);
            }
            Console.ReadKey();
        }
    }
}

That should be enough, I hope this works ;-) Best regards, Herber

0
votes

There's no alias or namespace / assembly shortcuts for the type Property. Where is this type defined? Fix that and it should work.

0
votes

A config file is meant to be read by humans thus don't make your life difficult. The following line is really hard to maintain

<alias alias="IService" type="Porject.Service.IService`1, Porject.Service" />