|
| 1 | +// Copyright (C) 2021 Xtensive LLC. |
| 2 | +// This code is distributed under MIT license terms. |
| 3 | +// See the License.txt file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Runtime.ExceptionServices; |
| 9 | + |
| 10 | +namespace Xtensive.Orm.Web |
| 11 | +{ |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// DataObjects.Net Session providing middleware. |
| 15 | + /// </summary> |
| 16 | + public class OpenSessionMiddlewere |
| 17 | + { |
| 18 | + private readonly RequestDelegate next; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Opens session and puts it to registered <see cref="SessionAccessor"/> service before |
| 22 | + /// next middleware execution and releases resources when execution returns. |
| 23 | + /// </summary> |
| 24 | + /// <param name="context">The <see cref="HttpContext"/>.</param> |
| 25 | + /// <returns>Task perfroming this operation.</returns> |
| 26 | + public async Task Invoke(HttpContext context) |
| 27 | + { |
| 28 | + var sessionAccessor = (SessionAccessor) context.RequestServices.GetService(WellKnownTypes.SessionAccessorType); |
| 29 | + if (sessionAccessor != null) { |
| 30 | + var domain = GetDomainFromServices(context); |
| 31 | + var session = await OpenSessionAsync(domain, context); |
| 32 | + var transaction = await OpenTransactionAsync(session, context); |
| 33 | + BindResourcesToContext(session, transaction, context); |
| 34 | + |
| 35 | + using (sessionAccessor.BindHttpContext(context)) { |
| 36 | + try { |
| 37 | + await next.Invoke(context); |
| 38 | + } |
| 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 transaction.DisposeAsync(); |
| 54 | + await session.DisposeAsync(); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + else { |
| 59 | + await next.Invoke(context); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Opens session asynchronously. |
| 65 | + /// </summary> |
| 66 | + /// <param name="domain">Domain instance</param> |
| 67 | + /// <param name="context">Action executing context</param> |
| 68 | + /// <returns>Instance of <see cref="Session"/>.</returns> |
| 69 | + protected virtual Task<Session> OpenSessionAsync(Domain domain, HttpContext context) => domain.OpenSessionAsync(); |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Opens transaction scope asynchronously. |
| 73 | + /// </summary> |
| 74 | + /// <param name="session">Domain instance.</param> |
| 75 | + /// <param name="context">Action executing context.</param> |
| 76 | + /// <returns>Instance of <see cref="TransactionScope"/>.</returns> |
| 77 | + protected virtual async Task<TransactionScope> OpenTransactionAsync(Session session, HttpContext context) => |
| 78 | + await session.OpenTransactionAsync(); |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Determines whether transaction should be completed even though exception appeared. |
| 82 | + /// </summary> |
| 83 | + /// <param name="thrownException">Thrown exception instance.</param> |
| 84 | + /// <param name="context">The <see cref="HttpContext"/>.</param> |
| 85 | + /// <returns><see langword="true"/>if transaction should be rollbacked, otherwise, <see langword="false"/>.</returns> |
| 86 | + protected virtual bool CompleteTransactionOnException(Exception thrownException, HttpContext context) => false; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Determines whether exception should be re-thrown. |
| 90 | + /// </summary> |
| 91 | + /// <param name="thrownException">Thrown exception instance.</param> |
| 92 | + /// <param name="context">The <see cref="HttpContext"/>.</param> |
| 93 | + /// <returns><see langword="true"/>if the exception should be re-thrown, otherwise, <see langword="false"/>.</returns> |
| 94 | + protected virtual bool RethrowException(Exception thrownException, HttpContext context) => true; |
| 95 | + |
| 96 | + private static void BindResourcesToContext(Session session, TransactionScope transactionScope, HttpContext context) |
| 97 | + { |
| 98 | + context.Items[SessionAccessor.SessionIdentifier] = session; |
| 99 | + context.Items[SessionAccessor.ScopeIdentifier] = transactionScope; |
| 100 | + } |
| 101 | + |
| 102 | + private static void RemoveResourcesFromContext(HttpContext context) |
| 103 | + { |
| 104 | + _ = context.Items.Remove(SessionAccessor.ScopeIdentifier); |
| 105 | + _ = context.Items.Remove(SessionAccessor.SessionIdentifier); |
| 106 | + } |
| 107 | + |
| 108 | + private static Domain GetDomainFromServices(HttpContext context) |
| 109 | + { |
| 110 | + var domain = (Domain) context.RequestServices.GetService(WellKnownTypes.DomainType); |
| 111 | + return domain == null |
| 112 | + ? throw new InvalidOperationException("Domain is not found among registered services.") |
| 113 | + : domain; |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Creates an instance of <see cref="OpenSessionMiddlewere"/> |
| 118 | + /// </summary> |
| 119 | + /// <param name="next"></param> |
| 120 | + public OpenSessionMiddlewere(RequestDelegate next) |
| 121 | + { |
| 122 | + this.next = next; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments