posinformatique.aspnet.webforms.dependencyinject เป็นไลบรารีเพื่อเพิ่มการสนับสนุนคอนเทนเนอร์ IOC ของ microsoft.extensions.dependencyinjection สำหรับ ASP .NET Web Forms
posinformatique.aspnet.webforms.dependencyinject มีอยู่โดยตรงบนเว็บไซต์ทางการของ NuGet ในการดาวน์โหลดและติดตั้งไลบรารีไปยังโครงการ Visual Studio ของคุณโดยใช้บรรทัดคำสั่ง NUGET ต่อไปนี้
Install-Package PosInformatique.AspNet.WebForms.DependencyInjection
หลังจากเพิ่มแพ็คเกจ posinformatique.aspnet.webforms.dependencyinject ในโครงการ ASP .NET WebForms ของคุณเรียก AddServiceCollection บรรทัดต่อไปนี้ใน Application_Start ของคลาส HttpApplication ของคุณใน Global.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 :
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 Core, posinformatique.aspnet.webforms.dependencyinject ไม่รองรับบริการขอบเขต ที่มีขอบเขต ซึ่งมีอยู่ในช่วงอายุการร้องขอ HTTP
ถ้าในแอปพลิเคชัน ASP .NET ของคุณคุณโฮสต์โครงสร้างพื้นฐาน ASP .NET (โดยใช้ posinformatique.aspnetcore.server.aspnet Library เป็นต้น) คุณสามารถใช้ IServiceProvider และ IServiceCollection ภายในได้ แอปพลิเคชัน.
ตัวอย่างต่อไปนี้แสดงวิธีการนำ IServiceProvider ภายในและ IServiceCollection ของแอปพลิเคชัน ASP .NET Core มาใช้ใหม่ใน ASP .NET โดยใช้ Posinformatique.aspnetcore.server.aspnet ไลบรารี:
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 ซึ่งจะมีอยู่ใน IServiceProvider ที่สร้างขึ้นโดยโครงสร้างพื้นฐาน ASP .NET
วิธี UseServiceProvider() ช่วยให้สามารถตั้งค่าโครงสร้างพื้นฐานที่ไม่ใช่คอร์ ASP .NET เพื่อใช้การใช้งานของ IServiceProvider ซึ่งถูกสร้างขึ้นโดย IWebHost ของโครงสร้างพื้นฐาน ASP .NET Core เริ่มต้นขึ้น
ด้วยวิธีการนี้ส่วนประกอบ ASP .NET ที่ไม่ใช่คอร์และหลักจะแบ่งปันบริการ เดียวกัน ตัวอย่างเช่นหากคุณลงทะเบียนบริการ IDogManager เป็น Singleton ในบริการของ ASP .NET CORE อินสแตนซ์บริการ IDogManager จะพร้อมใช้งานและ อินสแตนซ์เดียวกัน :
โดยค่าเริ่มต้น Microsoft.extensions.dependencyinjection คอนเทนเนอร์การฉีดขึ้นอยู่กับการฉีดยาจะใช้ตัวสร้างใด ๆ ที่ตรงกับพารามิเตอร์ของการควบคุมหรือบริการเพื่อสร้างอินสแตนซ์ ในเวอร์ชัน 2.1 ของ Microsoft.extensions.DependencyInject Library, Microsoft ได้เปิดตัวแอตทริบิวต์ ActivatorUtilitiesConstructorAttribute ใหม่ซึ่งช่วยให้สามารถบังคับให้ IServiceProvider ของ Microsoft.extensions.DependenciNturetion Library
เวอร์ชัน 1.2.0 ของ posinformatique.aspnet.webforms.dependencyinject library เพิ่มการสนับสนุนของแอตทริบิวต์ activatorutitiveSconstructorattribute แม้ในทางเทคนิคแอตทริบิวต์นี้ไม่ได้ถูกเพิ่มโดยคอมไพเลอร์ 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 คอมไพเลอร์จะสร้างรหัสต่อไปนี้ซึ่งจะไม่เพิ่มแอตทริบิวต์ ActivatorUtilitiesConstructorAttribute บนตัวสร้างที่เกี่ยวข้อง:
public class usercontrolwithdependency_ascx : UserControlWithDependency
{
...
[DebuggerNonUserCode]
public usercontrolwithdependency_ascx()
{
__Init();
}
[DebuggerNonUserCode]
public usercontrolwithdependency_ascx(IDogManager dogManager)
: base(dogManager)
{
__Init();
}
...
}
รหัสที่สร้างขึ้นก่อนหน้านี้ไม่สามารถใช้งานได้อย่างถูกต้องโดย Microsoft.extensions.dependencyInject Library เพื่อเรียกตัวสร้างที่เหมาะสมตาม ActivatorutilitiesConstructorAttribute
ในเวอร์ชัน 1.2.0 , posinformatique.aspnet.webforms.dependencyinject เพิ่มการสนับสนุนอย่างเต็มรูปแบบของ activatorutilitiesconstructorattribute โดยเพิ่ม แอตทริบิวต์ โดยการสะท้อนในคลาสที่สร้างโดยคอมไพเลอร์ ASP .NET
เมื่อเปลี่ยนคอนสตรัคเตอร์ของการควบคุมผู้ใช้และการควบคุมเพื่อฉีดพึ่งพาโดยใช้ httpruntime.webobjectactivator ข้อความต่อไปนี้สามารถปรากฏขึ้นเมื่อเพิ่มการควบคุมเหล่านี้ลงในหน้าหรือการควบคุมผู้ใช้อื่น ๆ :
องค์ประกอบ 'xxxxx' ไม่ใช่องค์ประกอบที่รู้จัก สิ่งนี้สามารถเกิดขึ้นได้หากมีข้อผิดพลาดในการรวบรวมในเว็บไซต์หรือไฟล์ web.config หายไป

