posinformatique.aspnet.webforms.dependencyinjectionは、microsoft.extensions.depencencyinjectionのiocコンテナサポートを追加するライブラリです。
posinformatique.aspnet.webforms.dependencyinjectionは、Nuget公式Webサイトで直接入手できます。次のNugetコマンドラインを使用して、ライブラリをVisual Studioプロジェクトにダウンロードしてインストールするには
Install-Package PosInformatique.AspNet.WebForms.DependencyInjection
asp .net webformsプロジェクトにposinformatique.aspnet.webforms.dependencyinjectionパッケージHttpApplication追加しApplication_Start後、 AddServiceCollectionを呼び出してください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フォルダーに、 Microsoft.Extensions.DependencyInjection.ServiceCollectionを使用してサービスを登録できるServicesConfigという新しい静的クラスを追加します。
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.dependencyinjectionは、HTTPリクエストの寿命に存在するスコープスコープサービスをサポートしていません。
ASP .NETアプリケーションでASP .NETコアインフラストラクチャをホストしている場合(たとえば、posinformatique.aspnetcore.server.aspnetライブラリを使用して)、ASP .Netアプリケーションを共有するためにASP .net Instanceを共有するためにASP .NETコアインフラストラクチャによってIServiceProviderとIServiceCollection再利用できます。
次の例では、posinformatique.aspnetcore.server.aspnetライブラリを使用して、ASP .NETでホストされている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コアインフラストラクチャによって構築されたIServiceProviderで利用可能なIServiceCollectionに、 HttpRequestやHttpContextなどのASP .NET非コアインフラストラクチャサービスを追加できます。
UseServiceProvider()メソッドでは、ASP .NET非コアインフラストラクチャをセットアップして、ASP .NETコアインフラストラクチャのIWebHostによって建設されたIServiceProviderの実装を使用できます。
このアプローチにより、ASP .NET非コアとコアコンポーネントが同じサービスを共有します。たとえば、ASP .NET CoreのサービスでSingletonとしてIDogManagerサービスを登録すると、 IDogManager Serviceインスタンスが利用可能になります。
デフォルトでは、microsoft.extensions.dependencyInjection依存関係噴射コンテナは、コントロールのパラメーターまたはインスタンス化するサービスに一致するコンストラクターを使用します。 Microsoft.extensions.DependencyInjection Libraryのバージョン2.1では、Microsoftは、Microsoft.Extensions.depencencyInjectionライブラリのIServiceProvider強制的に許可する新しいActivatorutitivitionConstructorattribute属性を導入しました。
posinformatique.aspnet.webforms.dependencyinjectionライブラリのバージョン1.2.0は、ActatorutitivitionsConstructorattribute属性のサポートを技術的にも追加します。
たとえば、次のユーザーコントロールがあると想像してください。
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コンパイラが次のコードを生成します。
public class usercontrolwithdependency_ascx : UserControlWithDependency
{
...
[DebuggerNonUserCode]
public usercontrolwithdependency_ascx()
{
__Init();
}
[DebuggerNonUserCode]
public usercontrolwithdependency_ascx(IDogManager dogManager)
: base(dogManager)
{
__Init();
}
...
}
以前の生成されたコードは、Microsoft.extensions.dependencyInjectionライブラリで正しく使用することはできません。ActivatorutitivitiesConstructorattributeに基づいて正しいコンストラクターを呼び出すことはできません。
バージョン1.2.0では、posinformatique.aspnet.webforms.dependencyInjection actatorutitivitionconstructorattributeの完全なサポートを追加します。
httpruntime.webobjectactivatorを使用して依存関係を挿入するためにユーザーコントロールとコントロールのコンストラクターを変更すると、これらのコントロールをページまたは他のユーザーコントロールに追加すると、次のメッセージが表示されます。
要素「xxxxx」は既知の要素ではありません。これは、Webサイトにコンパイルエラーがある場合、またはweb.configファイルが欠落している場合に発生する可能性があります。

この場合、Visual Studioによって多くの偽のエラー/警告を提起できます(ただし、アプリケーションは正常にコンパイルできます)。

また、追加されたコントロールのプロパティを編集するASPXコードのIntelliSenseは機能しません...
これは、Visual StudioとMicrosoft Seamの既知の問題です。
Posinformatique.aspnet.webforms.dependencyInjectionパッケージをコントロールに追加することにより、次のようにコンストラクターを追加することにより、回避策があります。
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 ( ) ;
}
}備考:Microsoft.extensions.DependencyInjectionコンテナに登録されているサービスを必要とするActivatorutitivitionConstructorAttributeをコンストラクターに追加する必要もあります。この属性がないと、マイクロソフト.extensions.dependencyInjectionのデフォルトの動作を使用して、アプリケーションの実行中にパラメーターのないコンストラクターを使用できます。
私のコードをクローンしていくつかの変更を送信することを躊躇しないでください...それはオープンソースプロジェクトです。だから誰もがこのライブラリを改善することを歓迎します...私はフランス語です...だから私の英語は本当に流fluentではないことを発言するでしょう...だから私のリソースの文字列や私のドキュメントを修正することを躊躇しないでください...メルミ!
Dilitrust Companyにテストしてくれたことに感謝し、ASP .NET WebFormsアプリケーションのためにこのライブラリのフィードバックをくれました。