Skip to content

Commit

Permalink
.NET 8 sample projects
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisb92 committed Nov 24, 2023
1 parent d3525d2 commit c0fcdbf
Show file tree
Hide file tree
Showing 30 changed files with 764 additions and 0 deletions.
38 changes: 38 additions & 0 deletions OpenTracing.Contrib.sln
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "samples\net7.0\Sh
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrafficGenerator", "samples\net7.0\TrafficGenerator\TrafficGenerator.csproj", "{69E6E77E-646D-475A-9B6B-C5511C21B11C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "net8.0", "net8.0", "{71609BAA-55BF-4A0A-AE05-83D87644E44C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CustomersApi", "samples\net8.0\CustomersApi\CustomersApi.csproj", "{0DF66128-3C35-4301-98E2-8822FB56B146}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FrontendWeb", "samples\net8.0\FrontendWeb\FrontendWeb.csproj", "{744E71C6-79A5-4566-BCBA-221AF084A7EF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OrdersApi", "samples\net8.0\OrdersApi\OrdersApi.csproj", "{63E80769-4B3E-4897-ABD1-237226ED0629}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "samples\net8.0\Shared\Shared.csproj", "{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrafficGenerator", "samples\net8.0\TrafficGenerator\TrafficGenerator.csproj", "{67A203A9-9EAC-4375-9A32-E42E91C8AA22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -145,6 +157,26 @@ Global
{69E6E77E-646D-475A-9B6B-C5511C21B11C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69E6E77E-646D-475A-9B6B-C5511C21B11C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69E6E77E-646D-475A-9B6B-C5511C21B11C}.Release|Any CPU.Build.0 = Release|Any CPU
{0DF66128-3C35-4301-98E2-8822FB56B146}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0DF66128-3C35-4301-98E2-8822FB56B146}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DF66128-3C35-4301-98E2-8822FB56B146}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DF66128-3C35-4301-98E2-8822FB56B146}.Release|Any CPU.Build.0 = Release|Any CPU
{744E71C6-79A5-4566-BCBA-221AF084A7EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{744E71C6-79A5-4566-BCBA-221AF084A7EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{744E71C6-79A5-4566-BCBA-221AF084A7EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{744E71C6-79A5-4566-BCBA-221AF084A7EF}.Release|Any CPU.Build.0 = Release|Any CPU
{63E80769-4B3E-4897-ABD1-237226ED0629}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63E80769-4B3E-4897-ABD1-237226ED0629}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63E80769-4B3E-4897-ABD1-237226ED0629}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63E80769-4B3E-4897-ABD1-237226ED0629}.Release|Any CPU.Build.0 = Release|Any CPU
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC}.Release|Any CPU.Build.0 = Release|Any CPU
{67A203A9-9EAC-4375-9A32-E42E91C8AA22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67A203A9-9EAC-4375-9A32-E42E91C8AA22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67A203A9-9EAC-4375-9A32-E42E91C8AA22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67A203A9-9EAC-4375-9A32-E42E91C8AA22}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -171,6 +203,12 @@ Global
{48CD3687-6EB7-47CD-A611-8D579FBCBB3B} = {E46F4333-859A-4CC5-BD2A-7FE8892861BE}
{B4A8B209-AA79-4C34-9A12-F03E3423B330} = {E46F4333-859A-4CC5-BD2A-7FE8892861BE}
{69E6E77E-646D-475A-9B6B-C5511C21B11C} = {E46F4333-859A-4CC5-BD2A-7FE8892861BE}
{71609BAA-55BF-4A0A-AE05-83D87644E44C} = {36333C22-54F7-403C-ABC4-BECE4EE3F52D}
{0DF66128-3C35-4301-98E2-8822FB56B146} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
{744E71C6-79A5-4566-BCBA-221AF084A7EF} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
{63E80769-4B3E-4897-ABD1-237226ED0629} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
{05A112D5-65C4-4F7C-A974-94E5EFBE32DC} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
{67A203A9-9EAC-4375-9A32-E42E91C8AA22} = {71609BAA-55BF-4A0A-AE05-83D87644E44C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {832F86C2-4B74-4259-8073-04EE0C37FBCD}
Expand Down
19 changes: 19 additions & 0 deletions samples/net8.0/CustomersApi/CustomersApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\..\..\src\OpenTracing.Contrib.NetCore\OpenTracing.Contrib.NetCore.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="[8.0.0,9)" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="[8.0.0,9)" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions samples/net8.0/CustomersApi/DataStore/CustomerDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Shared;

namespace CustomersApi.DataStore;

public class CustomerDbContext : DbContext
{
public CustomerDbContext(DbContextOptions<CustomerDbContext> options)
: base(options)
{
}

public DbSet<Customer> Customers => Set<Customer>();

public void Seed()
{
if (Database.EnsureCreated())
{
Database.Migrate();

Customers.Add(new Customer(1, "Marcel Belding"));
Customers.Add(new Customer(2, "Phyllis Schriver"));
Customers.Add(new Customer(3, "Estefana Balderrama"));
Customers.Add(new Customer(4, "Kenyetta Lone"));
Customers.Add(new Customer(5, "Vernita Fernald"));
Customers.Add(new Customer(6, "Tessie Storrs"));

SaveChanges();
}
}
}
74 changes: 74 additions & 0 deletions samples/net8.0/CustomersApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using CustomersApi.DataStore;
using Microsoft.EntityFrameworkCore;
using Shared;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.WebHost.UseUrls(Constants.CustomersUrl);

// Registers and starts Jaeger (see Shared.JaegerServiceCollectionExtensions)
builder.Services.AddJaeger();

// Enables OpenTracing instrumentation for ASP.NET Core, CoreFx, EF Core
builder.Services.AddOpenTracing(ot =>
{
ot.ConfigureAspNetCore(options =>
{
// We don't need any tracing data for our health endpoint.
options.Hosting.IgnorePatterns.Add(ctx => ctx.Request.Path == "/health");
});
ot.ConfigureEntityFrameworkCore(options =>
{
// This is an example for how certain EF Core commands can be ignored.
// As en example, we're ignoring the "PRAGMA foreign_keys=ON;" commands that are executed by Sqlite.
// Remove this code to see those statements.
options.IgnorePatterns.Add(cmd => cmd.Command.CommandText.StartsWith("PRAGMA"));
});
});

// Adds a Sqlite DB to show EFCore traces.
builder.Services.AddDbContext<CustomerDbContext>(options =>
{
options.UseSqlite("Data Source=DataStore/customers.db");
});

builder.Services.AddHealthChecks()
.AddDbContextCheck<CustomerDbContext>();


var app = builder.Build();


// Load some dummy data into the db.
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<CustomerDbContext>();
dbContext.Seed();
}


