다음은 Application Startup in ASP.NET Core hDocs.microsoft.com/en-us/aspnet/core/fundamentals/startup) 의 내용을 번역했습니다.

 

.

 

 

Application Startup in ASP.NET Core

 

Startup Class 는 응용 프로그램에 대한 모든 Request 를 처리하는 Request pipeline을 구성한다.

Startup Class

ASP.NET Core Apps 는 Startup class 를 필요로한다.. 관례상 Startup class 는 "Startup" 이라 부른다.

Main program WebHostBuilderExtensions 의  UseStartup<Tstartup> method 내에 startup class 이름을 지정한다.

 

다양한 환경에 대해 별도의 Startup class 를 구성하는 것도 가능하며, runtime 이 그 중 적합한 하나를 선택하게된다.

WebHost Configuration 이나 Option 에서 StartAssembly 를 지정한다면 Hosting 은 지정한 startup assembly 를  Load 하고 Startup 이나 Startup[Environment] Type 을 검색한다.

 

StartupLoader 의 FindStartupType다양한 환경에서의 작업 (Working with multiple environment) 을 참조하자.

UseStartup<TStartup> 를 사용한 접근을 권장한다.

 

Startup class 의 생성자는 dependency injection 을 통해 제공하는 종속성을 수락하는 작업이 가능하다.

IHostingEnvironment 를 사용한 Configuration 소스를 구성하는 작업이 가능하고, ILoggerFactory 를 사용하여  Logging Provider 를 구성하는 작업이 가능하다.

 

Startup class 는 Configure Method 를 반드시 포함해야하며 선택적으로 ConfigureServices Method 를 포함하는 것이 가능하다. 이 두 Method 는 Application 이 시작할 때 호출된다.

이 Class 는 이 Method 의 환경별 (environment-specific) 버전을 포함하는 것 또한 가능하다. 

 

Application startup 동안 예외처리 에 대해 배워보자.

 


Configure Method


Configure Method 는 ASP.NET application 이 HTTP Request 에 대해 Respond 하는 방법을 지정하는데 사용한다. request pipeline 은 dependency injection 에 의해 제공되는 IApplicationBuilder 에 추가되는 middleware  를 추가하므로써 구성된다.

 

default web site template 를 이용한 다음 예제에서 몇가지 확장 Method 들이 BrowserLink, 오류페이지, 정적파일, ASP.NET MVC, Identity 들을 지원하는 pipeline 을 구성하는데 사용된다.

 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
 
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
 
    app.UseStaticFiles();
 
    app.UseIdentity();
 
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

 

각 Use 확장 메소드는 request pipeline 에 middleware component 를 추가한다.

예를 들어, UseMvc 확장 메서드는 routing middleware 를  request pipeline 에 추가하고 MVC 를 기본 handler로 구성한다.


IApplicationBuilder를 사용하는 방법에 대한 자세한 정보는 Middleware 를 살펴보자.

IHostingEnvironment 와 ILoggerFactory와 같은 추가적인 서비스도 method signature 에 지정할 수 있으며, 이 경우 이 서비스들은 사용가능하다면 삽입(injected)될 것이다.

ConfigureServices method

ConfigureServices method 는 선택 사항이다. 하지만 사용될 경우 runtime 에 의해 Configure method 보다 먼저 호출된다. (일부 기능은 request pipeline 에 연결되기 전에 추가된다).

Configuration option 은 이 method 에 설정된다.


실질적인 Setup 이 필요한 기능들은 IServiceCollection 의 Add[Service] Extension method 다.

default web site template 를 이용한 다음 예제는  Entity Framwork, Identity, MVC 를 위한 Service 를 사용하기위해 app 을 구성한다.

 

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
 
    services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
 
    services.AddMvc();
 
    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}

 

service container 에 서비스를 추가하는 작업은 dependency injection 을 통해 application 내에서 Service 를 사용할 수 있다.

 

Startup 에서 사용가능한 Service

 

ASP.NET Core 의 dependency injection 은  application 의 startup 동안 여러 application service 를 제공한다.

Startup class 의 생성자나 Startup class 의 Configure 나 ConfigureService method 중 한 method 의 parameter 로 적당한 Interface 를 추가함으로써 이런 Service 를 요청하는 것이 가능하다.

 

