Skip to content

Commit c0fcdbf

Browse files
committed
.NET 8 sample projects
1 parent d3525d2 commit c0fcdbf

30 files changed

+764
-0
lines changed

OpenTracing.Contrib.sln

+38
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "samples\net7.0\Sh
6767
EndProject
6868
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrafficGenerator", "samples\net7.0\TrafficGenerator\TrafficGenerator.csproj", "{69E6E77E-646D-475A-9B6B-C5511C21B11C}"
6969
EndProject
70+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "net8.0", "net8.0", "{71609BAA-55BF-4A0A-AE05-83D87644E44C}"
71+
EndProject
72+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomersApi", "samples\net8.0\CustomersApi\CustomersApi.csproj", "{0DF66128-3C35-4301-98E2-8822FB56B146}"
73+
EndProject
74+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FrontendWeb", "samples\net8.0\FrontendWeb\FrontendWeb.csproj", "{744E71C6-79A5-4566-BCBA-221AF084A7EF}"
75+
EndProject
76+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrdersApi", "samples\net8.0\OrdersApi\OrdersApi.csproj", "{63E80769-4B3E-4897-ABD1-237226ED0629}"
77+
EndProject
78+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "samples\net8.0\Shared\Shared.csproj", "{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}"
79+
EndProject
80+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrafficGenerator", "samples\net8.0\TrafficGenerator\TrafficGenerator.csproj", "{67A203A9-9EAC-4375-9A32-E42E91C8AA22}"
81+
EndProject
7082
Global
7183
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7284
Debug|Any CPU = Debug|Any CPU
@@ -145,6 +157,26 @@ Global
145157
{69E6E77E-646D-475A-9B6B-C5511C21B11C}.Debug|Any CPU.Build.0 = Debug|Any CPU
146158
{69E6E77E-646D-475A-9B6B-C5511C21B11C}.Release|Any CPU.ActiveCfg = Release|Any CPU
147159
{69E6E77E-646D-475A-9B6B-C5511C21B11C}.Release|Any CPU.Build.0 = Release|Any CPU
160+
{0DF66128-3C35-4301-98E2-8822FB56B146}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
161+
{0DF66128-3C35-4301-98E2-8822FB56B146}.Debug|Any CPU.Build.0 = Debug|Any CPU
162+
{0DF66128-3C35-4301-98E2-8822FB56B146}.Release|Any CPU.ActiveCfg = Release|Any CPU
163+
{0DF66128-3C35-4301-98E2-8822FB56B146}.Release|Any CPU.Build.0 = Release|Any CPU
164+
{744E71C6-79A5-4566-BCBA-221AF084A7EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
165+
{744E71C6-79A5-4566-BCBA-221AF084A7EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
166+
{744E71C6-79A5-4566-BCBA-221AF084A7EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
167+
{744E71C6-79A5-4566-BCBA-221AF084A7EF}.Release|Any CPU.Build.0 = Release|Any CPU
168+
{63E80769-4B3E-4897-ABD1-237226ED0629}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
169+
{63E80769-4B3E-4897-ABD1-237226ED0629}.Debug|Any CPU.Build.0 = Debug|Any CPU
170+
{63E80769-4B3E-4897-ABD1-237226ED0629}.Release|Any CPU.ActiveCfg = Release|Any CPU
171+
{63E80769-4B3E-4897-ABD1-237226ED0629}.Release|Any CPU.Build.0 = Release|Any CPU
172+
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
173+
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
174+
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
175+
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}.Release|Any CPU.Build.0 = Release|Any CPU
176+
{67A203A9-9EAC-4375-9A32-E42E91C8AA22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
177+
{67A203A9-9EAC-4375-9A32-E42E91C8AA22}.Debug|Any CPU.Build.0 = Debug|Any CPU
178+
{67A203A9-9EAC-4375-9A32-E42E91C8AA22}.Release|Any CPU.ActiveCfg = Release|Any CPU
179+
{67A203A9-9EAC-4375-9A32-E42E91C8AA22}.Release|Any CPU.Build.0 = Release|Any CPU
148180
EndGlobalSection
149181
GlobalSection(SolutionProperties) = preSolution
150182
HideSolutionNode = FALSE
@@ -171,6 +203,12 @@ Global
171203
{48CD3687-6EB7-47CD-A611-8D579FBCBB3B} = {E46F4333-859A-4CC5-BD2A-7FE8892861BE}
172204
{B4A8B209-AA79-4C34-9A12-F03E3423B330} = {E46F4333-859A-4CC5-BD2A-7FE8892861BE}
173205
{69E6E77E-646D-475A-9B6B-C5511C21B11C} = {E46F4333-859A-4CC5-BD2A-7FE8892861BE}
206+
{71609BAA-55BF-4A0A-AE05-83D87644E44C} = {36333C22-54F7-403C-ABC4-BECE4EE3F52D}
207+
{0DF66128-3C35-4301-98E2-8822FB56B146} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
208+
{744E71C6-79A5-4566-BCBA-221AF084A7EF} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
209+
{63E80769-4B3E-4897-ABD1-237226ED0629} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
210+
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
211+
{67A203A9-9EAC-4375-9A32-E42E91C8AA22} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
174212
EndGlobalSection
175213
GlobalSection(ExtensibilityGlobals) = postSolution
176214
SolutionGuid = {832F86C2-4B74-4259-8073-04EE0C37FBCD}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\Shared\Shared.csproj" />
11+
<ProjectReference Include="..\..\..\src\OpenTracing.Contrib.NetCore\OpenTracing.Contrib.NetCore.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="[8.0.0,9)" />
16+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="[8.0.0,9)" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Shared;
3+
4+
namespace CustomersApi.DataStore;
5+
6+
public class CustomerDbContext : DbContext
7+
{
8+
public CustomerDbContext(DbContextOptions<CustomerDbContext> options)
9+
: base(options)
10+
{
11+
}
12+
13+
public DbSet<Customer> Customers => Set<Customer>();
14+
15+
public void Seed()
16+
{
17+
if (Database.EnsureCreated())
18+
{
19+
Database.Migrate();
20+
21+
Customers.Add(new Customer(1, "Marcel Belding"));
22+
Customers.Add(new Customer(2, "Phyllis Schriver"));
23+
Customers.Add(new Customer(3, "Estefana Balderrama"));
24+
Customers.Add(new Customer(4, "Kenyetta Lone"));
25+
Customers.Add(new Customer(5, "Vernita Fernald"));
26+
Customers.Add(new Customer(6, "Tessie Storrs"));
27+
28+
SaveChanges();
29+
}
30+
}
31+
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using CustomersApi.DataStore;
2+
using Microsoft.EntityFrameworkCore;
3+
using Shared;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Add services to the container.
8+
9+
builder.WebHost.UseUrls(Constants.CustomersUrl);
10+
11+
// Registers and starts Jaeger (see Shared.JaegerServiceCollectionExtensions)
12+
builder.Services.AddJaeger();
13+
14+
// Enables OpenTracing instrumentation for ASP.NET Core, CoreFx, EF Core
15+
builder.Services.AddOpenTracing(ot =>
16+
{
17+
ot.ConfigureAspNetCore(options =>
18+
{
19+
// We don't need any tracing data for our health endpoint.
20+
options.Hosting.IgnorePatterns.Add(ctx => ctx.Request.Path == "/health");
21+
});
22+
23+
ot.ConfigureEntityFrameworkCore(options =>
24+
{
25+
// This is an example for how certain EF Core commands can be ignored.
26+
// As en example, we're ignoring the "PRAGMA foreign_keys=ON;" commands that are executed by Sqlite.
27+
// Remove this code to see those statements.
28+
options.IgnorePatterns.Add(cmd => cmd.Command.CommandText.StartsWith("PRAGMA"));
29+
});
30+
});
31+
32+
// Adds a Sqlite DB to show EFCore traces.
33+
builder.Services.AddDbContext<CustomerDbContext>(options =>
34+
{
35+
options.UseSqlite("Data Source=DataStore/customers.db");
36+
});
37+
38+
builder.Services.AddHealthChecks()
39+
.AddDbContextCheck<CustomerDbContext>();
40+
41+
42+
var app = builder.Build();
43+
44+
45+
// Load some dummy data into the db.
46+
using (var scope = app.Services.CreateScope())
47+
{
48+
var dbContext = scope.ServiceProvider.GetRequiredService<CustomerDbContext>();
49+
dbContext.Seed();
50+
}
51+
52+
53+
// Configure the HTTP request pipeline.
54+
55+
app.MapGet("/", () => "Customers API");
56+
57+
app.MapHealthChecks("/health");
58+
59+
app.MapGet("/customers", async (CustomerDbContext dbContext) => await dbContext.Customers.ToListAsync());
60+
61+
app.MapGet("/customers/{id}", async (int id, CustomerDbContext dbContext, ILogger<Program> logger) =>
62+
{
63+
var customer = await dbContext.Customers.FirstOrDefaultAsync(x => x.CustomerId == id);
64+
65+
if (customer == null)
66+
return Results.NotFound();
67+
68+
// ILogger events are sent to OpenTracing as well!
69+
logger.LogInformation("Returning data for customer {CustomerId}", id);
70+
71+
return Results.Ok(customer);
72+
});
73+
74+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"CustomersApi": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"launchUrl": "http://localhost:5001",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"LogLevel": {
5+
"Default": "Debug",
6+
"System": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Text;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.Rendering;
4+
using Newtonsoft.Json;
5+
using Shared;
6+
7+
namespace FrontendWeb.Controllers;
8+
9+
public class HomeController : Controller
10+
{
11+
private readonly HttpClient _httpClient;
12+
13+
public HomeController(HttpClient httpClient)
14+
{
15+
_httpClient = httpClient;
16+
}
17+
18+
[HttpGet]
19+
public IActionResult Index()
20+
{
21+
return View();
22+
}
23+
24+
[HttpGet]
25+
public async Task<IActionResult> PlaceOrder()
26+
{
27+
ViewBag.Customers = await GetCustomers();
28+
return View(new PlaceOrderCommand { ItemNumber = "ABC11", Quantity = 1 });
29+
}
30+
31+
[HttpPost, ValidateAntiForgeryToken]
32+
public async Task<IActionResult> PlaceOrder(PlaceOrderCommand cmd)
33+
{
34+
if (!ModelState.IsValid)
35+
{
36+
ViewBag.Customers = await GetCustomers();
37+
return View(cmd);
38+
}
39+
40+
string body = JsonConvert.SerializeObject(cmd);
41+
42+
var request = new HttpRequestMessage
43+
{
44+
Method = HttpMethod.Post,
45+
RequestUri = new Uri(Constants.OrdersUrl + "orders"),
46+
Content = new StringContent(body, Encoding.UTF8, "application/json")
47+
};
48+
49+
await _httpClient.SendAsync(request);
50+
51+
return RedirectToAction("Index");
52+
}
53+
54+
private async Task<IEnumerable<SelectListItem>> GetCustomers()
55+
{
56+
var request = new HttpRequestMessage
57+
{
58+
Method = HttpMethod.Get,
59+
RequestUri = new Uri(Constants.CustomersUrl + "customers")
60+
};
61+
62+
var response = await _httpClient.SendAsync(request);
63+
64+
response.EnsureSuccessStatusCode();
65+
66+
var body = await response.Content.ReadAsStringAsync();
67+
68+
return JsonConvert.DeserializeObject<List<Customer>>(body)
69+
.Select(x => new SelectListItem { Value = x.CustomerId.ToString(), Text = x.Name });
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\Shared\Shared.csproj" />
11+
<ProjectReference Include="..\..\..\src\OpenTracing.Contrib.NetCore\OpenTracing.Contrib.NetCore.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

samples/net8.0/FrontendWeb/Program.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Shared;
2+
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
// Add services to the container.
7+
8+
builder.WebHost.UseUrls(Constants.FrontendUrl);
9+
10+
// Registers and starts Jaeger (see Shared.JaegerServiceCollectionExtensions)
11+
builder.Services.AddJaeger();
12+
13+
// Enables OpenTracing instrumentation for ASP.NET Core, CoreFx, EF Core
14+
builder.Services.AddOpenTracing();
15+
16+
builder.Services.AddSingleton<HttpClient>();
17+
18+
builder.Services.AddMvc();
19+
20+
21+
var app = builder.Build();
22+
23+
24+
// Configure the HTTP request pipeline.
25+
26+
app.MapDefaultControllerRoute();
27+
28+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"FrontendWeb": {
4+
"commandName": "Project",
5+
"launchBrowser": false,
6+
"launchUrl": "http://localhost:5000",
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
}
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>FrontendWeb</title>
4+
<h1>FrontendWeb</h1>
5+
<p>This is the beautiful web frontend.</p>
6+
<p>Refresh this page a few times and check the Jaeger UI - you should already see traces!</p>
7+
8+
<p><a asp-action="PlaceOrder">Place an order</a></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@model Shared.PlaceOrderCommand
2+
<!DOCTYPE html>
3+
<meta charset="utf-8">
4+
<title>FrontendWeb</title>
5+
<h1>FrontendWeb</h1>
6+
<p>Place an order</p>
7+
8+
<form asp-action="PlaceOrder" asp-antiforgery="true">
9+
<div>Customer: @Html.DropDownListFor(x => x.CustomerId, (IEnumerable<SelectListItem>)ViewBag.Customers)</div>
10+
<div>ItemNumber: @Html.TextBoxFor(x => x.ItemNumber)</div>
11+
<div>Quantity: @Html.TextBoxFor(x => x.Quantity)</div>
12+
<input type="submit" value="Place order"/>
13+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@using FrontendWeb
2+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"LogLevel": {
5+
"Default": "Debug",
6+
"System": "Information",
7+
"Microsoft": "Information",
8+
"OpenTracing": "Debug"
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)