// Configure the HTTP request pipeline.

app.MapGet("/", () => "Customers API");

app.MapHealthChecks("/health");

app.MapGet("/customers", async (CustomerDbContext dbContext) => await dbContext.Customers.ToListAsync());

app.MapGet("/customers/{id}", async (int id, CustomerDbContext dbContext, ILogger<Program> logger) =>
{
var customer = await dbContext.Customers.FirstOrDefaultAsync(x => x.CustomerId == id);
if (customer == null)
return Results.NotFound();
// ILogger events are sent to OpenTracing as well!
logger.LogInformation("Returning data for customer {CustomerId}", id);
return Results.Ok(customer);
});

app.Run();
12 changes: 12 additions & 0 deletions samples/net8.0/CustomersApi/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"CustomersApi": {
"commandName": "Project",
"launchBrowser": false,
"launchUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
10 changes: 10 additions & 0 deletions samples/net8.0/CustomersApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
71 changes: 71 additions & 0 deletions samples/net8.0/FrontendWeb/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using Shared;

namespace FrontendWeb.Controllers;

public class HomeController : Controller
{
private readonly HttpClient _httpClient;

public HomeController(HttpClient httpClient)
{
_httpClient = httpClient;
}

[HttpGet]
public IActionResult Index()
{
return View();
}

[HttpGet]
public async Task<IActionResult> PlaceOrder()
{
ViewBag.Customers = await GetCustomers();
return View(new PlaceOrderCommand { ItemNumber = "ABC11", Quantity = 1 });
}

[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> PlaceOrder(PlaceOrderCommand cmd)
{
if (!ModelState.IsValid)
{
ViewBag.Customers = await GetCustomers();
return View(cmd);
}

string body = JsonConvert.SerializeObject(cmd);

var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(Constants.OrdersUrl + "orders"),
Content = new StringContent(body, Encoding.UTF8, "application/json")
};

await _httpClient.SendAsync(request);

return RedirectToAction("Index");
}

private async Task<IEnumerable<SelectListItem>> GetCustomers()
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(Constants.CustomersUrl + "customers")
};

var response = await _httpClient.SendAsync(request);

response.EnsureSuccessStatusCode();

var body = await response.Content.ReadAsStringAsync();

return JsonConvert.DeserializeObject<List<Customer>>(body)
.Select(x => new SelectListItem { Value = x.CustomerId.ToString(), Text = x.Name });
}
}
14 changes: 14 additions & 0 deletions samples/net8.0/FrontendWeb/FrontendWeb.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\..\..\src\OpenTracing.Contrib.NetCore\OpenTracing.Contrib.NetCore.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions samples/net8.0/FrontendWeb/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Shared;


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.WebHost.UseUrls(Constants.FrontendUrl);

