public async Task <T> ExecuteAsync <T>(IDomainContext domainContext, MethodInfo method) { if (domainContext == null) { throw new ArgumentNullException(nameof(domainContext)); } if (method == null) { throw new ArgumentNullException(nameof(method)); } if (method.DeclaringType != GetType()) { throw new ArgumentException("该方法不是此领域服务的方法。"); } IDomainServiceFilter[] filters = _FilterCache.GetOrAdd(method, t => t.GetCustomAttributes <DomainServiceFilterAttribute>().ToArray()); var accessor = domainContext.GetRequiredService <IDomainServiceAccessor>(); var context = new DomainExecutionContext(this, domainContext, method); var executionContext = ExecutionContext.Capture(); Context = context; accessor.DomainService = this; try { await Task.WhenAll(filters.Select(t => t.OnExecutingAsync(context))); if (Executing != null) { await Executing(context); } var result = await(Task <T>) method.Invoke(this, context.ParameterValues); context.Result = result; if (Executed != null) { await Executed(context); } await Task.WhenAll(filters.Select(t => t.OnExecutedAsync(context))); return(result); } catch (Exception ex) { await Task.WhenAll(filters.Select(t => t.OnExceptionThrowingAsync(context, ex))); throw ex; } finally { accessor.DomainService = null; } }
public async Task ExecuteAsync(IDomainContext domainContext, MethodInfo method) { if (domainContext == null) { throw new ArgumentNullException(nameof(domainContext)); } if (method == null) { throw new ArgumentNullException(nameof(method)); } if (!method.DeclaringType.IsAssignableFrom(GetType())) { throw new ArgumentException("该方法不是此领域服务的方法。"); } IDomainServiceFilter[] methodFilters = _FilterCache.GetOrAdd(method, t => t.GetCustomAttributes <DomainServiceFilterAttribute>().ToArray()).ToArray(); var accessor = domainContext.GetRequiredService <IDomainServiceAccessor>(); var context = new DomainExecutionContext(this, domainContext, method); Context = context; accessor.DomainService = this; try { await OnFilterExecuting(context, methodFilters); if (context.IsCompleted) { return; } await RaiseAsyncEvent(ExecutingEvent); if (context.IsCompleted) { return; } var synchronizationContext = SynchronizationContext.Current; await(Task) method.Invoke(this, context.ParameterValues); SynchronizationContext.SetSynchronizationContext(synchronizationContext); await RaiseAsyncEvent(ExecutedEvent); if (context.IsCompleted) { return; } await OnFilterExecuted(context, methodFilters); } catch (Exception ex) { await OnFilterThrowing(context, methodFilters, ex); if (context.IsCompleted) { return; } ExceptionDispatchInfo.Capture(ex).Throw(); throw; } finally { accessor.DomainService = null; Context = null; } }