有什么办法可以用 Unity 3 解决这个问题吗?
我已尽一切可能绕过此消息错误,但我无法解决;我已经完成了我在谷歌搜索中看到的所有内容。
我几乎要放弃并尝试另一种 DI 解决方案。
我的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="Biblioteca" />
<assembly name="Biblioteca.Contracts" />
<assembly name="Biblioteca.Business" />
<namespace name="Biblioteca" />
<namespace name="Biblioteca.Contracts" />
<namespace name="Biblioteca.Business" />
<container>
<register type="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" />
</container>
</unity>
</configuration>
我的界面:
using Biblioteca.Transport;
using System.Linq;
namespace Biblioteca.Contracts
{
public interface IManterCategoriaBO
{
IQueryable<CategoriaDTO> GetAll();
CategoriaDTO GetById(int id);
void Insert(CategoriaDTO dto);
}
}
我的具体课程:
using Biblioteca.Contracts;
using Biblioteca.Transport;
using Biblioteca.Data;
using System;
using System.Linq;
namespace Biblioteca.Business
{
public class ManterCategoriaBO : IManterCategoriaBO
{
public CategoriaDTO GetById(int id)
{
CategoriaDTO dto = new CategoriaDTO();
ManterCategoriaDO categoriaDO = new ManterCategoriaDO();
dto = categoriaDO.GetById(1);
return dto;
}
public IQueryable<CategoriaDTO> GetAll()
{
throw new NotImplementedException();
}
public void Insert(CategoriaDTO dto)
{
throw new NotImplementedException();
}
}
}
我的 Global.asax:
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Biblioteca.Dependency;
namespace Biblioteca
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//Below is a static variable to take the unity container
//which is on a dependency project
Global.Container = Bootstrapper.Initialise();
}
}
}
我的引导程序类:
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using System.Web.Mvc;
using Unity.Mvc4;
namespace Biblioteca
{
public static class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
string path = ConfigurationManager.AppSettings["UnityConfigFilePath"].ToString();
var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = path + "\\Unity.config" };
System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
//*** this line is firing the error !!! ****
var container = new UnityContainer().LoadConfiguration(unitySection);
return container;
}
}
}
我的依赖项目静态类:
using Microsoft.Practices.Unity;
namespace Biblioteca.Dependency
{
public static class Global
{
public static IUnityContainer Container = null;
public static T Resolve<T>()
{
return Container.Resolve<T>();
}
}
}
我在 MVC 4 项目上的 UI 模型类文件。我正在使用 4.5 框架。
using Biblioteca.Contracts;
using Biblioteca.Dependency;
namespace Biblioteca.Models
{
public class LivroModel
{
public void GetAll()
{
if (Global.Container != null)
{
var categoriaBO = Global.Resolve<IManterCategoriaBO>();
categoriaBO.GetById(1);
}
}
}
}
我认为一切都是正确的。但是,我看不到这个 DI 工作,因为我在映射过程中遇到了一个错误,在我的 Bootstrapper 类 BuildUnityContainer 方法的下面一行:
var container = new UnityContainer().LoadConfiguration(unitySection);
错误是:
无法解析类型名称或别名 Biblioteca.Contracts.IManterCategoriaBO。请检查您的配置文件并验证此类型名称。
我已经仔细检查了我所有的课程,对我来说,他们没问题。或者它缺少什么?