-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathStartup.cs
68 lines (59 loc) · 2.25 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Samples.Remote.Api.Data;
using StackExchange.Profiling;
namespace Samples.Remote.Api
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// Register MiniProfiler
services.AddMiniProfiler()
.AddEntityFramework();
var connection = new SqliteConnection("DataSource=:memory:");
// The Sqlite in memory dB is alive till the connection is open, so for the sake of simplicity we're opening
// the connection here and keep it open forever, this should not be done in real world scenario
connection.Open();
services.AddDbContext<SampleContext>(options =>
{
options.UseSqlite(connection);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseMiniProfiler();
app.Use((ctx, next) =>
{
// Add the ID of the current miniprofiler session to the response headers
ctx.Response.Headers.Add("MiniProfiler-Remote-Id", MiniProfiler.Current.Id.ToString());
return next();
});
app.UseHttpsRedirection();
app.UseMvc();
// Initialize the dB - don try this at home!
using (var scope = app.ApplicationServices.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<SampleContext>();
context.Database.EnsureCreated();
context.Samples.AddRange(
Enumerable.Range(1, 10).Select(index => new Sample { Name = $"Smaple-{index}" }));
context.SaveChanges();
}
}
}
}