-
Notifications
You must be signed in to change notification settings - Fork 936
/
Copy pathCallSessionContext.cs
58 lines (54 loc) · 1.55 KB
/
CallSessionContext.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
using System;
using System.Collections;
using System.Threading;
using NHibernate.Engine;
#if NETFX
using System.Runtime.Remoting.Messaging;
#endif
namespace NHibernate.Context
{
#pragma warning disable CS1574
/// <summary>
/// Provides a <see cref="ISessionFactory.GetCurrentSession()">current session</see>
/// for each <see cref="System.Runtime.Remoting.Messaging.CallContext"/>.
/// Uses <see cref="AsyncLocal{T}"/> instead if run under .NET Core/.NET Standard.
/// <remarks>
/// <para>Not recommended for .NET 2.0 web applications.</para>
/// </remarks>
/// </summary>
#pragma warning restore CS1574
[Serializable]
public class CallSessionContext : MapBasedSessionContext
{
#if NETFX
private const string SessionFactoryMapKey = "NHibernate.Context.CallSessionContext.SessionFactoryMapKey";
#else
private static readonly AsyncLocal<IDictionary> SessionFactoryMap = new AsyncLocal<IDictionary>();
#endif
public CallSessionContext(ISessionFactoryImplementor factory) : base(factory)
{
}
/// <summary>
/// The key is the session factory and the value is the bound session.
/// </summary>
protected override void SetMap(IDictionary value)
{
#if NETFX
CallContext.SetData(SessionFactoryMapKey, value);
#else
SessionFactoryMap.Value = value;
#endif
}
/// <summary>
/// The key is the session factory and the value is the bound session.
/// </summary>
protected override IDictionary GetMap()
{
#if NETFX
return CallContext.GetData(SessionFactoryMapKey) as IDictionary;
#else
return SessionFactoryMap.Value;
#endif
}
}
}