Create a .NET Core app §Tomas Hruby §2016 - Time consuming I/O op. or Web Request - Synchronous calling blocks possible execution of independent code Before § • • • • §.NET core § https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer .Blogs.Components.WeblogFiles/00/00/01/12/34/3173.Pic1.png .NET was intended as a single framework After some time, it was branched for other platforms (Phone, Silverlight, Desktop) Programming across verticals was hard – multiple projects for every platform, #if pragmas PCL – portable class libraries Compatibility issues (breaking changes) ->fork of .NET framework Now § • • § § § § § § § § §https://github.com/dotnet/core §https://github.com/dotnet/coreclr §.NET core § https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer .Blogs.Components.WeblogFiles/00/00/01/12/34/2783.Pic3.png https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer .Blogs.Components.WeblogFiles/00/00/01/12/34/0841.Pic4.png New, smaller subset of BCL All distributed using NuGet Runs on all devices On GitHub .NET Standard § • • • • §.NET core § •NETStandard – runtime API specification, NETCore – implementation of the standard .NET Standard § • • • • §.NET core § netstandard-apis •NETStandard – runtime API specification, NETCore – implementation of the standard It’s Demo time… §https://docs.asp.net/en/latest/fundamentals/index.html § * Show how to create .NET Core Web App with WebAPI •Show .NET Core project structure on filesystem •Global.json, Project.json •Return to VS and explain project.json •Dependencies, frameworks, imports, framework dependencies, target: project •TFM – Frameworks – available API for the developer https://docs.microsoft.com/en-us/dotnet/articles/standard/library •Show Deprecated TFMs - https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md •Metapackages •The NETStandard.Library metapackage targets the netstandard framework. The most common way to target netstandard is by referencing this metapackage. It describes and provides access to the ~40 .NET libraries and associated APIs that define the .NET Standard Library. •The Microsoft.NETCore.App metapackage targets the netcoreapp framework. It provides access to ~60 libraries, ~40 provided by the NETStandard.Library package and ~20 more in addition. •Show https://docs.microsoft.com/en-us/dotnet/articles/core/tools/project-json Adding middlewares § ASP.NET middlewares § • • • • §.NET core § ../_images/request-delegate-pipeline.png •Linear HTTP request-response pipeline It’s Demo time… § •Program.cs is the main entry point •Startup.cs is the bootstrap for the whole application •Show writing simple middleware •Show enabling MVC Developing a full-blown web app § Dependency Injection •Is available everywhere — Startup, Middleware, Services •Enables you to modularize your application •https://docs.asp.net/en/latest/fundamentals/dependency-injection.html • § • • • §a full-blown web App § Image result for dependency injection •Add new service that returns static data App Configuration § •Configuration is build on the project startup (Startup.cs) •Combine configuration from multiple sources •https://docs.asp.net/en/latest/fundamentals/configuration.html • § • • • §a full-blown web App § •Add new configuration section Logging § •Access logger everywhere while maintaining IOC •Switch loggers application-wise (Startup.cs) •Specify level of details for every namespace •https://docs.asp.net/en/latest/fundamentals/logging.html • § • • • §a full-blown web App § •Add Nlog logging •Add nlog.config •Add middleware Routing •Map URI to Controller •URI – controller, action, query §a full-blown web App •Attribute routing Unit tests •xUnit •https://xunit.github.io/docs/getting-started-dotnet-core.html •NSubstitute •http://nsubstitute.github.io/help/getting-started/ • • • • • • §a full-blown web App •How to install xUnit, NSubstitute •How to configure runner •How to use Test Explorer •Fact / Theory •Mocking method / property Asynchronous programming §Tomas Hruby §2015 - Time consuming I/O op. or Web Request - Synchronous calling blocks possible execution of independent code § § Async-Await •Put async-await only across the whole application •Don’t mix manipulation with threads with async-await •Blocking context threads causes deadlocks •To use async code from normal method use Task.Run •Method can continue in a different thread after awaiting •Don’t depend on the thread context •HTTP context copies context information to new thread from the thread pool • • • • • §https://msdn.microsoft.com/en-us/library/hh191443.aspx §http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html §MVC basics § Show deadlock when blocking the context thread. REST API design § Describe REST application role. Basics revisited •Stateless HTTP communication (Request-Response) •URL identifies an object •~/api/users/ngk67 •~/api/users/ngk67/roles •Use plural for endpoints (/api/users, /api/projects) •Method identifies action (GET, POST, PUT, PATCH, DELETE) •GET ~/api/users (get all users) •DELETE ~/api/users/ngld456 (delete user with given ID) •Request body represents the object identified by URI •POST ~/api/users (object in request body is inserted as user) •Server answers with HTTP Status Code and response body containing requested object(s) • §MVC basics § HTTP Status Codes §401 – Unauthorized §500 – Internal Server Error §429 – Too many requests (response should contain time of renewal) • §http://www.restapitutorial.com/httpstatuscodes.html §MVC basics § Method URL pattern Statuses (Notes) GET /api/users 200 GET /api/users/{id} 200, 404 POST /api/users 201, 400 201 – Created PUT /api/users/{id} 200, 404, 400 400 – Bad request DELETE /api/users/{id} 204, 404 204 – No content Browse default UI showing controllers in action Create an route without controller (for HomeController) Filtering & Paging •Filtering using query parameters •GET ~/api/students?semester=spring •Filtering using query objects •GET ~/api/students?filter={“subjects”=[“PE”,“maths”],”age”=18}} •REST calls remain the same even if new filter is added (forward compatible) •Easy to write by hand in browser URL bar •Paging request •GET ~/api/students?pagesize=10&page=3 •Paging responses •Pagination info in header (X-Total-Count, X-Page-Size, X-Current-Page) •Pagination envelope •{ “total”: 40, “pageSize”: 10, “page”: 1, “data”: […] } •Cursor based pagination (link to next page) • • • • §MVC basics § Versioning •Using URL prefixes •~/api/v1/users •Using HTTP headers •X-API-Version: 7.1.3 • • • §MVC basics § Special actions •Don’t use special query parameters (as flags). Use special endpoints •~/api/students/:student_id/disable •~/api/users/resetpassword • • • • §MVC basics §