我有以下问题:
我正在使用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();
}
}
我的问题是,当我尝试使用Negocio
通过IOC调用var interfaz = IocContainer.Contenedor.Resolver<INegocio>();
课程时,它会给我以下问题:
> 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:
问题是我基于做工作的早期项目,但现在我在新项目中实现它,它不起作用。我不知道为什么,需要帮助。
注意:我将其翻译成英文,看原文,转到下面的“编辑”链接。
答案 0 :(得分:0)
我会从:
开始configuration.Unity.container.register
是否为有效元素?<强>参考强>