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應用程序的反饋。