// Registers and starts Jaeger (see Shared.JaegerServiceCollectionExtensions)
builder.Services.AddJaeger();

// Enables OpenTracing instrumentation for ASP.NET Core, CoreFx, EF Core
builder.Services.AddOpenTracing();

builder.Services.AddSingleton<HttpClient>();

builder.Services.AddMvc();


var app = builder.Build();


// Configure the HTTP request pipeline.

app.MapDefaultControllerRoute();

app.Run();
12 changes: 12 additions & 0 deletions samples/net8.0/FrontendWeb/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"FrontendWeb": {
"commandName": "Project",
"launchBrowser": false,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions samples/net8.0/FrontendWeb/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>FrontendWeb</title>
<h1>FrontendWeb</h1>
<p>This is the beautiful web frontend.</p>
<p>Refresh this page a few times and check the Jaeger UI - you should already see traces!</p>

<p><a asp-action="PlaceOrder">Place an order</a></p>
13 changes: 13 additions & 0 deletions samples/net8.0/FrontendWeb/Views/Home/PlaceOrder.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@model Shared.PlaceOrderCommand
<!DOCTYPE html>
<meta charset="utf-8">
<title>FrontendWeb</title>
<h1>FrontendWeb</h1>
<p>Place an order</p>

<form asp-action="PlaceOrder" asp-antiforgery="true">
<div>Customer: @Html.DropDownListFor(x => x.CustomerId, (IEnumerable<SelectListItem>)ViewBag.Customers)</div>
<div>ItemNumber: @Html.TextBoxFor(x => x.ItemNumber)</div>
<div>Quantity: @Html.TextBoxFor(x => x.Quantity)</div>
<input type="submit" value="Place order"/>
</form>
2 changes: 2 additions & 0 deletions samples/net8.0/FrontendWeb/Views/_ViewImports.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@using FrontendWeb
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
11 changes: 11 additions & 0 deletions samples/net8.0/FrontendWeb/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information",
"OpenTracing": "Debug"
}
}
}
Loading

0 comments on commit c0fcdbf

Please sign in to comment.