-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
764 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
samples/net8.0/CustomersApi/DataStore/CustomerDbContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
samples/net8.0/CustomersApi/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@using FrontendWeb | ||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
Oops, something went wrong.