Unity Ioc配置错误
本文关键字:错误 配置 Ioc Unity | 更新日期: 2023-09-27 18:10:16
我有以下问题:
我使用IOC Unity和c#。实现IOC的情况如下:
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
namespace TansPritr.Ioc
{
public class IocContainer
{
private static readonly IocContainer contenedor = new IocContainer();
private readonly IUnityContainer unityContainer;
private IocContainer()
{
unityContainer = new UnityContainer();
unityContainer.LoadConfiguration("miContenedor");
}
public static IocContainer Contenedor
{
get { return contenedor; }
}
/// <summary>
/// Create an instance of an object which implements TServicio.
/// </summary>
/// <typeparam name = "TServicio">Type of service which we want to resolve</typeparam>
/// <returns></returns>
public TServicio Resolver<TServicio>() where TServicio : class
{
return unityContainer.Resolve<TServicio>();
}
}
}
我的App.Config如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<container>
<register type="TransPritr.Interfaz.INegocio, TransPritr.Interfaz" mapTo="TransPritr.Buisness.Negocio, TransPritr.Buisness"/>
<register type="TransPritr.Interfaz.ISqlServer,TransPritr.Interfaz" mapTo="TransPritr.DataAcces.SqlServer, TransPritr.DataAcces"/>
</container>
</unity>
</configuration>
现在,例如类Negocio
如下:
using TransPritr.Interfaz;
namespace TransPritr.Buisness
{
public class Negocio : INegocio
{
//private Collection<string[]> GetAllTransPritr()
//{
// //var dataControl = IocContainer.Contenedor.Resolver<ISqlServer>();
// // return dataControl.GetAllTransPritr();
// return new Collection<string[]>();
//}
//private void CreateFile(Collection<string[]> lista)
//{
// var filename = ConfigurationManager.AppSettings["FileName"];
// StreamWriter write = new StreamWriter(filename);
// int cont = 1;
// foreach (var linea in lista)
// {
// string temp = cont.ToString() + "|";
// foreach(var texto in linea)
// {
// temp += texto + "|";
// }
// write.WriteLine(temp);
// }
//}
public void Load()
{
//var lista = GetAllTransPritr();
// CreateFile(lista);
}
}
}
它的界面是:
namespace TransPritr.Interfaz
{
public interface INegocio
{
void Load();
}
}
我的问题是,当我试图通过IOC与var interfaz = IocContainer.Contenedor.Resolver<INegocio>();
调用Negocio
类时,它给了我以下问题:
> No se controló System.TypeInitializationException Message=Se produjo
> una excepción en el inicializador de tipo de
> 'TansPritr.Ioc.IocContainer'. Source=TansPritr.Ioc
> TypeName=TansPritr.Ioc.IocContainer StackTrace:
> en TansPritr.Ioc.IocContainer.get_Contenedor()
> en TransPritr.Program.Main(String[] args) en D:'Altiuz'Beny'TRANS_PRITR'TRANS_PRITR'TransPritr'Program.cs:línea 14
> en System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
> en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
> en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
> en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
> en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
> en System.Threading.ThreadHelper.ThreadStart() InnerException: System.ArgumentNullException
> Message=El valor no puede ser nulo. Nombre del parámetro: section
> Source=Microsoft.Practices.Unity
> ParamName=section
> StackTrace:
> en Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull(Object
> argumentValue, String argumentName) en
> e:'Builds'Unity'UnityTemp'Compile'Unity'Unity'Src'Utility'Guard.cs:línea
> 33
> en Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer
> container, UnityConfigurationSection section, String containerName) en
> e:'Builds'Unity'UnityTemp'Compile'Unity'Unity.Configuration'Src'UnityContainerExtensions.cs:línea
> 35
> en Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer
> container, String containerName) en
> e:'Builds'Unity'UnityTemp'Compile'Unity'Unity.Configuration'Src'UnityContainerExtensions.cs:línea
> 52
> en TansPritr.Ioc.IocContainer..ctor() en D:'Altiuz'Beny'TRANS_PRITR'TRANS_PRITR'TansPritr.Ioc'IocContainer.cs:línea
> 14
> en TansPritr.Ioc.IocContainer..cctor() en D:'Altiuz'Beny'TRANS_PRITR'TRANS_PRITR'TansPritr.Ioc'IocContainer.cs:línea
> 8
> InnerException:
问题是我基于做工作的早期项目,但现在我在一个新项目中实现它,它不起作用。我不知道为什么,需要帮助。
注意:我把这篇文章翻译成了英文,要看原文,请到下面的"编辑"链接。
我会这样开头:
- 确保你使用正确的配置(在。config中)为你的版本的Unity
-
configuration.Unity.container.register
是一个有效的元素吗? - 尝试使用:企业库配置工具
-
- 确保所有必需的依赖都是可发现的。
引用
- 企业库配置工具
- 输入配置信息
- MSDN: Unity Container