我使用了文章“在 WebForms 应用程序中使用依赖注入” https://devblogs.microsoft.com/aspnet/use-dependency-injection-in-webforms-application/ \n该项目在项目属性中重新定位到 .NET Framework 4.7.2,并且在 web.config 中:
\n <system.web>\n <httpRuntime targetFramework="4.72" ...\nRun Code Online (Sandbox Code Playgroud)\nAspNet.WebFormsDependencyInjection.Unity NuGet 包已安装。
\n类型已在全局中注册:
public class Global : System.Web.HttpApplication\n {\n void Application_Start(object sender, EventArgs e)\n {\n var container = this.AddUnity();\n container.RegisterType<IVCRole, eVCRole>();\n }\n ...\nRun Code Online (Sandbox Code Playgroud)\n我检查了容器,它正在工作并将接口 IVCRole 映射到类 eVCRole。\nDefault.aspx.cs 已重构:
\n public partial class Default : System.Web.UI.Page\n {\n private IVCRole vcr; \n public Default(IVCRole avcr) \n { \n vcr = avcr;\n } \n protected void Page_Load(object sender, EventArgs e)\n {\n ...\nRun Code Online (Sandbox Code Playgroud)\n但是当我运行 …