posinformatique.aspnet.webforms.dependencyInjection은 Microsoft.extensions.dectensions.dependence -dections.dectensions.dependence -asp .NET 웹 양식을 추가하는 라이브러리입니다.
posinformatique.aspnet.webforms.dependencyInjection은 NUGET 공식 웹 사이트에서 직접 제공됩니다. 다음 Nuget 명령 줄을 사용하여 Visual Studio 프로젝트에 라이브러리를 다운로드하여 설치하려면
Install-Package PosInformatique.AspNet.WebForms.DependencyInjection
ASP Application_Start WebForms 프로젝트에 posinformatique.aspnet.webforms.dependencyInjection 패키지 HttpApplication 추가 한 후 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 Core 인프라 (예 : PosinFormatique.aspnetCore.server.aspnet 라이브러리 사용)를 호스팅하는 경우 ASP .NET Core Infrastructure에서 내부적으로 구축 된 IServiceProvider 및 IServiceCollection 재사용하여 동일한 서비스 (및 ASP 응용 프로그램 및 ASP. NET Core Instances를 공유하기 위해 내부적으로 구축 할 수 있습니다.
다음 예는 PosinFormatique.aspnetCore.server.aspnet 라이브러리를 사용하여 ASP .NET에서 호스팅 된 ASP .NET Core 응용 프로그램의 내부 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() 메소드를 사용하면 HttpRequest 또는 HttpContext 와 같은 ASP .NET 비 코어 인프라 서비스를 ASP .NET Core Infrastructure에서 구축 한 IServiceProvider 에서 사용할 수있는 IServiceCollection 에 ASP .NET 비 코어 인프라 서비스를 추가 할 수 있습니다.
UseServiceProvider() 메소드를 사용하면 ASP .NET 비 코어 인프라를 설정하여 ASP .NET 코어 인프라의 IWebHost 가 시작한 IServiceProvider 의 구현을 사용할 수 있습니다.
이 접근법을 사용하면 ASP .NET 비 코어 및 핵심 구성 요소가 동일한 서비스를 공유합니다. 예를 들어 ASP .NET Core의 서비스에서 IDogManager 서비스를 Singleton으로 등록하면 IDogManager 서비스 인스턴스를 사용할 수 있으며 동일한 인스턴스를 사용할 수 있습니다.
기본적으로 Microsoft.extensions.dependencyInjection 종속성 분사 컨테이너는 제어의 매개 변수 또는 인스턴스화 서비스와 일치하는 모든 생성자를 사용합니다. Microsoft.extensions.dependencyInjection Library의 버전 2.1 에서 Microsoft는 Microsoft.extensions의 IServiceProvider 강요 할 수있는 새로운 ActivatorUtilitiessconstructorAttribute 속성을 도입했습니다.
posinformatique.aspnet.webforms.dependencyInjection Library의 버전 1.2.0은 ActivatorUtilitiessConstructorAttribute 속성의 지원을 추가하여 기술적 으로도이 속성은 생성 된 ASPX 및 ASCX 뷰에서 ASP .NET 컴파일러에 의해 추가되지 않습니다.
예를 들어, 다음과 같은 사용자 컨트롤이 있다고 상상해보십시오.
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 컴파일러는 다음 코드를 생성하여 다음 코드를 생성합니다.이 코드는 ActivatorUtilitiessConstructOrattribute Attribute가 추가되지 않습니다.
public class usercontrolwithdependency_ascx : UserControlWithDependency
{
...
[DebuggerNonUserCode]
public usercontrolwithdependency_ascx()
{
__Init();
}
[DebuggerNonUserCode]
public usercontrolwithdependency_ascx(IDogManager dogManager)
: base(dogManager)
{
__Init();
}
...
}
이전에 생성 된 코드는 Microsoft.extensions.dependencyInjection 라이브러리에서 올바르게 사용할 수 없습니다.
버전 1.2.0 에서는 posinformatique.aspnet.webforms.dependencyInjection에서 ASP .NET Compiler가 생성 한 클래스에 반영하여 속성을 거의 추가하여 ActivatorUtilitiesConstructOrattribute의 전체 지원을 추가합니다.
httpruntime.webobjectActivator를 사용하여 종속성을 주입하기 위해 사용자 컨트롤 및 컨트롤의 생성자를 변경할 때 이러한 컨트롤을 페이지 또는 기타 사용자 컨트롤에 추가 할 때 다음 메시지가 나타날 수 있습니다.
요소 'xxxxx'는 알려진 요소가 아닙니다. 웹 사이트에 컴파일 오류가 있거나 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 컨테이너에 등록 된 서비스가 필요한 생성자에 ActivatorUtilitiessconstructorAttribute를 추가해야합니다. 이 속성이 없으면 매개 변수가없는 생성자는 Microsoft.extensions.dependencyInjection의 기본 동작을 사용하여 응용 프로그램을 실행하는 동안 사용할 수 있습니다.
주저하지 말고 내 코드를 복제하고 몇 가지 변경 사항을 제출하는 것을 주저하지 마십시오. 오픈 소스 프로젝트이므로 모든 사람 이이 라이브러리를 향상시킬 수 있습니다 ... 그런데, 나는 프랑스어입니다 ... 그래서 내 영어가 실제로 유창하지 않다고 말할 것입니다 ... 그래서 내 리소스 문자열이나 문서를 수정하는 것을 망설이지 마십시오 ... Merci!
Dilitrust Company에게 감사의 말을 전하고 ASP .NET WebForms 응용 프로그램에 대한이 라이브러리에 대한 의견을 제시하고 싶습니다.