posinformatique.aspnet.webforms.ipendentiveindoction是一个库,用于添加Microsoft.extensions.extensions.ipplentency的IOC容器支持ASP .NET Web表单
posinformatique.aspnet.webforms.ipententiondivestion可以直接在Nuget官方网站上获得。使用以下Nuget命令行下载并安装库到您的Visual Studio项目
Install-Package PosInformatique.AspNet.WebForms.DependencyInjection
在您的ASP .NET WebForms项目上添加posinformatique.aspnet.webforms.dependentimentspect project在您的HttpApplication类中的Application_Start中的AddServiceCollection在Global.asax.cs code offem.asax.cs代码背后:
public class Global : HttpApplication
{
protected void Application_Start ( Object sender , EventArgs e )
{
ServicesConfig . RegisterServices ( this . AddServiceCollection ( ) ) ;
// Code that runs on application startup
RouteConfig . RegisterRoutes ( RouteTable . Routes ) ;
BundleConfig . RegisterBundles ( BundleTable . Bundles ) ;
}
}在App_Start文件夹中,添加了一个名为ServicesConfig的新静态类,该类允许使用Microsoft.Extensions.DependencyInjection.ServiceCollection :serviceConcollection.servicecollection:
namespace PosInformatique . AspNet . WebForms . DependencyInjection . IntegrationTests
{
using System ;
using System . Collections . Generic ;
using System . Collections . ObjectModel ;
using System . Linq ;
using Microsoft . Extensions . DependencyInjection ;
public static class ServicesConfig
{
public static void RegisterServices ( IServiceCollection serviceCollection )
{
serviceCollection . AddSingleton < IDogRepository , DogRepository > ( ) ;
serviceCollection . AddTransient < IDogManager , DogManager > ( ) ;
}
}
}您可以注册瞬态或单例范围的服务。与ASP .NET核心不同,posinformatique.aspnet.webforms.ipendentencyIndoction不支持HTTP请求寿命期间存在的范围示波器服务。
如果在您的ASP .NET应用程序中,您可以托管ASP .NET核心基础架构(例如,使用posinformatique.aspnetcore.server.server.aspnet库),您可以重复使用IServiceProvider和IServiceCollection ,并在内部由ASP .NET核心基础架构构建的ISERVICECOLLECTION与同一申请(以及同一singleton Aspers and Aspers yyspects yousseps)。 应用。
下面的示例,显示如何使用posinformatique.aspnetcore.server.server.aspnet library重复使用ASP .NET核心应用程序的内部IServiceProvider和IServiceCollection :
public class Global : System . Web . HttpApplication
{
protected void Application_Start ( object sender , EventArgs e )
{
var host = WebHost . CreateDefaultBuilder ( )
. UseAspNet ( options =>
{
options . Routes . Add ( "api" ) ;
options . Routes . Add ( "swagger" ) ;
} )
. ConfigureServices ( services =>
{
// Add the default ASP .NET non-core services
// in the IServiceCollection of ASP .NET core.
services . AddDefaultAspNetServices ( this ) ;
} )
. UseStartup < Startup > ( )
. Start ( ) ;
// Reuse the built IServiceProvider of ASP .NET Core WebHost inside
// the ASP .NET non-core infrastructure to use IoC feature.
this . UseServiceProvider ( host ) ;
}
} AddDefaultAspNetServices()方法允许将ASP .NET非核心基础架构服务(如HttpRequest或HttpContext添加到IServiceCollection中,该服务将在由ASP.NET Core Core基础架构构建的IServiceProvider中可用。
UseServiceProvider()方法允许设置ASP .NET非核心基础结构来使用IServiceProvider的实现,该实现由ASP .NET Core Infrstructure的IWebHost构建。
使用这种方法,ASP .NET非核心和核心组件将共享相同的服务。例如,如果您在ASP .NET Core的服务中注册IDogManager服务为Singleton,则可以使用IDogManager服务实例,并且相同的实例:
默认情况下,microsoft.extensions.ipententiondextiondeptive依赖性注入容器将使用匹配控件参数或服务实例化的任何构造函数。在Microsoft.extensions.ipendentightiondostion库的2.1版中,Microsoft引入了一个新的ActivatorialsConstructoratibtribute属性,该属性允许强迫Microsoft.extensions.extensions.ippentendentientIndention.ipendentiondendectionsportion IServiceProvider明确调用构造函数。
posinformatique.aspnet.webforms.ypententiondoction库的版本1.2.0添加了激活量的支持,即使从技术上讲,该属性也不会由ASP .NET编译器在生成的ASPX和ASCX视图上添加。
例如,想象一下您具有以下用户控制:
public partial class UserControlWithDependency : System.Web.UI.UserControl
{
private readonly IDogManager dogManager;
[ActivatorUtilitiesConstructor] // Will be call by the IServiceProvider of the Microsoft.Extensions.DependencyInjection library.
public UserControlWithDependency(IDogManager dogManager)
{
this.dogManager = dogManager;
}
public UserControlWithDependency()
{
}
}
加载应用程序后,ASP .NET编译器生成以下代码,该代码不会在关联的构造函数上添加ActivatorItilitysConstructoratibtribute属性:
public class usercontrolwithdependency_ascx : UserControlWithDependency
{
...
[DebuggerNonUserCode]
public usercontrolwithdependency_ascx()
{
__Init();
}
[DebuggerNonUserCode]
public usercontrolwithdependency_ascx(IDogManager dogManager)
: base(dogManager)
{
__Init();
}
...
}
先前生成的代码无法通过Microsoft.extensions.ipendentiveindoction库正确使用,以基于ActivatorialsConstructoratribute调用正确的构造函数。
在版本1.2.0中,posinformatique.aspnet.webforms.ependentiveindoction添加了通过在ASP .Net Compiler生成的类中通过反射添加反射的activatorItilitiesConstructoratibute的全部支持。
在使用httpruntime.webobjectivator更改用户控件和控件的构造函数以注入依赖关系时,将这些控件添加到页面或其他用户控件时,可以显示以下消息:
元素“ xxxxx”不是已知元素。如果网站中存在汇编错误,或者丢失了web.config文件,可能会发生这种情况。

在这种情况下,Visual Studio可以提出很多假错误/警告(但该应用程序可以成功编译)。

另外,ASPX代码编辑添加控件的属性的Intellisense不起作用...
这是视觉工作室和Microsoft Seam的已知问题,要考虑...
有解决方法,它由posinformatique.aspnet.webforms.ypentendentiondostion package提供,通过在控件中添加没有参数的构造函数:
public partial class UserControlWithDependency : System . Web . UI . UserControl
{
private readonly IDogManager dogManager ;
[ ActivatorUtilitiesConstructor ] // It is import to set this attribute to be sure this constructor will be called by the Microsoft.Extensions.DependencyInjection.
public UserControlWithDependency ( IDogManager dogManager )
{
this . dogManager = dogManager ;
}
// Avoid the errors and the warnings in the Visual Studio ASPX code designer
public UserControlWithDependency ( )
{
}
protected void Page_Load ( object sender , EventArgs e )
{
this . doggoList . DataSource = this . dogManager . GetDogs ( ) ;
this . doggoList . DataBind ( ) ;
}
}备注:您还必须在构造函数上添加ActivatorItilitysConstructoratibute,该构造器需要在Microsoft.extensions.ipplyentiveindoction容器中注册的服务。如果没有此属性,则可以在执行应用程序时使用Microsoft.extensions.ipentendentyendoction的默认行为来使用没有参数的构造函数。
请不要犹豫,克隆我的代码并提交一些更改...这是一个开源项目,因此欢迎每个人都可以改进这个库...顺便说一句,我是法国人...所以也许您会说我的英语并不是真正流利的...所以请不要犹豫,请不要修复我的资源字符串或文档... Merci ... Merci!
我要感谢Dilitrust Company测试,并向我提供了该库的ASP .NET WebForms应用程序的反馈。