|
1 |
| -using Microsoft.AspNetCore.Hosting; |
2 |
| -using Microsoft.Extensions.DependencyInjection; |
3 |
| -using Microsoft.Extensions.Hosting; |
| 1 | +using CustomersApi.DataStore; |
| 2 | +using Microsoft.EntityFrameworkCore; |
4 | 3 | using Shared;
|
5 | 4 |
|
6 |
| -namespace Samples.CustomersApi |
| 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 => |
7 | 16 | {
|
8 |
| - public class Program |
| 17 | + ot.ConfigureAspNetCore(options => |
9 | 18 | {
|
10 |
| - public static void Main(string[] args) |
11 |
| - { |
12 |
| - CreateHostBuilder(args).Build().Run(); |
13 |
| - } |
14 |
| - |
15 |
| - public static IHostBuilder CreateHostBuilder(string[] args) |
16 |
| - { |
17 |
| - return Host.CreateDefaultBuilder(args) |
18 |
| - .ConfigureWebHostDefaults(webBuilder => |
19 |
| - { |
20 |
| - webBuilder |
21 |
| - .UseStartup<Startup>() |
22 |
| - .UseUrls(Constants.CustomersUrl); |
23 |
| - }) |
24 |
| - .ConfigureServices(services => |
25 |
| - { |
26 |
| - // Registers and starts Jaeger (see Shared.JaegerServiceCollectionExtensions) |
27 |
| - services.AddJaeger(); |
28 |
| - |
29 |
| - // Enables OpenTracing instrumentation for ASP.NET Core, CoreFx, EF Core |
30 |
| - services.AddOpenTracing(builder => |
31 |
| - { |
32 |
| - builder.ConfigureAspNetCore(options => |
33 |
| - { |
34 |
| - // We don't need any tracing data for our health endpoint. |
35 |
| - options.Hosting.IgnorePatterns.Add(ctx => ctx.Request.Path == "/health"); |
36 |
| - }); |
37 |
| - |
38 |
| - builder.ConfigureEntityFrameworkCore(options => |
39 |
| - { |
40 |
| - // This is an example for how certain EF Core commands can be ignored. |
41 |
| - // As en example, we're ignoring the "PRAGMA foreign_keys=ON;" commands that are executed by Sqlite. |
42 |
| - // Remove this code to see those statements. |
43 |
| - options.IgnorePatterns.Add(cmd => cmd.Command.CommandText.StartsWith("PRAGMA")); |
44 |
| - }); |
45 |
| - }); |
46 |
| - |
47 |
| - }); |
48 |
| - } |
49 |
| - } |
| 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 | 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(); |
0 commit comments