ในกรณีนี้ข้อผิด พลาด /คำเตือนจำนวนมากสามารถเพิ่มขึ้นได้โดย Visual Studio (แต่แอปพลิเคชันสามารถรวบรวมได้สำเร็จ)

นอกจากนี้ IntelliSense สำหรับรหัส ASPX เพื่อแก้ไขคุณสมบัติของการควบคุมที่เพิ่มขึ้นไม่ได้ผล ...
นี่เป็นปัญหาที่รู้จักกันดีของ Visual Studio และ Microsoft Seam ที่จะ พิจารณา ...
มีวิธีแก้ปัญหาซึ่งมาพร้อมกับแพ็คเกจ posinformatique.aspnet.webforms.dependencyinject โดยการเพิ่มตัวสร้างโดยไม่มีพารามิเตอร์ในการควบคุม:
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 ( ) ;
}
}ข้อสังเกต : คุณต้องเพิ่ม activatorutilitiesConstructorAttribute บนคอนสตรัคเตอร์ซึ่งต้องการบริการที่ลงทะเบียนใน Microsoft.extensions.dependencyInject Container หากไม่มีแอตทริบิวต์นี้ตัวสร้างที่ไม่มีพารามิเตอร์สามารถใช้ในระหว่างการดำเนินการของแอปพลิเคชันด้วยพฤติกรรมเริ่มต้นของ microsoft.extensions.dependencyinject
อย่าลังเลที่จะโคลนรหัสของฉันและส่งการเปลี่ยนแปลงบางอย่าง ... มันเป็นโครงการโอเพ่นซอร์สดังนั้นทุกคนยินดีที่จะปรับปรุงห้องสมุดนี้ ... โดยวิธีการที่ฉันเป็นคนฝรั่งเศส ... ดังนั้นบางทีคุณอาจจะพูดว่าภาษาอังกฤษของฉันไม่คล่องแคล่วจริงๆ ...
ฉันต้องการขอบคุณ บริษัท Dilitrust ที่จะทดสอบและให้ข้อเสนอแนะเกี่ยวกับห้องสมุดนี้สำหรับแอปพลิเคชัน ASP .NET Webforms ของพวกเขา