-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathHttpContextLifetimeProvider.cs
125 lines (115 loc) · 3.38 KB
/
HttpContextLifetimeProvider.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
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
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
using System.Collections.Concurrent;
using System.Linq;
using Microsoft.AspNetCore.Http;
using React.Exceptions;
using React.TinyIoC;
using Microsoft.Extensions.DependencyInjection;
namespace React.AspNet
{
/// <summary>
/// Handles registering per-request objects in the dependency injection container.
/// </summary>
internal class HttpContextLifetimeProvider : TinyIoCContainer.ITinyIoCObjectLifetimeProvider
{
private readonly IHttpContextAccessor _httpContextAccessor;
/// <summary>
/// Creates a new <see cref="HttpContextLifetimeProvider" />.
/// </summary>
public HttpContextLifetimeProvider(IHttpContextAccessor httpContextAccessor)
{
if (httpContextAccessor == null)
{
throw new ReactNotInitialisedException(
"IHttpContextAccessor is not registered correctly. Please add it to your " +
"application startup:\n" +
"services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();"
);
}
_httpContextAccessor = httpContextAccessor;
}
/// <summary>
/// Prefix to use on HttpContext items
/// </summary>
private const string PREFIX = "React.PerRequest.";
/// <summary>
/// Name of the key for this particular registration
/// </summary>
private readonly string _keyName = PREFIX + Guid.NewGuid();
/// <summary>
/// Gets the current per-request registrations for the current request.
/// </summary>
private PerRequestRegistrations Registrations
{
get
{
var requestServices = _httpContextAccessor.HttpContext.RequestServices;
var registrations = requestServices.GetService<PerRequestRegistrations>();
if (registrations == null)
{
throw new ReactNotInitialisedException(
"ReactJS.NET has not been initialised correctly. Please ensure you have " +
"called services.AddReact() and app.UseReact() in your Startup.cs file."
);
}
return registrations;
}
}
/// <summary>
/// Gets the value of this item in the dependency injection container.
/// </summary>
/// <returns></returns>
public object GetObject()
{
object value;
Registrations.TryGetValue(_keyName, out value);
return value;
}
/// <summary>
/// Sets the value of this item in the dependency injection container.
/// </summary>
/// <param name="value">Value to set</param>
public void SetObject(object value)
{
Registrations[_keyName] = value;
}
/// <summary>
/// Removes this item from the dependency injection container.
/// </summary>
public void ReleaseObject()
{
object value;
if (Registrations.TryRemove(_keyName, out value))
{
if (value is IDisposable)
{
((IDisposable)value).Dispose();
}
}
}
/// <summary>
/// Contains all per-request dependency injection registrations.
/// </summary>
internal class PerRequestRegistrations : ConcurrentDictionary<string, object>, IDisposable
{
/// <summary>
/// Disposes all registrations in this container.
/// </summary>
public void Dispose()
{
foreach (var kvp in this.Where(kvp => kvp.Value is IDisposable))
{
((IDisposable)kvp.Value).Dispose();
}
}
}
}
}