c# - Unity Ioc Configuration Error -
i have following problem:
i using ioc unity c#. case implements ioc following:
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 { { return contenedor; } } /// <summary> /// create instance of object implements tservicio. /// </summary> /// <typeparam name = "tservicio">type of service want resolve</typeparam> /// <returns></returns> public tservicio resolver<tservicio>() tservicio : class { return unitycontainer.resolve<tservicio>(); } } }
my app.config following:
<?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>
now example class negocio
following:
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); } } }
and it's interface is:
namespace transpritr.interfaz { public interface inegocio { void load(); } }
my problem when try invoke negocio
class via ioc var interfaz = ioccontainer.contenedor.resolver<inegocio>();
, gives me following problem:
> 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:
the problem based on earlier project did work, implement in new project, doesn't work. don't know why, , need help.
note: translated english, see original, go "edited" link below.
i start with:
- ensure using correct configuration (in .config) version of unity
- is
configuration.unity.container.register
valid element? - try using the: enterprise library configuration tool
- is
- ensure of required dependencies discoverable.
references
Comments
Post a Comment