C# 当我在Web API中使用from IOC(UnityConfig)时,我不能调用该构造函数
当我将from-IOC与unity容器一起使用时,我可以调用current方法,但我的字段为null,因为没有调用默认构造函数C# 当我在Web API中使用from IOC(UnityConfig)时,我不能调用该构造函数,c#,oop,asp.net-web-api2,ioc-container,C#,Oop,Asp.net Web Api2,Ioc Container,当我将from-IOC与unity容器一起使用时,我可以调用current方法,但我的字段为null,因为没有调用默认构造函数 public static class UnityConfig { public static void RegisterComponents() { var container = new UnityContainer(); // register all your components with th
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<IUnitOfWork, TestJirProjectContext>(new HierarchicalLifetimeManager());
container.RegisterType<IApplicationServiceTest, ApplicationServiceTest.ApplicationServiceTest>(new HierarchicalLifetimeManager());
#region ApplicationService
container.RegisterType<IPersonApplicationService, PersonApplicationService>(new HierarchicalLifetimeManager());
container.RegisterType<IEntityMapper<Person, PersonDto>, PersonMapper>(new HierarchicalLifetimeManager());
//
container.RegisterType<IBookApplicationService, BookApplicationService>(new HierarchicalLifetimeManager());
container.RegisterType<IEntityMapper<Book, BookDto>, BookMapper>(new HierarchicalLifetimeManager());
#endregion
#region Services
container.RegisterType<IPersonService, PersonService>(new HierarchicalLifetimeManager());
container.RegisterType<IBookService, BookService>(new HierarchicalLifetimeManager());
#endregion
#region Repositories
container.RegisterType<IPersonRepository, PersonRepository>(new HierarchicalLifetimeManager());
container.RegisterType<IBookRepository, BookRepository>(new HierarchicalLifetimeManager());
#endregion
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
而且
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
UnityConfig.RegisterComponents();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
public class BookController : ApiController
{
public BookController()
{
}
private readonly IApplicationServiceTest ApplicationService;
public BookController(IApplicationServiceTest applicationService)
{
ApplicationService = applicationService;
}
[HttpGet]
public BookDto GetById()
{
int id = 1;
var res = ApplicationService.GetById(id: id);
return res;
}
}
当调用这个api控制器时,我的ApplicationService
为null,我得到的错误对象引用在这一行为null
ApplicationService.GetById(id:id)代码>
另一方面,当我用ApiController替换控制器时,它工作正常。我不确定我是否完全理解(我们是否需要所有这些代码来理解您的问题?)。
IoC设计的一个原则是使用构造函数传递依赖项,而不是使用第二个空构造函数来简单地实例化/获取该依赖项(这没有任何用途)。因此,每次需要调用并传递依赖项时,都需要这样做
编辑:我没有看到您的BookController及其接口也被添加到Unity容器中,因此我不确定是否理解您的问题,这也可能是您的构造函数没有被调用的原因。我不确定我是否完全理解(我们是否需要所有这些代码来理解您的问题?)。
IoC设计的一个原则是使用构造函数传递依赖项,而不是使用第二个空构造函数来简单地实例化/获取该依赖项(这没有任何用途)。因此,每次需要调用并传递依赖项时,都需要这样做
编辑:我没有看到您的BookController及其接口也被添加到Unity容器中,因此我不确定是否理解您的问题,这也可能是您的构造函数没有被调用的原因。好的,我同意您的看法。我只想说,mvc控制器中的依赖项注入工作正常,但在WebAPI控制器中没有。顺便说一下,我刚刚安装了unity.mvc5,我想问题就在这里好的,我同意你的看法。我只想说,mvc控制器中的依赖项注入工作正常,但在WebAPI控制器中没有。顺便说一句,我刚刚安装了unity.mvc5,我想问题在于你忘了用自定义的替换Web API的IDependencyResolver
。您确实替换了MVC的IDependencyResolver
,但这完全是两码事。您现在只能让Unity构建MVC控制器,而不能构建WebAPI控制器。再见。@Steven谢谢你。我们应该安装Unity.MVC5
和Unity.WebAPI
然后配置这个DependencyResolver.SetResolver(新的Unity.MVC5.UnityDependencyResolver(容器));GlobalConfiguration.Configuration.DependencyResolver=新的Unity.WebApi.UnityDependencyResolver(容器)
您忘记用自定义API替换Web API的IDependencyResolver
。您确实替换了MVC的IDependencyResolver
,但这完全是两码事。您现在只能让Unity构建MVC控制器,而不能构建WebAPI控制器。再见。@Steven谢谢你。我们应该安装Unity.MVC5
和Unity.WebAPI
然后配置这个DependencyResolver.SetResolver(新的Unity.MVC5.UnityDependencyResolver(容器));GlobalConfiguration.Configuration.DependencyResolver=新的Unity.WebApi.UnityDependencyResolver(容器)代码>
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
UnityConfig.RegisterComponents();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
public class BookController : ApiController
{
public BookController()
{
}
private readonly IApplicationServiceTest ApplicationService;
public BookController(IApplicationServiceTest applicationService)
{
ApplicationService = applicationService;
}
[HttpGet]
public BookDto GetById()
{
int id = 1;
var res = ApplicationService.GetById(id: id);
return res;
}
}