简体   繁体   English

跨 MVC、WebAPI 和 WebForms 共享同一个 Unity 容器?

[英]Share the same Unity container across MVC, WebAPI, and WebForms?

Issue问题

The title probably doesn't use the correct terminology, but I think you should be able to figure out what I mean: I want to register the same services for use across all three technologies in the same project.标题可能没有使用正确的术语,但我认为您应该能够理解我的意思:我想注册相同的服务,以便在同一个项目中的所有三种技术中使用。 I've read that it is possible to use Unity in a project with both MVC and Web API.我读过可以在同时具有 MVC 和 Web API 的项目中使用 Unity。 But my project is a bit more interesting than that.但我的项目比这更有趣。

Background背景

I have a legacy (ancient) Webforms project running on .NET 4.7.2.我有一个在 .NET 4.7.2 上运行的遗留(古老)Webforms 项目。 We are actively working on converting the whole thing to Angular.我们正在积极努力将整个东西转换为 Angular。 The final goal being to convert to .NET Core/.NET 5 -- having only the Angular project and Web API in the final solution.最终目标是转换为 .NET Core/.NET 5 - 在最终解决方案中只有 Angular 项目和 Web ZDB974238714CA8ADE4FACE38。 My plan is to convert each .aspx page to an Angular component(s) and replace all the code-behind data access to Web API calls, one-at-a-time as I touch them.我的计划是将每个.aspx页面转换为 Angular 组件并替换所有对 Web API 调用的代码隐藏数据访问,一次我触摸它们。 We are only using ASP.NET MVC to make it easier to authenticate/authorize against our IdentityServer 4 implementation.我们仅使用 ASP.NET MVC 来更轻松地针对我们的 IdentityServer 4 实现进行身份验证/授权。

What I know so far到目前为止我所知道的

I'm pretty sure I need to install the following three packages to get DI working in all three:我很确定我需要安装以下三个包才能让 DI 在所有三个包中工作:

  • Unity.Mvc
  • Unity.AspNet.WebApi
  • AspNet.WebFormsDependencyInjection.Unity

I've seen articles that describe how to use Unity with both ASP.NET MVC and Web API in the same project.我看过描述如何在同一个项目中将 Unity 与 ASP.NET MVC 和 Web API 一起使用的文章。 But I'm still a little fuzzy on the whole thing.但我对整个事情还是有点模糊。

Question问题

How do I wire up Unity for all three (ASP.NET MVC, Web API and Webforms) and register my services once for all three?如何为所有三个(ASP.NET MVC、Web API 和 Webforms)连接 Unity 并为所有三个注册一次我的服务?

Alright well I've got Unity working in all three technologies.好吧,我已经让 Unity 在所有三种技术中工作。 Unity.Mvc and Unity.AspNet.WebApi were easy since they both share the same scaffolded UnityConfig class in my project. Unity.MvcUnity.AspNet.WebApi很简单,因为它们在我的项目中共享相同的脚手架UnityConfig class。 The UnityConfig class creates the UnityContainer and keeps a static reference to it. UnityConfig class 创建UnityContainer并保留对它的static引用。

This package: AspNet.WebFormsDependencyInjection.Unity is no longer maintained and has some problems (project doesn't compile with this referenced).这个 package: AspNet.WebFormsDependencyInjection.Unity不再维护并且有一些问题(项目没有编译这个引用)。 But I found an article (which I cannot find the URL to) that said to use HouseofCat.DependencyInjection.WebForms which at least compiles.但我发现一篇文章(我找不到 URL 到)说使用至少可以编译的HouseofCat.DependencyInjection.WebForms However it creates its own instance of UnityContainer .但是,它会创建自己的UnityContainer实例。 So I had to look at what that code was doing and adapt it to reference the shared container from my UntityConfig .因此,我必须查看该代码在做什么,并对其进行调整以引用我的UntityConfig中的共享容器。

Now I've got ctor injection in my API Controllers, MVC and WebForms.现在我在我的 API 控制器、MVC 和 WebForms 中进行了 ctor 注入。 And I only have to register services once.而且我只需要注册一次服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM