Guest User

Untitled

a guest
Jul 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. public void Configuration(IAppBuilder app)
  2. {
  3. var httpConfig = new HttpConfiguration();
  4.  
  5. var container = SimpleInjectorWebApiInitializer.Initialize(httpConfig);
  6.  
  7. var config = (IConfigurationProvider)httpConfig.DependencyResolver
  8. .GetService(typeof(IConfigurationProvider));
  9.  
  10. ConfigureJwt(app, config);
  11. ConfigureWebApi(app, httpConfig, config);
  12. ConfigureHangfire(app, container);
  13. }
  14. private void ConfigureHangfire(IAppBuilder app, Container container)
  15. {
  16. Hangfire.GlobalConfiguration.Configuration
  17. .UseSqlServerStorage("Hangfire");
  18.  
  19. Hangfire.GlobalConfiguration.Configuration
  20. .UseActivator(new SimpleInjectorJobActivator(container));
  21.  
  22. app.UseHangfireDashboard();
  23. app.UseHangfireServer();
  24. }
  25.  
  26. public static Container Initialize(HttpConfiguration config)
  27. {
  28. var container = new Container();
  29. container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
  30.  
  31. InitializeContainer(container);
  32.  
  33. container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
  34. container.RegisterWebApiControllers(config);
  35. container.RegisterMvcIntegratedFilterProvider();
  36.  
  37. container.Register<Mailer>(Lifestyle.Scoped);
  38. container.Register<PortalContext>(Lifestyle.Scoped);
  39. container.RegisterSingleton<TemplateProvider, TemplateProvider>();
  40.  
  41. container.Verify();
  42.  
  43. DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
  44.  
  45. config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
  46.  
  47. return container;
  48. }
  49.  
  50. public class MailNotificationHandler : IAsyncNotificationHandler<FeedbackCreated>
  51. {
  52. private readonly Mailer mailer;
  53.  
  54. public MailNotificationHandler(Mailer mailer)
  55. {
  56. this.mailer = mailer;
  57. }
  58.  
  59. public Task Handle(FeedbackCreated notification)
  60. {
  61. BackgroundJob.Enqueue<Mailer>(x => x.SendFeedbackToSender(notification.FeedbackId));
  62. BackgroundJob.Enqueue<Mailer>(x => x.SendFeedbackToManagement(notification.FeedbackId));
  63.  
  64. return Task.FromResult(0);
  65. }
  66. }
  67.  
  68. public class Mailer
  69. {
  70. private readonly PortalContext dbContext;
  71. private readonly TemplateProvider templateProvider;
  72.  
  73. public Mailer(PortalContext dbContext, TemplateProvider templateProvider)
  74. {
  75. this.dbContext = dbContext;
  76. this.templateProvider = templateProvider;
  77. }
  78.  
  79. public void SendFeedbackToSender(int feedbackId)
  80. {
  81. Feedback feedback = dbContext.Feedbacks.Find(feedbackId);
  82.  
  83. Send(TemplateType.FeedbackSender, new { Name = feedback.CreateUserId });
  84. }
  85.  
  86. public void SendFeedbackToManagement(int feedbackId)
  87. {
  88. Feedback feedback = dbContext.Feedbacks.Find(feedbackId);
  89.  
  90. Send(TemplateType.FeedbackManagement, new { Name = feedback.CreateUserId });
  91. }
  92.  
  93. public void Send(TemplateType templateType, object model)
  94. {
  95. MailMessage msg = templateProvider.Get(templateType, model).ToMailMessage();
  96.  
  97. using (var client = new SmtpClient())
  98. {
  99. client.Send(msg);
  100. }
  101. }
  102. }
  103.  
  104. var container = new Container();
  105.  
  106. container.Options.DefaultScopedLifestyle = Lifestyle.CreateHybrid(
  107. new AsyncScopedLifestyle(),
  108. new WebRequestLifestyle());
  109.  
  110. container.Register<DbContext>(() => new DbContext(...), Lifestyle.Scoped);
  111.  
  112. public interface IMailer
  113. {
  114. void SendFeedbackToSender(int feedbackId);
  115. void SendFeedbackToManagement(int feedbackId);
  116. }
  117.  
  118. public class MailNotificationHandler : IAsyncNotificationHandler<FeedbackCreated>
  119. {
  120. private readonly IMailer mailer;
  121.  
  122. public MailNotificationHandler(IMailer mailer)
  123. {
  124. this.mailer = mailer;
  125. }
  126.  
  127. public Task Handle(FeedbackCreated notification)
  128. {
  129. this.mailer.SendFeedbackToSender(notification.FeedbackId));
  130. this.mailer.SendFeedbackToManagement(notification.FeedbackId));
  131.  
  132. return Task.FromResult(0);
  133. }
  134. }
  135.  
  136. // Part of your composition root
  137. private sealed class HangfireBackgroundMailer : IMailer
  138. {
  139. public void SendFeedbackToSender(int feedbackId) {
  140. BackgroundJob.Enqueue<Mailer>(m => m.SendFeedbackToSender(feedbackId));
  141. }
  142.  
  143. public void SendFeedbackToManagement(int feedbackId) {
  144. BackgroundJob.Enqueue<Mailer>(m => m.SendFeedbackToManagement(feedbackId));
  145. }
  146. }
  147.  
  148. container.Register<IMailer, HangfireBackgroundMailer>(Lifestyle.Singleton);
  149. container.Register<Mailer>(Lifestyle.Transient);
Add Comment
Please, Sign In to add comment