猿问

使用Unity配置文件映射出错

初次使用Unity框架,在用代码配置写Demo的时候成功执行,但是当使用配置文件映射时总是提示名字不能被解析,反复看找不出原因,请大家帮忙看看。

以下是C#代码(控制台应用程序,测试代码全部写在Program.cs里面)

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection config = ConfigurationManager.GetSection("unity") as UnityConfigurationSection;
            config.Configure(container);
            //container.RegisterType<ILogger, DatabaseLogger>();
            ILogger logger = container.Resolve<ILogger>();
            logger.Write("Hello world!");
        }
    }

    public interface ILogger
    {
        void Write(string message);
    }
    public class DatabaseLogger : ILogger
    {
        [Dependency]
        public ILogger Logger { get; set; }
        public void Write(string message)
        {
            Console.WriteLine(String.Format("Message:{0}", message));
            Console.WriteLine("Target:Database");
        }
    }
}

以下是App.config配置文件内容

<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity>
    <containers>
      <container>
        <types>
          <type type="ConsoleApplication.ILogger" mapTo="ConsoleApplication.DatabaseLogger"/>
        </types>
      </container>
    </containers>
  </unity>
</configuration>

错误提示内容:

未处理的异常: System.InvalidOperationException: The type name or alias ConsoleA
pplication.ILogger could not be resolved. Please check your configuration file a
nd verify this type name.
在 Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverI
mpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails)
...

HUX布斯
浏览 632回答 2
2回答

婷婷同学_

算了,自己搞了
随时随地看视频慕课网APP
我要回答