-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
40 lines (29 loc) · 1.35 KB
/
Program.cs
File metadata and controls
40 lines (29 loc) · 1.35 KB
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
using Microsoft.EntityFrameworkCore;
using SimpleExampleUsingRedis.Models;
using StackExchange.Redis;
namespace SimpleExampleUsingRedis
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// ïîëó÷àåì ñòðîêó ïîäêëþ÷åíèÿ èç ôàéëà êîíôèãóðàöèè
string connection = builder.Configuration.GetConnectionString("DefaultConnection");
// äîáàâëÿåì êîíòåêñò ApplicationContext â êà÷åñòâå ñåðâèñà â ïðèëîæåíèå
builder.Services.AddDbContext<CarContext>(options => options.UseSqlServer(connection));
builder.Services.AddMvc(); // äîáàâëÿåì ñåðâèñû MVC
builder.Services.AddSingleton<IRedisCacheService, RedisCacheService>();// äîáàâëÿåì ñåðâèñ Redis
var app = builder.Build();
// Ïîëó÷àåì êîíòåêñò èç ïðèëîæåíèÿ
using var serviceScope = app.Services.CreateScope();
var context = serviceScope.ServiceProvider.GetRequiredService<CarContext>();
//await CarsGenerator.GenerateRandomCarAsync(context, 10);
// óñòàíàâëèâàåì ñîïîñòàâëåíèå ìàðøðóòîâ ñ êîíòðîëëåðàìè
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
}
}
}