初次使用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 ConsoleApplication.ILogger could not be resolved. Please check your configuration file and verify this type name.在 Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails)...
添加回答
举报
0/150
提交
取消