This repository was archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathStartup.cs
More file actions
146 lines (130 loc) · 5.87 KB
/
Startup.cs
File metadata and controls
146 lines (130 loc) · 5.87 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using AutoMapper;
using CacheManager.Core;
using EFSecondLevelCache.Core.AspNetCoreSample.DataLayer;
using EFSecondLevelCache.Core.AspNetCoreSample.DataLayer.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using EFSecondLevelCache.Core.AspNetCoreSample.Profiles;
using System.Reflection;
using Microsoft.Extensions.Hosting;
namespace EFSecondLevelCache.Core.AspNetCoreSample
{
public class Startup
{
private readonly string _contentRootPath;
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
_contentRootPath = env.ContentRootPath;
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IServiceScopeFactory scopeFactory)
{
//app.UseBlockingDetection();
scopeFactory.Initialize();
scopeFactory.SeedData();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddEFSecondLevelCache();
// addInMemoryCacheServiceProvider(services);
addRedisCacheServiceProvider(services);
services.AddDbContext<SampleContext>(optionsBuilder =>
{
var useInMemoryDatabase = Configuration["UseInMemoryDatabase"].Equals("true", StringComparison.OrdinalIgnoreCase);
if (useInMemoryDatabase)
{
optionsBuilder.UseInMemoryDatabase("TestDb");
}
else
{
var connectionString = Configuration["ConnectionStrings:ApplicationDbContextConnection"];
if (connectionString.Contains("%CONTENTROOTPATH%"))
{
connectionString = connectionString.Replace("%CONTENTROOTPATH%", _contentRootPath);
}
optionsBuilder.UseSqlServer(
connectionString
, serverDbContextOptionsBuilder =>
{
var minutes = (int)TimeSpan.FromMinutes(3).TotalSeconds;
serverDbContextOptionsBuilder.CommandTimeout(minutes);
});
optionsBuilder.EnableSensitiveDataLogging();
optionsBuilder.ConfigureWarnings(w =>
{
});
}
});
services.AddAutoMapper(typeof(PostProfile).GetTypeInfo().Assembly);
services.AddControllersWithViews();
}
private static void addInMemoryCacheServiceProvider(IServiceCollection services)
{
var jss = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
services.AddSingleton(typeof(ICacheManagerConfiguration),
new CacheManager.Core.ConfigurationBuilder()
.WithJsonSerializer(serializationSettings: jss, deserializationSettings: jss)
.WithMicrosoftMemoryCacheHandle(instanceName: "MemoryCache1")
.WithExpiration(ExpirationMode.Absolute, TimeSpan.FromMinutes(10))
.DisablePerformanceCounters()
.DisableStatistics()
.Build());
services.AddSingleton(typeof(ICacheManager<>), typeof(BaseCacheManager<>));
}
private static void addRedisCacheServiceProvider(IServiceCollection services)
{
var jss = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
const string redisConfigurationKey = "redis";
services.AddSingleton(typeof(ICacheManagerConfiguration),
new CacheManager.Core.ConfigurationBuilder()
.WithJsonSerializer(serializationSettings: jss, deserializationSettings: jss)
.WithUpdateMode(CacheUpdateMode.Up)
.WithRedisConfiguration(redisConfigurationKey, config =>
{
config.WithAllowAdmin()
.WithDatabase(0)
.WithEndpoint("localhost", 6379)
// Enables keyspace notifications to react on eviction/expiration of items.
// Make sure that all servers are configured correctly and 'notify-keyspace-events' is at least set to 'Exe', otherwise CacheManager will not retrieve any events.
// See https://redis.io/topics/notifications#configuration for configuration details.
.EnableKeyspaceEvents();
})
.WithMaxRetries(100)
.WithRetryTimeout(50)
.WithRedisCacheHandle(redisConfigurationKey)
.WithExpiration(ExpirationMode.Absolute, TimeSpan.FromMinutes(10))
.Build());
services.AddSingleton(typeof(ICacheManager<>), typeof(BaseCacheManager<>));
}
}
}