Page 1
4/12/2019
1
Kevin Griffin
Upgrading Your ASP.NET 4.0+ Skills to ASP.NET Core
2SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Welcome
▪ Microsoft MVP
▪ Host of the 2 Frugal Dudes Podcast
▪ Founder of RevolutionConf
[email protected]
twitter.com/1kevgriff
kevgriffin.com
swiftkick.in
Kevin Griffin
Owner, Swift Kick
Page 2
4/12/2019
2
3SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Let’s Keep in Touch!
▪ I’m always working on new stuff around ASP.NET – and I’d love to keep you up to date.
▪ Free webinars, blog posts, courses, etc
▪ Sign up + free gift!
https://go.swiftkick.in/codestock2019
(all lowercase)
4SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
A Couple Assumptions
▪ You are heavily invested in ASP.NET Classic.
▪ .NET 3.5 or higher
▪ You’ve realized the future is .NET Core
▪ With the upcoming release of .NET Core 3.0, Microsoft is recommending all greenfield development is done on Core
▪ .NET Framework will be supported long term – but not compatible with new features
Page 3
4/12/2019
3
Goodbye IIS, Hello Kestrel
6SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Goodbye IIS, Hello Kestrel!
ASP.NET 4.5
• ASP.NET applications were bootstrapped by an IIS handler.
• IIS was responsible for processing incoming request, and handing them off to ASP.NET
• Stuck on Windows!
ASP.NET Core
• Kestrel is the cross-platform web server built for ASP.NET Core applications.
• Processes requests directly
• Runs on Linux, Windows, Mac – or any platform that supports the .NET Standard.
Page 4
4/12/2019
4
7SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Bootstrapping an ASP.NET Core Application
8SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
CreateDefaultBuilder(…)
▪ The method does a lot of work
▪ Sets working directories
▪ Loads configuration
▪ Environment variables
▪ Appsettings.json
▪ Command line
▪ Configure logging
▪ Configure Kestrel (web server)
▪ Configure IIS integration
Page 5
4/12/2019
5
Demo: dotnet new web
Goodbye IIS, Hello Kestrel
Dependency Injection
Page 6
4/12/2019
6
11SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Dependency Injection
ASP.NET 4.5
• Relied on external libraries to provide Dependency Injection and Inversion of Control (IOC) containers.
• Other dependencies had to be configured to use your DI solutions.
ASP.NET Core
• Baked in at the framework level, no external libraries required!
• Supported out-of-the-box by all ASP.NET Core features. Ex: SignalR, Identity, etc.
• CI works outside of ASP.NET Core application.
12SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Dependency Injection
Page 7
4/12/2019
7
13SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Dependency Injection
14SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Dependency Injection
Page 8
4/12/2019
8
15SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Dependency Injection
16SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Dependency Injection
Page 9
4/12/2019
9
17SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Dependency Injection
▪ AddSingleton()
▪ Creates one instance for the lifetime of the application.
▪ AddTransient()
▪ Create one instance per instantiation.
▪ Example: You might want to use IUserRepository in multiple classes in ONE request. Each class gets its own unique instance of FakeUserRepository.
▪ AddScoped()
▪ Creates one instance per Request.
▪ Example: You might want to use IUserRepository in multiple classes in ONE request. Each class gets the same instance for the request.
Goodbye IIS, Hello Kestrel
Dependency Injection
Routing and Pipelines
Page 10
4/12/2019
10
19SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Routing and Pipelines
20SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Routing and Pipelines
Page 11
4/12/2019
11
21SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
MVC Routing Options
▪ Attribute-based Routing
▪ Default preference
▪ Use the [Route] attribute to decorate Actions with the request path the Action should handle.
▪ Use shortcuts such as [HttpGet], [HttpPost], etc to save on attributes!
22SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
MVC Routing Options
▪ Route prefixes can still be defined on the Controller
Page 12
4/12/2019
12
23SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
MVC Routing Options
▪ If you’re used to “convention-based” routing, that is still possible by manually configuring the UseMvc() method.
▪ PERSONAL OPINION: Please don’t do this.
▪ If you’d like to use WebApi-style controllers and actions (verb-based), that’s doable too!
Goodbye IIS, Hello Kestrel
Dependency Injection
Routing and Pipelines
MVC vs WebAPI
Page 13
4/12/2019
13
25SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
MVC vs WebApi
▪ In ASP.NET Core, there is no separation between Controller and ApiController.
▪ A controller is a controller.
▪ Documentation still defined MVC and WebAPI separately – but they both derive from Controller.
26SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Action Return Types
▪ IActionResult
▪ Abstracted response.
▪ Several built in types:
▪ View()
▪ Ok()
▪ BadRequest()
▪ Forbid();
▪ Unauthorized();
▪ StatusCode();
▪ And more!
Page 14
4/12/2019
14
27SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Action Return Types
▪ Specific Type
▪ Like WebAPI, the controller will automatically convert the response into the appropriate format (JSON) and return it.
28SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Action Return Types
▪ ActionResult<T>
▪ Best of both worlds!
▪ Return a specific type OR an ActionResultmessage
Page 15
4/12/2019
15
Goodbye IIS, Hello Kestrel
Dependency Injection
Routing and Pipelines
MVC vs WebAPI
Other Goodies
30SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Static files and wwwroot
▪ In IIS, wwwroot is the default location for your web application.
▪ In ASP.NET Core, wwwroot is the “public” folder for you application.
Page 16
4/12/2019
16
31SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Integration with SPA CLIs
▪ A growing number of developers are using CLIs for Vue, React, and Angular for front-end and ASP.NET for backend.
▪ This makes development difficult with ASP.NET – since it wants to be everything to everyone.
▪ You can add a proxy into the pipeline to help with debugging locally!
32SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Razor Pages
▪ In ASP.NET 4.0+, working with Razor meant working with “MVC”
▪ Any pages built in Razor needed a corresponding Controller and Action.
▪ Razor Pages are single pages that provide the “good” parts of Razor without the ceremony of “MVC”.
Page 17
4/12/2019
17
33SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Other Topics I Probably Don’t Have Time For
▪ Performance, performance, performance
▪ Custom Middleware
▪ Background/Hosted Services
▪ Health Checks
▪ Logger enhancements
34SLIDE |@1kevgriff - SOFTWARE TRAINING AND CONSULTING – [email protected] © 2019 Swift Kick
Thanks for attending!
▪ Microsoft MVP
▪ Host of the 2 Frugal Dudes Podcast
▪ Founder of RevolutionConf
[email protected]
twitter.com/1kevgriff
kevgriffin.com
swiftkick.in
Kevin Griffin
Owner, Swift Kick