@@ -21,47 +21,42 @@ public class OpenSessionMiddleware
21
21
/// next middleware execution and releases resources when execution returns.
22
22
/// </summary>
23
23
/// <param name="context">The <see cref="HttpContext"/>.</param>
24
+ /// <param name="domainFromServices"><see cref="Domain"/> regestered as service.</param>
25
+ /// <param name="sessionAccessor"><see cref="SessionAccessor"/> registered as service.</param>
24
26
/// <returns>Task perfroming this operation.</returns>
25
- public async Task Invoke ( HttpContext context )
27
+ public async Task Invoke ( HttpContext context , Domain domainFromServices , SessionAccessor sessionAccessor )
26
28
{
27
- var sessionAccessor = ( SessionAccessor ) context . RequestServices . GetService ( WellKnownTypes . SessionAccessorType ) ;
28
- if ( sessionAccessor != null ) {
29
- var domain = GetDomainFromServices ( context ) ;
30
- var session = await OpenSessionAsync ( domain , context ) ;
31
- var transaction = await OpenTransactionAsync ( session , context ) ;
32
- BindResourcesToContext ( session , transaction , context ) ;
33
-
34
- using ( sessionAccessor . BindHttpContext ( context ) ) {
35
- try {
36
- await next . Invoke ( context ) ;
29
+ var session = await OpenSessionAsync ( domainFromServices , context ) ;
30
+ var transaction = await OpenTransactionAsync ( session , context ) ;
31
+ BindResourcesToContext ( session , transaction , context ) ;
32
+
33
+ using ( sessionAccessor . BindHttpContext ( context ) ) {
34
+ try {
35
+ await next . Invoke ( context ) ;
36
+ transaction . Complete ( ) ;
37
+ }
38
+ catch ( Exception exception ) {
39
+ // if we caught exception here then
40
+ // 1) it is unhendeled exception
41
+ // or
42
+ // 2) it was thrown intentionally
43
+ if ( CompleteTransactionOnException ( exception , context ) ) {
37
44
transaction . Complete ( ) ;
38
45
}
39
- catch ( Exception exception ) {
40
- // if we caught exception here then
41
- // 1) it is unhendeled exception
42
- // or
43
- // 2) it was thrown intentionally
44
- if ( CompleteTransactionOnException ( exception , context ) ) {
45
- transaction . Complete ( ) ;
46
- }
47
- if ( RethrowException ( exception , context ) ) {
48
- ExceptionDispatchInfo . Throw ( exception ) ;
49
- }
50
- }
51
- finally {
52
- RemoveResourcesFromContext ( context ) ;
53
- await OnTransactionDisposingAsync ( transaction , session , context ) ;
54
- await transaction . DisposeAsync ( ) ;
55
- await OnTransactionDisposedAsync ( session , context ) ;
56
-
57
- await OnSessionDisposingAsync ( session , context ) ;
58
- await session . DisposeAsync ( ) ;
59
- await OnSessionDisposedAsync ( context ) ;
46
+ if ( RethrowException ( exception , context ) ) {
47
+ ExceptionDispatchInfo . Throw ( exception ) ;
60
48
}
61
49
}
62
- }
63
- else {
64
- await next . Invoke ( context ) ;
50
+ finally {
51
+ RemoveResourcesFromContext ( context ) ;
52
+ await OnTransactionDisposingAsync ( transaction , session , context ) ;
53
+ await transaction . DisposeAsync ( ) ;
54
+ await OnTransactionDisposedAsync ( session , context ) ;
55
+
56
+ await OnSessionDisposingAsync ( session , context ) ;
57
+ await session . DisposeAsync ( ) ;
58
+ await OnSessionDisposedAsync ( context ) ;
59
+ }
65
60
}
66
61
}
67
62
@@ -142,12 +137,6 @@ private static void RemoveResourcesFromContext(HttpContext context)
142
137
_ = context . Items . Remove ( SessionAccessor . SessionIdentifier ) ;
143
138
}
144
139
145
- private static Domain GetDomainFromServices ( HttpContext context )
146
- {
147
- var domain = ( Domain ) context . RequestServices . GetService ( WellKnownTypes . DomainType ) ;
148
- return domain ?? throw new InvalidOperationException ( "Domain is not found among registered services." ) ;
149
- }
150
-
151
140
/// <summary>
152
141
/// Creates an instance of <see cref="OpenSessionMiddleware"/>
153
142
/// </summary>
0 commit comments