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

52
ASP.NET CORE 實實實實實實實實 Presented by Duran Hsieh http:// dog0416.blogspot.tw/

Upload: duran-hsieh

Post on 12-Apr-2017

518 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

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

Page 2: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

CONTENTS• What's difference between .Net

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

Page 3: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

WHAT'S DIFFERENCE

BETWEEN .NET CORE

AND .NET FRAMEWORK

Page 4: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

4Presented By: Duran Hsieh

.NET CORE & .NET FRAMEWORK

新一代 .NET

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

.NET CORE

更好的效能 適合MICROSERVIC

ES

CONSOLE APPLICATION

CLASS LIBRARY

Page 5: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

5Presented By: Duran Hsieh

.NET CORE & .NET FRAMEWORK

PRROJECT.JSON -> .CSPROJ

穩定版本

WINDOWS FORMS AND

WPF

.NET CORE

Page 6: [Study4TW Visual Studio Everywhere] asp.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 套件

Page 7: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

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

Page 9: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

9Presented By: Duran Hsieh

.NET CORE & .NET FRAMEWORK

• Command line demo• Visual studio code demo

Page 10: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

INTRODUCTION TO

ASP.NET CORE

Page 11: [Study4TW Visual Studio Everywhere] 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)

Page 12: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

12Presented By: Duran Hsieh

INTRODUCTION TO ASP.NET CORE

Page 13: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

13Presented By: Duran Hsieh

INTRODUCTION TO ASP.NET CORE

靜態檔案位置參數設定

打包 JS 與 CSS

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

REQUEST 的位置

Page 14: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

14Presented By: Duran Hsieh

PROGRAM.CS

• 程式起始位置 – program.cs

Page 15: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

15Presented By: Duran Hsieh

STARTUP.CS

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

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

• ConfigureServices• 註冊服務

• Configuration • Request pipe-line 設定

Page 16: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

16Presented By: Duran Hsieh

WWWROOT

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

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

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

Page 17: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

17Presented By: Duran Hsieh

WWWROOT

• program.cs

Page 18: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

18Presented By: Duran Hsieh

WWWROOT

• startup.cs

Page 19: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

19Presented By: Duran Hsieh

APPSETTING.JSON

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

appsetting

appsetting.json

web.config

Page 20: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

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"));• 於建構子注入使用

Page 21: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

21Presented By: Duran Hsieh

APPSETTING.JSON

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

Page 22: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

Request pipeline

Page 23: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

23Presented By: Duran Hsieh

REQUEST PIPELINE

Request

Response

Middleware1 Middleware2 Middleware3

logic logic logic

logiclogiclogic

Page 24: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

24Presented By: Duran Hsieh

REQUEST PIPELINE

.USE

.RUN

Page 25: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

25Presented By: Duran Hsieh

REQUEST PIPELINE

.MAP

Page 26: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

26Presented By: Duran Hsieh

REQUEST PIPELINE

• 專案經驗 • 對於 SPA 作為

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

• 順序很重要

Page 27: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

Middleware

Page 28: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

28Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

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

Page 29: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

29Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

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

Page 30: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

30Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

Page 31: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

31Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

• middleware 應用 • JSON Web Token, JWT

Page 32: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

32Presented By: Duran Hsieh

MIDDLEWAREInset your short subtitle here to explain a little above title

Page 33: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

Dependency Injection

Page 34: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

34Presented By: Duran Hsieh

DEPENDENCY INJECTION

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

Page 35: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

35Presented By: Duran Hsieh

DEPENDENCY INJECTION

• Configuration Objects

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

Page 36: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

36Presented By: Duran Hsieh

DEPENDENCY INJECTION

• Framework-provided services

依情況使用ConfigureServices 方法內註冊

Page 37: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

37Presented By: Duran Hsieh

DEPENDENCY INJECTION

• Application Services (Own Services)

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

Page 38: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

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>();

Page 39: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

Multiple Environments

Page 40: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

40Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

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

• Development• QA• Staging• Production• …

Page 41: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

41Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

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

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

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

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

Page 42: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

42Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

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

Page 43: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

43Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

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

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

Page 44: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

44Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

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

ANS: 只需要加入專案即可

Page 45: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

45Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

• 環境參數設定• Name: ASPNETCORE_ENVIRONMENT

value : Development

• Name: ASPNETCORE_ENVIRONMENTvalue : Staging

• Name: ASPNETCORE_ENVIRONMENTvalue : Production

Page 46: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

46Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

開發期間: Visual Studio 內部設定

Page 47: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

47Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

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

Page 49: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

49Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

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

Page 50: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

50Presented By: Duran Hsieh

ENVIRONMENT VARIABLE

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

Page 51: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

QUESTION & ANSWERS

Page 52: [Study4TW Visual Studio Everywhere] asp.net core 實務開發經驗分享

THANK YOU FOR WATCHING