Skip to content

Commit b1e55c1

Browse files
committed
Make infrastructure provide required services
1 parent 0039b84 commit b1e55c1

File tree

2 files changed

+30
-42
lines changed

2 files changed

+30
-42
lines changed

Extensions/Xtensive.Orm.Web.Tests/MiddlewareTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public async Task NoDomainAsServiceTest()
100100

101101
using (var host = await hostBuilder.StartAsync()) {
102102
var ex = Assert.ThrowsAsync<InvalidOperationException>(async () => await host.GetTestClient().GetAsync("/"));
103-
Assert.That(ex.Message.Contains("Domain is not found"), Is.True);
104103
}
105104
}
106105

Extensions/Xtensive.Orm.Web/Middleware/OpenSessionMiddleware.cs

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,42 @@ public class OpenSessionMiddleware
2121
/// next middleware execution and releases resources when execution returns.
2222
/// </summary>
2323
/// <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>
2426
/// <returns>Task perfroming this operation.</returns>
25-
public async Task Invoke(HttpContext context)
27+
public async Task Invoke(HttpContext context, Domain domainFromServices, SessionAccessor sessionAccessor)
2628
{
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)) {
3744
transaction.Complete();
3845
}
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);
6048
}
6149
}
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+
}
6560
}
6661
}
6762

@@ -142,12 +137,6 @@ private static void RemoveResourcesFromContext(HttpContext context)
142137
_ = context.Items.Remove(SessionAccessor.SessionIdentifier);
143138
}
144139

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-
151140
/// <summary>
152141
/// Creates an instance of <see cref="OpenSessionMiddleware"/>
153142
/// </summary>

0 commit comments

Comments
 (0)