Skip to content

Commit 7cfea1e

Browse files
committed
Added from stream
1 parent 25a687e commit 7cfea1e

File tree

20 files changed

+469
-11
lines changed

20 files changed

+469
-11
lines changed

sessions/Season-05/0505-ApiDesignAndDevelopment/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ Customize it further with [Swashbuckle tools](https://docs.microsoft.com/aspnet/
2828

2929
You can generate and build client-side code with the dotnet-openapi tool. Details at https://docs.microsoft.com/aspnet/core/web-api/Microsoft.dotnet-openapi
3030

31+
### Adding Swagger to .NET 6 Minimal APIs
32+
33+
https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-4/
34+
35+
3136
## API Versioning
3237

3338
Versioning in Swagger, [Scott Hanselman has a blog post](https://www.hanselman.com/blog/aspnet-core-restful-web-api-versioning-made-easy) to get us started.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0-preview.6.21355.2" />
9-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0-preview.6.21355.2" PrivateAssets="all" />
8+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0" />
9+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0" PrivateAssets="all" />
1010
</ItemGroup>
1111

1212
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
8+
<PackageReference Include="NSwag.ApiDescription.Client" Version="13.0.5" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<OpenApiReference Include="swagger.json" SourceUrl="https://localhost:5001/swagger/v1/swagger.json" CodeGenerator="NSwagCSharp" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
8+
<PackageReference Include="NSwag.ApiDescription.Client" Version="13.0.5" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<OpenApiReference Include="swagger.json" SourceUrl="https://localhost:5001/swagger/v1/swagger.json" CodeGenerator="NSwagCSharp" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace ConsoleApp1
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Console.WriteLine("Hello World!");
10+
}
11+
}
12+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "LiveOnStream",
5+
"version": "v1"
6+
},
7+
"paths": {
8+
"/api/WeatherForecast": {
9+
"get": {
10+
"tags": [
11+
"WeatherForecast"
12+
],
13+
"responses": {
14+
"200": {
15+
"description": "Success",
16+
"content": {
17+
"text/plain": {
18+
"schema": {
19+
"type": "array",
20+
"items": {
21+
"$ref": "#/components/schemas/WeatherForecast"
22+
}
23+
}
24+
},
25+
"application/json": {
26+
"schema": {
27+
"type": "array",
28+
"items": {
29+
"$ref": "#/components/schemas/WeatherForecast"
30+
}
31+
}
32+
},
33+
"text/json": {
34+
"schema": {
35+
"type": "array",
36+
"items": {
37+
"$ref": "#/components/schemas/WeatherForecast"
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
},
47+
"components": {
48+
"schemas": {
49+
"WeatherForecast": {
50+
"type": "object",
51+
"properties": {
52+
"date": {
53+
"type": "string",
54+
"format": "date-time"
55+
},
56+
"temperatureC": {
57+
"type": "integer",
58+
"format": "int32"
59+
},
60+
"temperatureF": {
61+
"type": "integer",
62+
"format": "int32",
63+
"readOnly": true
64+
},
65+
"summary": {
66+
"type": "string",
67+
"nullable": true
68+
}
69+
},
70+
"additionalProperties": false
71+
}
72+
}
73+
}
74+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<OpenApiReference Include="OpenAPIs\swagger.json" CodeGenerator="NSwagCSharp" Namespace="ConsoleApp2" ClassName="Weather">
10+
<SourceUri>https://localhost:5001/swagger/v1/swagger.json</SourceUri>
11+
</OpenApiReference>
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Extensions.ApiDescription.Client" Version="3.0.0">
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
</PackageReference>
19+
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
20+
<PackageReference Include="NSwag.ApiDescription.Client" Version="13.0.5">
21+
<PrivateAssets>all</PrivateAssets>
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
</PackageReference>
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "LiveOnStream",
5+
"version": "v1"
6+
},
7+
"paths": {
8+
"/api/WeatherForecast": {
9+
"get": {
10+
"tags": [
11+
"WeatherForecast"
12+
],
13+
"responses": {
14+
"200": {
15+
"description": "Success",
16+
"content": {
17+
"text/plain": {
18+
"schema": {
19+
"type": "array",
20+
"items": {
21+
"$ref": "#/components/schemas/WeatherForecast"
22+
}
23+
}
24+
},
25+
"application/json": {
26+
"schema": {
27+
"type": "array",
28+
"items": {
29+
"$ref": "#/components/schemas/WeatherForecast"
30+
}
31+
}
32+
},
33+
"text/json": {
34+
"schema": {
35+
"type": "array",
36+
"items": {
37+
"$ref": "#/components/schemas/WeatherForecast"
38+
}
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
},
47+
"components": {
48+
"schemas": {
49+
"WeatherForecast": {
50+
"type": "object",
51+
"properties": {
52+
"date": {
53+
"type": "string",
54+
"format": "date-time"
55+
},
56+
"temperatureC": {
57+
"type": "integer",
58+
"format": "int32"
59+
},
60+
"temperatureF": {
61+
"type": "integer",
62+
"format": "int32",
63+
"readOnly": true
64+
},
65+
"summary": {
66+
"type": "string",
67+
"nullable": true
68+
}
69+
},
70+
"additionalProperties": false
71+
}
72+
}
73+
}
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace ConsoleApp2
5+
{
6+
class Program
7+
{
8+
static async Task Main(string[] args)
9+
{
10+
Console.WriteLine("Hello World!");
11+
12+
var w = new Weather("https://localhost:5001", new System.Net.Http.HttpClient());
13+
var forecasts = await w.WeatherForecastAsync();
14+
15+
foreach (var item in forecasts)
16+
{
17+
Console.WriteLine($"Forecast on {item.Date}: {item.Summary} {item.TemperatureF}");
18+
}
19+
20+
}
21+
}
22+
}
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 LiveOnStream.Controllers
9+
{
10+
[ApiController]
11+
[Route("api/[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+
}

0 commit comments

Comments
 (0)