forked from VahidN/EFCoreSecondLevelCacheInterceptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EFServiceProvider.cs
74 lines (64 loc) · 3.16 KB
/
EFServiceProvider.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
69
70
71
72
73
74
using System;
using Microsoft.Extensions.DependencyInjection;
using System.Threading;
using System.IO;
using EFCoreSecondLevelCacheInterceptor.Tests.DataLayer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace EFCoreSecondLevelCacheInterceptor.ConsoleSample
{
public static class EFServiceProvider
{
private static readonly Lazy<IServiceProvider> _serviceProviderBuilder =
new Lazy<IServiceProvider>(getServiceProvider, LazyThreadSafetyMode.ExecutionAndPublication);
/// <summary>
/// A lazy loaded thread-safe singleton
/// </summary>
public static IServiceProvider Instance { get; } = _serviceProviderBuilder.Value;
public static T GetRequiredService<T>()
{
return Instance.GetRequiredService<T>();
}
public static void RunInContext(Action<ApplicationDbContext> action)
{
using var serviceScope = GetRequiredService<IServiceScopeFactory>().CreateScope();
using var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
action(context);
}
public static async Task RunInContextAsync(Func<ApplicationDbContext, Task> action)
{
using var serviceScope = GetRequiredService<IServiceScopeFactory>().CreateScope();
using var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await action(context);
}
private static IServiceProvider getServiceProvider()
{
var services = new ServiceCollection();
services.AddOptions();
services.AddLogging(cfg => cfg.AddConsole().AddDebug());
services.AddEFSecondLevelCache(options => options.UseMemoryCacheProvider());
var basePath = Directory.GetCurrentDirectory();
Console.WriteLine($"Using `{basePath}` as the ContentRootPath");
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
services.AddSingleton(_ => configuration);
services.AddConfiguredMsSqlDbContext(getConnectionString(basePath, configuration));
return services.BuildServiceProvider();
}
private static string getConnectionString(string basePath, IConfigurationRoot configuration)
{
var testsFolder = basePath.Split(new[] { "\\Tests\\" }, StringSplitOptions.RemoveEmptyEntries)[0];
var contentRootPath = Path.Combine(testsFolder, "Tests", "EFCoreSecondLevelCacheInterceptor.AspNetCoreSample");
var connectionString = configuration["ConnectionStrings:ApplicationDbContextConnection"];
if (connectionString.Contains("%CONTENTROOTPATH%"))
{
connectionString = connectionString.Replace("%CONTENTROOTPATH%", contentRootPath);
}
Console.WriteLine($"Using {connectionString}");
return connectionString;
}
}
}