public IServiceScope CreateScope() { var httpContext = _httpContextAccessor.HttpContext; string tenant = null; if (httpContext != null) { tenant = _tenantResolver.ResolveTenant(httpContext); } var scopedConatiner = _tenantContainerProvider.GetTenantContainer(tenant).OpenScope(Reuse.WebRequestScopeName); return(scopedConatiner.Resolve <IServiceScope>()); }
#pragma warning disable UseAsyncSuffix // Use Async suffix public async Task Invoke(HttpContext httpContext, ITenantResolver tenantResolver, ITenantProvider tenantProvider) #pragma warning restore UseAsyncSuffix // Use Async suffix { var tenant = tenantResolver.ResolveTenant(httpContext); if (string.IsNullOrEmpty(tenant)) { // Call the next middleware delegate in the pipeline await _next.Invoke(httpContext); } else { using (tenantProvider.BeginScope(tenant)) { await _next.Invoke(httpContext); } } }
#pragma warning disable UseAsyncSuffix // Use Async suffix public async Task Invoke(HttpContext httpContext, ITenantResolver tenantResolver, ITenantProvider tenantProvider) #pragma warning restore UseAsyncSuffix // Use Async suffix { var tenant = tenantResolver.ResolveTenant(httpContext); if (string.IsNullOrEmpty(tenant)) { // Call the next middleware delegate in the pipeline await _next.Invoke(httpContext); } else { var tenantContainerProvider = (ITenantContainerProvider)httpContext.RequestServices.GetService(typeof(ITenantContainerProvider)); var tenantContainer = tenantContainerProvider.GetTenantContainer(tenant); using (var scopedConatiner = tenantContainer.OpenScope(Reuse.WebRequestScopeName)) { // replace the RequestScope container by the tenant request scope httpContext.RequestServices = scopedConatiner.Resolve <IServiceProvider>(); // Call the next middleware delegate in the pipeline await _next.Invoke(httpContext); } } }
public async Task <MessageResult> Process(ContactFormMessage message) { var errorList = new List <MessageError>(); var sender = await _emailSenderResolver.GetEmailSender(); if (sender == null) { var logMessage = $"failed to send account confirmation email because email settings are not populated"; _log.LogError(logMessage); var m = new MessageError { Code = "NoSenderError", Description = logMessage }; errorList.Add(m); return(MessageResult.Failed(errorList.ToArray())); } var form = await _contactFormResolver.GetCurrentContactForm().ConfigureAwait(false); message.Tenant = await _tenantResolver.ResolveTenant(); try { var plainTextMessage = await _viewRenderer.RenderViewAsString <ContactFormMessage>(_messageProcessorOptions.NotificationTextViewName, message); var htmlMessage = await _viewRenderer.RenderViewAsString <ContactFormMessage>(_messageProcessorOptions.NotificationHtmlViewName, message); var replyTo = message.Email; await sender.SendEmailAsync( form.NotificationEmailCsv, null, message.Subject, plainTextMessage, htmlMessage, replyTo ).ConfigureAwait(false); if (form.CopySubmitterEmailOnSubmission) { try { plainTextMessage = await _viewRenderer.RenderViewAsString <ContactFormMessage>( _messageProcessorOptions.SubmitterNotificationTextViewName, message); htmlMessage = await _viewRenderer.RenderViewAsString <ContactFormMessage>( _messageProcessorOptions.SubmitterNotificationHtmlViewName, message); await sender.SendEmailAsync( message.Email, null, message.Subject, plainTextMessage, htmlMessage ).ConfigureAwait(false); } catch (Exception ex) { _log.LogError($"error sending contact form submitter notification email {ex.Message} : {ex.StackTrace}"); var m = new MessageError { Code = "SubmitterNotificationError", Description = ex.Message }; errorList.Add(m); } } } catch (Exception ex) { _log.LogError($"error sending contact form notification email: {ex.Message} : {ex.StackTrace}"); var m = new MessageError { Code = "NotificationError", Description = ex.Message }; errorList.Add(m); } if (errorList.Count > 0) { return(MessageResult.Failed(errorList.ToArray())); } return(MessageResult.Success); }