Skip to content

Commit 4c18753

Browse files
authored
Session 0205 (#68)
* Annotations demo * Versioning samples * WIP - trying to get OpenAPI + Versioning working * Fixed versioning * Completed demos * Added gRPC client * Updated docs and added streaming sample * Started count demo * Added reference for gRPC versioning * Updated from stream
1 parent 3a6d98b commit 4c18753

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1755
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Session 0205 - APIs Part 2: OpenAPI and gRPC
2+
3+
## Agenda
4+
5+
* Review API template
6+
* Discuss annotations to deliver more value using OpenAPI
7+
* API Clients
8+
* API versioning with OpenAPI
9+
* Introduce gRPC and gRPC template
10+
* gRPC client
11+
12+
## OpenAPI
13+
14+
The OpenAPI specification is a standard interface definition for RESTful services.According to [RapidAPI.com](https://rapidapi.com/blog/api-glossary/openapi/) the OpenAPI Specification is an open-source format and initiative for designing and creating machine-readable interface files that are utilized in producing, describing, consuming, and visualizing RESTful APIs and web services. The OpenAPI definition file defines an API with the following information
15+
16+
* Present endpoints and each endpoint’s operations
17+
* The input and output operation parameters
18+
* Authentication techniques
19+
* Contact information, terms of use, license, and more
20+
21+
OpenAPI and Swagger are sometimes used interchangeably. Swagger was the name of the original specification and a company that implements tools for working with the public specification. Swagger is now owned by SmartBear.
22+
23+
For ASP.NET Core projects, the OpenAPI definition file is generated for you and made available at `/swagger/v1/swagger.json`
24+
25+
### Customize OpenAPI
26+
27+
Documentation is available with details to update [the Swagger UI provided with ASP.NET Core](https://docs.microsoft.com/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-5.0)
28+
29+
### .NET OpenAPI tool for client
30+
31+
There is an [OpenAPI command-line tool available](https://docs.microsoft.com/aspnet/core/web-api/Microsoft.dotnet-openapi?view=aspnetcore-5.0) to help you generate client-code that you can install with:
32+
33+
```
34+
dotnet tool install --global Microsoft.dotnet-openapi --version 5.0.1
35+
```
36+
37+
Add a service reference from the web to your client project with:
38+
39+
```
40+
dotnet openapi add url https://localhost:5001/swagger/v1/swagger.json -p client.csproj
41+
```
42+
43+
There are additional command-line options to add from local files, refresh your references, and remove the references.
44+
45+
## HTTP-Repl
46+
47+
You can use the [http-repl tool](https://docs.microsoft.com/aspnet/core/web-api/http-repl/?view=aspnetcore-5.0) to interact with your APIs on the command-line and using scripts.
48+
49+
Install the http-repl using:
50+
51+
```dotnetcli
52+
dotnet tool install -g Microsoft.dotnet-httprepl
53+
```
54+
55+
You can then navigate around your API as though you were traversing folders and interacting with files on disk
56+
57+
## gRPC Server
58+
59+
- Add gRPC tools references
60+
61+
`<PackageReference Include="Grpc.AspNetCore" Version="2.32.0" />`
62+
63+
- Define a protobuf file and reference
64+
65+
`<Protobuf Include="Protos\greet.proto" GrpcServices="Server" />`
66+
67+
- ASP.NET Core generates services and message classes
68+
- Implement the service similar to a Controller
69+
70+
## gRPC Client
71+
72+
- Add gRPC package references: `Grpc.Net.ClientFactory`, `Grpc.Tools`
73+
- Import a protobuf file
74+
75+
`<Protobuf Include="..\4_gRPC\Protos\greet.proto" GrpcServices="Client">`
76+
77+
- Connect to the client
78+
79+
```
80+
using var channel = GrpcChannel.ForAddress("https://localhost:5001");
81+
var client = new Greeter.GreeterClient(channel);
82+
```
83+
84+
### Versioning gRPC
85+
86+
tldr; Add a version number to the package definition `greeter.v1`
87+
88+
Microsoft docs: https://docs.microsoft.com/aspnet/core/grpc/versioning?view=aspnetcore-5.0
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
5+
<clear />
6+
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
7+
</packageSources>
8+
</configuration>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>_1_Template</RootNamespace>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.1" NoWarn="NU1605" />
10+
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.1" NoWarn="NU1605" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace _1_Template.Controllers
9+
{
10+
[ApiController]
11+
[Route("[controller]")]
12+
public class WeatherForecastController : ControllerBase
13+
{
14+
private static readonly string[] Summaries = new[]
15+
{
16+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
17+
};
18+
19+
private readonly ILogger<WeatherForecastController> _logger;
20+
21+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
22+
{
23+
_logger = logger;
24+
}
25+
26+
[HttpGet]
27+
public IEnumerable<WeatherForecast> Get()
28+
{
29+
var rng = new Random();
30+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
31+
{
32+
Date = DateTime.Now.AddDays(index),
33+
TemperatureC = rng.Next(-20, 55),
34+
Summary = Summaries[rng.Next(Summaries.Length)]
35+
})
36+
.ToArray();
37+
}
38+
}
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace _1_Template
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:57845",
8+
"sslPort": 44333
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": true,
15+
"launchUrl": "swagger",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"_1_Template": {
21+
"commandName": "Project",
22+
"dotnetRunMessages": "true",
23+
"launchBrowser": true,
24+
"launchUrl": "swagger",
25+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.HttpsPolicy;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Hosting;
12+
using Microsoft.Extensions.Logging;
13+
using Microsoft.OpenApi.Models;
14+
15+
namespace _1_Template
16+
{
17+
public class Startup
18+
{
19+
public Startup(IConfiguration configuration)
20+
{
21+
Configuration = configuration;
22+
}
23+
24+
public IConfiguration Configuration { get; }
25+
26+
// This method gets called by the runtime. Use this method to add services to the container.
27+
public void ConfigureServices(IServiceCollection services)
28+
{
29+
30+
services.AddControllers();
31+
services.AddSwaggerGen(c =>
32+
{
33+
c.SwaggerDoc("v1", new OpenApiInfo { Title = "_1_Template", Version = "v1" });
34+
});
35+
}
36+
37+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
38+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
39+
{
40+
if (env.IsDevelopment())
41+
{
42+
app.UseDeveloperExceptionPage();
43+
app.UseSwagger();
44+
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "_1_Template v1"));
45+
}
46+
47+
app.UseHttpsRedirection();
48+
49+
app.UseRouting();
50+
51+
app.UseAuthorization();
52+
53+
app.UseEndpoints(endpoints =>
54+
{
55+
endpoints.MapControllers();
56+
});
57+
}
58+
}
59+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace _1_Template
4+
{
5+
public class WeatherForecast
6+
{
7+
public DateTime Date { get; set; }
8+
9+
public int TemperatureC { get; set; }
10+
11+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
12+
13+
public string Summary { get; set; }
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}

0 commit comments

Comments
 (0)