Skip to content

Commit 271e65c

Browse files
committed
Added Minimal session notes and samples
1 parent 6bab2db commit 271e65c

File tree

16 files changed

+380
-0
lines changed

16 files changed

+380
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (web)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/net6.0/ContactApi.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
"stopAtEntry": false,
17+
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
18+
"serverReadyAction": {
19+
"action": "openExternally",
20+
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
21+
},
22+
"env": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
},
25+
"sourceFileMap": {
26+
"/Views": "${workspaceFolder}/Views"
27+
}
28+
},
29+
{
30+
"name": ".NET Core Attach",
31+
"type": "coreclr",
32+
"request": "attach"
33+
}
34+
]
35+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/ContactApi.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/ContactApi.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"${workspaceFolder}/ContactApi.csproj",
36+
"/property:GenerateFullPaths=true",
37+
"/consoleloggerparameters:NoSummary"
38+
],
39+
"problemMatcher": "$msCompile"
40+
}
41+
]
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>bfa61f02-fc76-42fd-9601-f93a29aaef2f</UserSecretsId>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
13+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
14+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
2+
3+
FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine AS base
4+
WORKDIR /app
5+
EXPOSE 80
6+
EXPOSE 443
7+
8+
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
9+
WORKDIR /src
10+
COPY ["ContactApi/ContactApi.csproj", "ContactApi/"]
11+
RUN dotnet restore "ContactApi/ContactApi.csproj"
12+
COPY . .
13+
WORKDIR "/src/ContactApi"
14+
RUN dotnet build "ContactApi.csproj" -c Release -o /app/build
15+
16+
FROM build AS publish
17+
RUN dotnet publish "ContactApi.csproj" -c Release -o /app/publish
18+
19+
FROM base AS final
20+
WORKDIR /app
21+
COPY --from=publish /app/publish .
22+
ENTRYPOINT ["dotnet", "ContactApi.dll"]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.OpenApi.Models;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
7+
builder.Services.AddDbContext<MyDbContext>(options => {
8+
options.UseSqlite("Data Source=contacts.db");
9+
});
10+
11+
builder.Services.AddEndpointsApiExplorer();
12+
builder.Services.AddSwaggerGen(c =>
13+
{
14+
c.SwaggerDoc("v1", new OpenApiInfo {
15+
Title = "Contacts API",
16+
Description = "Storing and sharing contacts for my DEVintersection friends",
17+
Version = "v1" });
18+
});
19+
20+
var app = builder.Build();
21+
22+
var ctx = app.Services.CreateScope().ServiceProvider.GetService<MyDbContext>();
23+
ctx.Database.EnsureCreated();
24+
25+
app.UseSwagger();
26+
app.UseSwaggerUI();
27+
28+
app.MapGet("/", () => "Hello World!");
29+
app.MapGet("/contacts", (MyDbContext ctx) => ctx.Contacts);
30+
app.MapGet("/contacts/{id:int}", (MyDbContext ctx, int id) => ctx.Contacts.Find(id));
31+
app.MapPost("/contacts", async (MyDbContext ctx, Contact newContact) => {
32+
await ctx.Contacts.AddAsync(newContact);
33+
await ctx.SaveChangesAsync();
34+
return Results.Created($"/contacts/{newContact.Id}", newContact);
35+
}).Produces<Contact>(StatusCodes.Status201Created);
36+
app.MapPut("/contacts/{id:int}", async (MyDbContext ctx, Contact updateContact, int id) =>
37+
{
38+
var contact = await ctx.Contacts.FindAsync(id);
39+
if (contact is null) return Results.NotFound();
40+
ctx.Contacts.Attach(updateContact);
41+
await ctx.SaveChangesAsync();
42+
return Results.NoContent();
43+
}).Produces(StatusCodes.Status404NotFound)
44+
.Produces(StatusCodes.Status204NoContent);
45+
app.MapDelete("/contacts/{id:int}", async (MyDbContext ctx, int id) =>
46+
{
47+
var contact = await ctx.Contacts.FindAsync(id);
48+
if (contact is null) return Results.NotFound();
49+
ctx.Contacts.Remove(contact);
50+
await ctx.SaveChangesAsync();
51+
return Results.NoContent();
52+
}).Produces(StatusCodes.Status204NoContent);
53+
54+
app.Run();
55+
56+
public record class Contact {
57+
public int Id { get; set; }
58+
public string Name { get; set; }
59+
public string City { get; set; }
60+
}
61+
62+
public class MyDbContext : DbContext
63+
{
64+
65+
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) {}
66+
67+
public DbSet<Contact> Contacts { get; set; }
68+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:22077",
7+
"sslPort": 44350
8+
}
9+
},
10+
"profiles": {
11+
"ContactApi": {
12+
"commandName": "Project",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
},
17+
"applicationUrl": "https://localhost:7069;http://localhost:5082",
18+
"dotnetRunMessages": true
19+
},
20+
"IIS Express": {
21+
"commandName": "IISExpress",
22+
"launchBrowser": true,
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
},
27+
"Docker": {
28+
"commandName": "Docker",
29+
"launchBrowser": true,
30+
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
31+
"publishAllPorts": true,
32+
"useSSL": true
33+
}
34+
}
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
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.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/net6.0/ContactApp.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17+
"console": "internalConsole",
18+
"stopAtEntry": false
19+
},
20+
{
21+
"name": ".NET Core Attach",
22+
"type": "coreclr",
23+
"request": "attach"
24+
}
25+
]
26+
}

0 commit comments

Comments
 (0)