我有以下统一构造方法:
public static IUnityContainer CreateContainer()
{
UnityContainer container = new UnityContainer();
container.LoadConfiguration();
.......
}
然后是一个接口和一个实现类:
namespace MyCompany.Web.Areas.MyApp.Common
{
public interface ISession
{
}
}
namespace MyCompany.Web.Areas.MyApp.Common
{
public class SessionHandler : ISession
{
}
}
而不是这样做:
Container.RegisterType<ISession, SessionHandler>(new ContainerControlledLifetimeManager()) ;
我想使用container.LoadConfiguration()从web.config加载上面的RegisterType配置。 但它似乎不起作用
的web.config:
<unity>
<containers>
<container>
<types>
<type type="MyCompany.Web.Areas.MyApp.Common.ISession" mapTo="MyCompany.Web.Areas.MyApp.Common.SessionHandler">
<lifetime type="singleton"/>
</type>
</types>
</container>
</containers>
</unity>
例外:
无法解析类型名称或别名MyProject.Web.Areas.MyApp.Common.ISession。请检查配置文件并验证此类型名称。
描述:在执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息: System.InvalidOperationException:无法解析类型名称或别名MyProject.Web.Areas.MyApp.Common.ISession。请检查您的配置文件并验证此类型名称。
来源错误:
第33行:{ 第34行:UnityContainer容器=新的UnityContainer(); 第35行:container.LoadConfiguration(); 第36行: 第37行://container.RegisterType(new ContainerControlledLifetimeManager());
堆栈追踪:
[InvalidOperationException:无法解析类型名称或别名MyProject.Web.Areas.MyApp.Common.ISession。请检查您的配置文件并验证此类型名称。] e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ ConfigurationHelpers \ TypeResolverImpl.cs中的Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias,Boolean throwIfResolveFails):110 e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ ConfigurationHelpers \ TypeResolver.cs中的Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolver.ResolveType(String typeNameOrAlias):47 e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ RegisterElement.cs中的Microsoft.Practices.Unity.Configuration.RegisterElement.GetRegisteringType():121 e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ RegisterElement.cs中的Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer容器):88 e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ ContainerConfiguringElement.cs中的Microsoft.Practices.Unity.Configuration.ContainerConfiguringElement.ConfigureContainerInternal(IUnityContainer容器):43 Microsoft.Practices.Unity.Configuration。&lt;&gt; c__DisplayClass1.b__0(ContainerConfiguringElement元素)在e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ ContainerElement.cs:114 e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity \ Src \ ObjectBuilder \ Utility \ EnumerableExtensions.cs中的Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach(IEnumerable
1 sequence, Action
1动作):36 e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ ContainerElement.cs中的Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer容器):110 e. \ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ UnityConfigurationSection.cs中的Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer容器,String configuredContainerName):151 e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ UnityContainerExtensions.cs中的Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer容器,UnityConfigurationSection部分,String containerName):37 e:\ Builds \ Unity \ UnityTemp \ Compile \ Unity \ Unity.Configuration \ Src \ UnityContainerExtensions.cs中的Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer容器):64 C:\ Workspace \ MCDev \ MyApp \ MyApp \ MyProject.Web \ ApplicationContainer.cs中的MyProject.Web.ApplicationContainer.CreateContainer():35 C:\ Workspace \ MCDev \ MyApp \ MyApp \ MyProject.Web \ ApplicationContainer.cs中的MyProject.Web.ApplicationContainer.Initialize(HttpContext上下文):53 MyProject.Web.MvcApplication.Application_Start()在C:\ Workspace \ MCDev \ MyApp \ MyApp \ MyProject.Web \ Global.asax.cs:41
答案 0 :(得分:10)
我不认为它知道要查看什么dll:
<unity>
<containers>
<container>
<types>
<type type="MyCompany.Web.Areas.MyApp.Common.ISession, MyCompany" mapTo="MyCompany.Web.Areas.MyApp.Common.SessionHandler, MyCompany">
<lifetime type="singleton"/>
</type>
</types>
</container>
</containers>
</unity>