- How to create a RESTful Web API project with OWIN from scratch
- Understand the importance of NuGet (will become even more important with .NET Core)
- Learn very basics about ASP.NET Web API development (this will not be a deep dive as we focus on DevOps, not just Dev)
-
Start Visual Studio
-
Create a new web project. Disable Application Insights for now. We will add Application Insights later.
-
Discussion points:
- Evolution of .NET (3.x, 4.x, Core)
- Motivation of Microsoft to not just continue the way of the past with .NET (importance of cross-platform, modern architecture, open-source, etc.)
- Describe that we now focus on the production version of .NET (4.6), ASP.NET Core Preview follows later.
-
Install necessary NuGet packages by running the following commands in Visual Studio's Package Manager Console (you can use Manage NuGet Packages for Solution instead if you prefer GUI over PowerShell):
Install-Package Microsoft.AspNet.WebApi.Owin
Install-Package Microsoft.Owin.Host.SystemWeb
Install-Package Microsoft.Owin.Cors
Install-Package Microsoft.ApplicationInsights.Web
Install-Package WindowsAzure.Storage
-
Discussion points:
- NuGet as a delivery mechanism for .NET components
- Growing importance of NuGet for Microsoft-oriented development teams
- nuget.org vs. private feeds in the cloud (e.g. myget) vs. private feeds on-premise
-
Replace the
Configuration
method with the following sample code.public void Configuration(IAppBuilder app) { app.Run(context => context.Response.WriteAsync("Hello World!")); }
-
Discussion points:
- Describe OWIN architecture
- Describe OWIN's pipeline concept
- Difference to "old" ASP.NET web apps
- Note that a practical use of OWIN (self-host web server in automated test) will follow later
-
Run the program and see if the browser shows
Hello World
for all paths.
-
Add references to the following framework assemblies:
System.ComponentModel.Composition
System.ComponentModel.Composition.Registration
System.Reflection.Context
-
Copy
.cs
files from Excercise-1-Service-Implementation into your project. Note that you can overwriteStartup.cs
. -
Discussion points:
- Code walkthrough (depth depends on existing knowledge and interests of the audience)
- Describe the use of MEF for dependency injection (
INameGenerator
,BooksDemoDataOptions
) - Point out that this will work entirely different in ASP.NET Core 1.0
-
Add the following
appSettings
to yourweb.config
file. These settings control how many random books are generated by the Web API.<?xml version="1.0" encoding="utf-8"?> ... <configuration> <appSettings> <add key="MinimumNumberOfBooks" value="1"/> <add key="MaximumNumberOfBooks" value="5"/> </appSettings> ... </configuration>
-
Discussion points:
- Describe the problem of stage-related settings (e.g. different configuration options for dev/test/prod)
- Point out the importance of correct handling of security-critical settings (e.g. checked-in connection strings as an anti-pattern)
-
Build your solution. There should be no errors.
-
Run your program and try the URL
http://localhost:2690/api/books
(this assumes that your app runs on port 2690; you have to replace this port with the port that your Visual Studio has chosen). -
Refresh the URL mentioned above multiple times. Note how random books with random names are generated.
-
Discussion points:
- (Option if you have some Devs in the audience) Debug the application to give attendees a deeper insight into the structure of the application
-
Postman is a very handy tool for testing Web APIs. You can get it here: Postman on Chrome Web Store. Install Postman and try the Web API.
If you have time left, you could additionally cover topics like:
- Building a console app that hosts our Web API without IIS
- Add additional middleware components (e.g. CORS, authentication) to demonstrate modular architecture with NuGet
- (Not recommended but possible if you have a very web-development-oriented audience) Add the Angular 2 client (source, Gulpfile,
.json
config files) to the project