I'm trying to define an alias for PerRequestLifetimeManager, and use it in the container configuration section, but I keep getting this error: "The type name or alias PerRequestLifetimeManager could not be resolved. Please check your configuration file and verify this type name." What am I missing? The other 2 aliases work fine, but they are not in the same dll...
<!-- not working -->
<typeAlias alias="request" type="Microsoft.Practices.Unity.PerRequestLifetimeManager , Microsoft.Practices.Unity.Mvc"/>
<!-- working -->
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
<typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/>
UPDATE
Below is my unity config section. I get a "The type name or alias request could not be resolved." error.
<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="request" type="Microsoft.Practices.Unity.PerRequestLifetimeManager , Microsoft.Practices.Unity.Mvc"/>
</typeAliases>
<containers>
<container name="main">
<types>
<type type="IProvider" mapTo="ConcreteProvider">
<lifetime type="request"/>
</type>
</types>
</container>
</containers>
</unity>
Just put some very simple sample together and the below setup works for me.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration
.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="ILogger" type="ConsoleApplication2.ILogger, ConsoleApplication2" />
<alias alias="request" type="Microsoft.Practices.Unity.PerRequestLifetimeManager,
Microsoft.Practices.Unity.Mvc" />
<container name="MyContainer">
<register type="ILogger" mapTo="ConsoleApplication2.FileLogger,
ConsoleApplication2" />
</container>
</unity>
</configuration>
UPDATE 6/8/2014 for the comments below.
A. Create a new project i. C# Console App .NET v 4.5 B. Install-Package Unity C. Install-Package Unity.Mvc
Package.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommonServiceLocator" version="1.2" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Unity" version="3.5.1404.0" targetFramework="net45" />
<package id="Unity.Mvc" version="3.5.1404.0" targetFramework="net45" />
<package id="WebActivatorEx" version="2.0.5" targetFramework="net45" />
</packages>
Use the configuration file.
using Microsoft.Practices.Unity.Configuration;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.LoadConfiguration("MyContainer");
var logger = container.Resolve<ILogger>();
Console.Read();
}
}
public interface ILogger
{
}
public class FileLogger : ILogger
{
}
}