[study4tw visual studio everywhere] asp.net core 實務開發經驗分享

Post on 12-Apr-2017

518 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ASP.NET CORE 實務開發經驗分享Presented byDuran Hsiehhttp://dog0416.blogspot.tw/

CONTENTS• What's difference between .Net

core and .Net Framework• Introduction to ASP.Net Core• Request pipeline• Middleware• Dependency Injection• Multiple Environments• QA

WHAT'S DIFFERENCE

BETWEEN .NET CORE

AND .NET FRAMEWORK

4Presented By: Duran Hsieh

.NET CORE & .NET FRAMEWORK

新一代 .NET

FRAMEWORK跨平台 套件化管理 .NET CLI 指令列

.NET CORE

更好的效能 適合MICROSERVIC

ES

CONSOLE APPLICATION

CLASS LIBRARY

5Presented By: Duran Hsieh

.NET CORE & .NET FRAMEWORK

PRROJECT.JSON -> .CSPROJ

穩定版本

WINDOWS FORMS AND

WPF

.NET CORE

6Presented By: Duran Hsieh

.NET CORE & .NET FRAMEWORK

• .NET CLI指令 說明dotnet new 產生新的 .Net 專案內容dotnet restore 還原專案所使用的 Nuget 套件dotnet build 建構專案dotnet publish 產生可發行專案dotnet run 編譯與執行 .Net 專案dotnet repl 啟動互動式模式dotnet pack 產出封裝成 Nuget 套件

7Presented By: Duran Hsieh

.NET CORE & .NET FRAMEWORK

• Install for Windows• Visual Studio• Visual Studio 2015 Update 3• .NET Core 1.0.1 - VS 2015 Tooling Preview 2

• CI and Visual Studio Code• .NET Core SDK 1.1 for Windows

9Presented By: Duran Hsieh

.NET CORE & .NET FRAMEWORK

• Command line demo• Visual studio code demo

INTRODUCTION TO

ASP.NET CORE

11Presented By: Duran Hsieh

INTRODUCTION TO ASP.NET CORE

ASP.NET CORE•新的 ASP.NET (WEB 端的開發類別庫 )•具跨平台特性•不使用 SYSTEM.WEB 套件•截然不同的 HTTP REQUEST 處理流程•OWIN 概念•內建 DEPENDENCY INJECTION• .NET CLI 指令列工具 ( 過去使用 DNX)

12Presented By: Duran Hsieh

INTRODUCTION TO ASP.NET CORE

13Presented By: Duran Hsieh

INTRODUCTION TO ASP.NET CORE

靜態檔案位置參數設定

打包 JS 與 CSS

程式起始位置註冊服務與設定

REQUEST 的位置

14Presented By: Duran Hsieh

PROGRAM.CS

• 程式起始位置 – program.cs

15Presented By: Duran Hsieh

STARTUP.CS

• 啟動檔 – startup.cs• 建構子

• 路徑設定• 環境變數設定• appsetting.json 匯入設定

• ConfigureServices• 註冊服務

• Configuration • Request pipe-line 設定

16Presented By: Duran Hsieh

WWWROOT

• 靜態資料夾 - wwwroot• 前端與 ASP.NET 專案整合

• 前端人員的開發習慣• 前端專案與 MVC 專案整合• 需要調整持續整合流程

• 注意事項• 處理 Http Request 的順序相當重要• 存取的問題

17Presented By: Duran Hsieh

WWWROOT

• program.cs

18Presented By: Duran Hsieh

WWWROOT

• startup.cs

19Presented By: Duran Hsieh

APPSETTING.JSON

• appsetting.json• 相當於 webconfig.xml (app.config.xml) 的

appsetting

appsetting.json

web.config

20Presented By: Duran Hsieh

APPSETTING.JSON

• 使用方法 (startup.cs)• 方法 1:

• Configuration.GetSection(“Logging”); • getValue<T>() • key()• value()• path()

• 方法 2:• 建立 DTO 物件• 註冊 services.Configure<T> (Configuration.GetSection("logging"));• 於建構子注入使用

21Presented By: Duran Hsieh

APPSETTING.JSON

• bundleconfig.json• 相當於過去的 bundleconfig.cs• 打包、壓縮 javascript 與 css 檔案

Request pipeline

23Presented By: Duran Hsieh

REQUEST PIPELINE

Request

Response

Middleware1 Middleware2 Middleware3

logic logic logic

logiclogiclogic

24Presented By: Duran Hsieh

REQUEST PIPELINE

.USE

.RUN

25Presented By: Duran Hsieh

REQUEST PIPELINE

.MAP

26Presented By: Duran Hsieh

REQUEST PIPELINE

• 專案經驗 • 對於 SPA 作為

• 發生 404 錯誤導回前端• 同站台,開放 image

• 順序很重要

Middleware

28Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

• 建立 middleware • 透過 C# extension method • UseRequestLoggerMiddleware()• ApplicationBuilder 讀取 middleware

29Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

• 建立 middleware • 必須在 configure 方法內呼叫• 建構子必須包含 RequestDelegate• 必須包含一個 invoke 方法

30Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

31Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

• middleware 應用 • JSON Web Token, JWT

32Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

Dependency Injection

34Presented By: Duran Hsieh

DEPENDENCY INJECTION

• Configuration Objects• Framework-provided services• Application Services (Own Services)

35Presented By: Duran Hsieh

DEPENDENCY INJECTION

• Configuration Objects

建構子內注入建立 DTO ConfigureServices 方法內註冊

36Presented By: Duran Hsieh

DEPENDENCY INJECTION

• Framework-provided services

依情況使用ConfigureServices 方法內註冊

37Presented By: Duran Hsieh

DEPENDENCY INJECTION

• Application Services (Own Services)

建構子內注入ConfigureServices 方法內註冊

38Presented By: Duran Hsieh

DEPENDENCY INJECTION

• Service lifetimes• Transient

• 每次請求的時候皆會重新建立服務• Ex: services.AddTransient<IOperationTransient,

Operation>();

• Scoped• 每一次 Request 的時候建立服務, Request 完成後結束• Ex: services.AddScoped<IOperationScoped, Operation>();

• Singleton• 第一次請求的時候建立,永久存在• Ex: services.AddSingleton<IOperationSingleton,

Operation>();

Multiple Environments

40Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

• 多種開發環境設定 • 因應各種環境有不同的設定

• Development• QA• Staging• Production• …

41Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

• 多種開發環境設定 • .Net Framework

• web.config 設定參數• 發佈專案時設定取用檔案

• .Net Core• Appsetting.json 設定參數• 環境變數:

• 開發期間: visual studio 內部設定• 作業系統內設定• IIS 內參數設定

42Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

操作步驟 : 加入各環境 appsetting.json

43Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

.Net Core rc4 之前的版本 與 Visual Studio 2015

需加入否則發佈會遺失檔案

44Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

.Net Core rc4 的版本 與 Visual Studio 2017 RC

ANS: 只需要加入專案即可

45Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

• 環境參數設定• Name: ASPNETCORE_ENVIRONMENT

value : Development

• Name: ASPNETCORE_ENVIRONMENTvalue : Staging

• Name: ASPNETCORE_ENVIRONMENTvalue : Production

46Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

開發期間: Visual Studio 內部設定

47Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

發佈期間: Windows 內環境變數設定

49Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

發佈期間: IIS 內部設定 (2)

50Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

• ( 經驗 ) 若沒有設定環境變數, appsetting.{xxx}.json 會自動選擇最後一個使用。• ( 經驗 ) dotnet publish 參數無法指定環境變數。

QUESTION & ANSWERS

THANK YOU FOR WATCHING

top related