0

我有以下问题:

我正在使用带有 C# 的 IOC Unity。实现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:

问题是我基于一个早期的项目,它确实有效,但现在我在一个新项目中实现它,它不起作用。我不知道为什么,需要帮助。


注意:我将其翻译成英文,要查看原文,请转到下面的“已编辑”链接。

4

1 回答 1

0

我将从:

  • 确保为您的Unity版本使用正确的配置(在 .config 中)
  • 确保所有必需的依赖项都是可发现的。

参考

于 2013-04-19T20:47:48.837 回答