Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Configuration(IAppBuilder app)
- {
- var httpConfig = new HttpConfiguration();
- var container = SimpleInjectorWebApiInitializer.Initialize(httpConfig);
- var config = (IConfigurationProvider)httpConfig.DependencyResolver
- .GetService(typeof(IConfigurationProvider));
- ConfigureJwt(app, config);
- ConfigureWebApi(app, httpConfig, config);
- ConfigureHangfire(app, container);
- }
- private void ConfigureHangfire(IAppBuilder app, Container container)
- {
- Hangfire.GlobalConfiguration.Configuration
- .UseSqlServerStorage("Hangfire");
- Hangfire.GlobalConfiguration.Configuration
- .UseActivator(new SimpleInjectorJobActivator(container));
- app.UseHangfireDashboard();
- app.UseHangfireServer();
- }
- public static Container Initialize(HttpConfiguration config)
- {
- var container = new Container();
- container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
- InitializeContainer(container);
- container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
- container.RegisterWebApiControllers(config);
- container.RegisterMvcIntegratedFilterProvider();
- container.Register<Mailer>(Lifestyle.Scoped);
- container.Register<PortalContext>(Lifestyle.Scoped);
- container.RegisterSingleton<TemplateProvider, TemplateProvider>();
- container.Verify();
- DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
- config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
- return container;
- }
- public class MailNotificationHandler : IAsyncNotificationHandler<FeedbackCreated>
- {
- private readonly Mailer mailer;
- public MailNotificationHandler(Mailer mailer)
- {
- this.mailer = mailer;
- }
- public Task Handle(FeedbackCreated notification)
- {
- BackgroundJob.Enqueue<Mailer>(x => x.SendFeedbackToSender(notification.FeedbackId));
- BackgroundJob.Enqueue<Mailer>(x => x.SendFeedbackToManagement(notification.FeedbackId));
- return Task.FromResult(0);
- }
- }
- public class Mailer
- {
- private readonly PortalContext dbContext;
- private readonly TemplateProvider templateProvider;
- public Mailer(PortalContext dbContext, TemplateProvider templateProvider)
- {
- this.dbContext = dbContext;
- this.templateProvider = templateProvider;
- }
- public void SendFeedbackToSender(int feedbackId)
- {
- Feedback feedback = dbContext.Feedbacks.Find(feedbackId);
- Send(TemplateType.FeedbackSender, new { Name = feedback.CreateUserId });
- }
- public void SendFeedbackToManagement(int feedbackId)
- {
- Feedback feedback = dbContext.Feedbacks.Find(feedbackId);
- Send(TemplateType.FeedbackManagement, new { Name = feedback.CreateUserId });
- }
- public void Send(TemplateType templateType, object model)
- {
- MailMessage msg = templateProvider.Get(templateType, model).ToMailMessage();
- using (var client = new SmtpClient())
- {
- client.Send(msg);
- }
- }
- }
- var container = new Container();
- container.Options.DefaultScopedLifestyle = Lifestyle.CreateHybrid(
- new AsyncScopedLifestyle(),
- new WebRequestLifestyle());
- container.Register<DbContext>(() => new DbContext(...), Lifestyle.Scoped);
- public interface IMailer
- {
- void SendFeedbackToSender(int feedbackId);
- void SendFeedbackToManagement(int feedbackId);
- }
- public class MailNotificationHandler : IAsyncNotificationHandler<FeedbackCreated>
- {
- private readonly IMailer mailer;
- public MailNotificationHandler(IMailer mailer)
- {
- this.mailer = mailer;
- }
- public Task Handle(FeedbackCreated notification)
- {
- this.mailer.SendFeedbackToSender(notification.FeedbackId));
- this.mailer.SendFeedbackToManagement(notification.FeedbackId));
- return Task.FromResult(0);
- }
- }
- // Part of your composition root
- private sealed class HangfireBackgroundMailer : IMailer
- {
- public void SendFeedbackToSender(int feedbackId) {
- BackgroundJob.Enqueue<Mailer>(m => m.SendFeedbackToSender(feedbackId));
- }
- public void SendFeedbackToManagement(int feedbackId) {
- BackgroundJob.Enqueue<Mailer>(m => m.SendFeedbackToManagement(feedbackId));
- }
- }
- container.Register<IMailer, HangfireBackgroundMailer>(Lifestyle.Singleton);
- container.Register<Mailer>(Lifestyle.Transient);
Add Comment
Please, Sign In to add comment