Startup class 의 각 method 를 호출되는 순서대로 살펴보면, 다음 Service 들이 parameter 로 요청될 것이다.

• Constructor 에서 : IHostingEnvironment, ILoggerFactory
• ConfigureServices method에서 : IServiceCollection
• Configure method 에서 : IApplicationBuilder, IHostingEnvironment, ILoggerFactory, IApplicationLifetime

 

추가 자료

 

이제 제 공부차원에서 https://docs.microsoft.com/en-us/aspnet/core 에 있는 내용들을 하나씩 올려보려고 합니다.

 

행복한 고수되십시요.

 

woojja ))*

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 

 

반응형

 

안녕하셔요?

Object  instance 의 Copy 에 대해서 알아보려고 합니다.

Clone 이죠.

 

Object 를 "=" 을 사용해서 대입하면 주소값이 들어가므로 그 값들을 다른 instance로 복사하기 위한 작업입니다.

 

Key Point 는 "ICloneable" interface 를 구현한다는 것인데요.

바로 Source 를 보도록 하겠습니다.

 

 

  1. public class MyBuffer : ICloneable
  2. {
  3.     public int id;
  4.     public List<String> items;
  5.  
  6.     public MyBuffer()
  7.     {
  8.         id = 0;
  9.         items = new List<String>();
  10.     }
  11.  
  12.     public bool IsEmpty
  13.     {
  14.         get { return items.Count == 0; }
  15.     }
  16.  
  17.     public void Clear()
  18.     {
  19.         id = 0;
  20.         items = new List<String>();
  21.     }
  22.  
  23.     public MyBuffer Clone()
  24.     {
  25.         return (MyBuffer)this.MemberwiseClone();
  26.     }
  27.  
  28.     object ICloneable.Clone()
  29.     {
  30.         return Clone();
  31.     }
  32. }

 

 

이 녀석을 호출하는 것도 살펴봐야겠죠?

 

 

  1. public MyBuffer SomeFunction()
  2. {
  3.     MyBuffer myBuffer = new MyBuffer();
  4.     myBuffer.id = 0;
  5.     myBuffer.items.Add("AAA");
  6.     myBuffer.items.Add("BBB");
  7.     myBuffer.items.Add("CCC");
  8.     myBuffer.items.Add("DDD");
  9.     MyBuffer cloneBuffer = myBuffer.Clone();
  10.     myBuffer.Clear();
  11.     return cloneBuffer;
  12. }

 

 

글자 보기가 조금 그렇네요. ㅡㅡ;

Copy As Html 기능을 사용한건데 VisualStudio 테마으 글자 색을 따라가다 보니 저렇게 나오는 듯합니다.

CSS 를 한번 살펴봐야겠군요.

 

 

행복한 고수되셔요. ^^;

woojja ))*

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

반응형

오래된 Error Message 를 소개하고자 합니다. ^^;

 

"Collection was modified; enumeration operation may not execute."

 

위 Message 는 한글 에러로는

"컬렉션이 수정되었습니다. 열거 작업이 실행되지 않을 수도 있습니다." 라는 에러로 나타납니다.

 

foreach (VB.NET 의 경우 For Each) 문은 IEnumerable, IEnumerable<T> 를 구현한 배열이나 컬렉션의 요소들을 반복하여 접근하는 작업을 합니다만

For Each 반복과정에서 배열이나 Collection 의 변경이 생기는 경우 내부에서 사용하는 iterator 가 무효화 되어 사용할 수 없게되어 InvalidOperationException 이 발생하게 됩니다.

 

따라서 For Each 작업을 하기 위해서는 For 문을 사용하거나

반복에 사용할 대상을 미리 List 로 취합한뒤 그 List 를 대상으로 작업을 하시기 바랍니다.

 

 

행복한 고수되십시요.

 

woojja ))*

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 

반응형

'.NET' 카테고리의 다른 글

[.NET] Time Zone Identifiers  (2) 2018.07.24
[.NET] Clone  (0) 2017.05.31
[.NET] .NET Core Roadmap  (0) 2017.02.10
[.NET] Bit 연산에 대한 내용입니다.  (0) 2014.05.07
[.NET] Telerik Code Convertor  (0) 2014.05.07

+ Recent posts