OWIN Startup.cs 클래스를 사용하고 있고 모든 구성을 여기로 옮기려면 Global.asax.cs 파일이 필요합니까?
예를 들어 개별 계정 템플릿이있는 MVC로 만든 새로운 ASP.NET MVC 5 응용 프로그램에서 Global.asax.cs
클래스를 삭제하고 Startup.cs
Configuration()
다음과 같이 구성 코드를 메소드로 이동 하면 단점은 무엇입니까?
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ConfigureAuth(app);
}
}
나에게 단점은 ASP.NET 4 응용 프로그램을 ASP.NET 5로 업그레이드하고 Startup.cs 클래스에서 구성 해야하는 부분을 사용할 때 관련성있는 두 개의 다른 클래스에서 종속성 주입 및 기타 구성을 수행하지 않는다는 것입니다 시작 및 구성.
Startup.Configuration은 Application_Start보다 약간 늦게 호출되지만 대부분의 경우 그 차이가별로 중요하지 않다고 생각합니다.
Global.asax에서 다른 코드를 유지 한 주요 이유는 다음과 같습니다.
- 이전 버전의 MVC와 일치합니다. (현재 모든 사람들이이 코드를 찾을 것으로 기대합니다.)
- 다른 이벤트 핸들러를 추가하는 기능 Global.asax에서는 Session_Start 및 Application_Error와 같은 다른 방법을 처리 할 수 있습니다.
- 다양한 인증 시나리오의 정확성 bin 디렉토리에 Microsoft.Owin.Host.SystemWeb.dll이있는 경우에만 Startup.Configuration 메소드가 호출됩니다. 이 DLL을 제거하면 Startup.Configuration 호출을 자동으로 중지하므로 이해하기 어려울 수 있습니다.
일부 시나리오에는이 DLL이 포함되어 있지 않기 때문에 세 번째 이유는 기본적 으로이 방법을 사용하지 않은 가장 중요한 이유라고 생각합니다. 비 관련 코드가있는 위치를 무효화하지 않고 인증 방법을 변경할 수있는 것이 좋습니다 경로 등록)이 배치됩니다.
그러나 이러한 이유 중 어느 것도 시나리오에 적용되지 않으면이 방법을 사용하는 것이 좋습니다.
전체 단계를 찾는 사람들을 위해 : OWIN 기반의 IIS 호스팅 웹 API를 만들려면 다음 단계를 수행하십시오.
File -> New -> Project
- 대화에서
Installed -> templates -> Other Project types -> Visual Studio Solutions -> Blank Solution targeting .NET 4.6
솔루션에서 마우스 오른쪽 버튼을 클릭하고 추가
Project -> Web -> ASP.NET Web Application
(타겟팅 .NET 4.6)3.1 이제 ASP.NET 4.5 템플릿에서 템플릿으로 비어 있음을 선택하십시오.
3.2 이렇게하면 두 개의 nuget 패키지가있는 빈 솔루션이 생성됩니다.
Microsoft.CodeDom.Providers.DotNetCompilerPlatform v 1.0.0 Microsoft.Net.Compilers v 1.0.0
다음 패키지를 설치하십시오.
Install-Package Microsoft.AspNet.WebApi.WebHost -Version 5.2.3 Install-Package Microsoft.AspNet.WebApi -Version 5.2.3 Install-Package WebApiContrib.Formatting.Razor 2.3.0.0
OWIN의 경우 :
Install-Package Microsoft.Owin.Host.SystemWeb
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
그런 다음 Configuration 메소드를 사용하여 Startup.cs를 추가하십시오.
[assembly:OwinStartup(typeof(namespace.Startup))]
public class Startup
{
/// <summary> Configurations the specified application. </summary>
/// <param name="app">The application.</param>
public static void Configuration(IAppBuilder app)
{
var httpConfiguration = CreateHttpConfiguration();
app
.UseWebApi(httpConfiguration);
}
/// <summary> Creates the HTTP configuration. </summary>
/// <returns> An <see cref="HttpConfiguration"/> to bootstrap the hosted API </returns>
public static HttpConfiguration CreateHttpConfiguration()
{
var httpConfiguration = new HttpConfiguration();
httpConfiguration.MapHttpAttributeRoutes();
return httpConfiguration;
}
}
Now add a class that inherits from ApiController
, annotate it with RoutePrefix
attribute and the action method with Route + HttpGet/PutPost
(representing the Http verb you're after) and you should be good to go
This is my understanding of how starting/hosting a web application evolved as it's all pretty confusing to follow. A small summary:
1. Classic ASP.NET: Write only the application code to run in the last step of the mandatory IIS pipeline
2. ASP.NET with OWIN: Configure a .NET webserver and write your application code. No longer directly coupled to IIS, so you're no longer forced to use it.
3. ASP.NET Core: Configure both the host and the webserver to use and write your application code. No longer mandatatory to use a .NET webserver if you target .NET Core instead of the full .NET Framework.
Now I'll go a bit more into detail of how it works and which classes are used to start the application:
Classic ASP.NET
Classic ASP.NET applications have the Global.asax
file as entry point. These applications can only be run in IIS and your code gets executed at the end of the IIS pipeline (so IIS is responsible for CORS, authentication... before your code even runs). Since IIS 7 you can run your application in integrated mode which integrates the ASP.NET runtime into IIS. This enables your code to configure functionality which wasn't possible before (or only in IIS itself) such as url rewriting in the Application_Start
event of your Global.asax
file or use the new <system.webserver>
section in your web.config
file.
ASP.NET with OWIN
First of all OWIN is not a library but a specification of how .NET web servers (for example IIS) interact with web applications. Microsoft themselves have an implementation of OWIN called project Katana (distributed through several different NuGet packages). This implementation provides the IAppBuilder
interface you encounter in a Startup
class and some OWIN middleware components (OMC's) provided by Microsoft. Using IAppBuilder
you basically compose middleware in a plug-and-play way to create the pipeline for the webserver (in addition to only the ASP.NET pipeline in IIS7+ as in the point above) instead of being tied to the IIS pipeline (but now you use a middleware component for CORS, a middleware component for authentication...). Because of this, your application is not specifically coupled to IIS anymore and you can run it on any .NET Webserver, for example:
- The OwinHost package can be used to self-host your application with a Katana webserver.
- The Microsoft.Owin.Host.SystemWeb package is used to host your OWIN application in IIS7+ in integrated mode, by subscribing your middleware to the correct lifetime events internally.
The thing that makes everything so confusing is that Global.asax
is still supported together with the OWIN Startup
class, while they can both do similar things. For example you could implement CORS in Global.asax
and authentication using OWIN middleware which becomes really confusing.
My rule of thumb is to remove the Global.asax
file alltogether in favor of using Startup
whenever I need to add OWIN.
ASP.NET Core
ASP.NET Core is the next evolution and now you can target either .NET Core or the full .NET Framework. When you target .NET Core you can run your application on any host which supports the .NET Standard. This means you are no longer restricted to a .NET webserver (as in the previous point), but can host your application in Docker containers, a linux webserver, IIS...
The entry point for an ASP.NET Core web application is the Program.cs
file. There you configure your host and again specify your Startup
class where you configure your pipeline. Using OWIN (by using the IAppBuilder.UseOwin
extension method) is optional, but fully supported.
'IT' 카테고리의 다른 글
한 달에이 아기가 필요 해요 (0) | 2020.05.16 |
---|---|
GitHub의 요점에 대해 풀 요청을 할 수 있습니까? (0) | 2020.05.16 |
cast_sender.js 오류 : Chrome에서 리소스를로드하지 못했습니다 : net :: ERR_FAILED (0) | 2020.05.16 |
폴리곤 팽창 / 팽창 (오프셋, 버퍼링) 알고리즘 (0) | 2020.05.16 |
Android, ListView IllegalStateException :“어댑터의 내용이 변경되었지만 ListView가 알림을받지 못했습니다” (0) | 2020.05.16 |