Ejemplo n.º 1
0
        public void TestTransmitterNoRecipient()
        {
            var loggerMoq = new Mock <ILogger>();
            var smtpMoq   = new Mock <SmtpClient>();
            //smtpMoq.Setup(smtpMoq => smtpMoq.Send(It.IsAny<MailMessage>())).Verifiable();

            var logger = loggerMoq.Object;

            var transmitter = new EmailTransmitter(logger, "server", "100", "user", "password")
            {
                Client = smtpMoq.Object
            };

            transmitter.Transmit("", "");

            Assert.AreNotEqual("server", transmitter.Client.Host);
            Assert.AreNotEqual(100, transmitter.Client.Port);
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var logFile = Configuration.GetValue <string>("LogFile");

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .WriteTo.File(
                logFile,
                fileSizeLimitBytes: 1_000_000,
                rollOnFileSizeLimit: true,
                shared: true,
                flushToDiskInterval: TimeSpan.FromSeconds(1))
                         .CreateLogger();

            services.AddDbContext <NotificationDbContext>(options =>
                                                          options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddTransient <INotificationEntryRepository, NotificationEntryRepository>();

            // add jobs to the DI pipeline to allow them to receive DBContext
            services.AddTransient <ContractJob>();
            services.AddTransient <ProjectJob>();
            services.AddTransient <ReminderJob>();

            _ = services.AddMassTransit(x =>
            {
                x.AddConsumer <Controllers.AddNotificationItemConsumer>();
                x.AddConsumer <Controllers.RemoveNotificationItemConsumer>();
                x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    cfg.UseHealthCheck(provider);
                    cfg.Host(new Uri("rabbitmq://localhost"), h =>
                    {
                        h.Username("guest");
                        h.Password("guest");
                    });
                    cfg.ReceiveEndpoint(MessageBusQueueNames.ADDNOTIFYITEM, ep =>
                    {
                        ep.PrefetchCount = 16;
                        ep.UseMessageRetry(r =>
                        {
                            r.Interval(2, 100);
                        });
                        ep.ConfigureConsumer <Controllers.AddNotificationItemConsumer>(provider);
                    });
                    cfg.ReceiveEndpoint(MessageBusQueueNames.REMOVENOTIFYITEM, ep =>
                    {
                        ep.PrefetchCount = 16;
                        ep.UseMessageRetry(r =>
                        {
                            r.Interval(2, 100);
                        });
                        ep.ConfigureConsumer <Controllers.RemoveNotificationItemConsumer>(provider);
                    });
                }));
            });
            services.AddMassTransitHostedService();

            services.AddQuartz(q =>
            {
                // base quartz scheduler, job and trigger configuration
                q.UseMicrosoftDependencyInjectionJobFactory(
                    p => p.AllowDefaultConstructor = true
                    );
            });

            // ASP.NET Core hosting
            services.AddQuartzServer(options =>
            {
                // when shutting down we want jobs to complete gracefully
                options.WaitForJobsToComplete = true;
            });

            var section = Configuration.GetSection("Mail");
            IMessageTransmitter transmitter = new EmailTransmitter(null,
                                                                   section.GetValue <string>("Server"),
                                                                   section.GetValue <string>("Port"),
                                                                   section.GetValue <string>("Username"),
                                                                   section.GetValue <string>("Password")
                                                                   );

            services.AddSingleton <IMessageTransmitter>(transmitter);

            // First we must get a reference to a scheduler
            ISchedulerFactory sf        = new StdSchedulerFactory();
            IScheduler        scheduler = sf.GetScheduler().Result;

            // add the jobs if needed
            AddJob <ContractJob>(scheduler, uPromis.Services.Notification.NotificationType.CONTRACTNOTIFICATION);
            AddJob <ProjectJob>(scheduler, uPromis.Services.Notification.NotificationType.PROJECTNOTIFICATION);
            AddJob <ReminderJob>(scheduler, uPromis.Services.Notification.NotificationType.REMINDERNOTIFICATION);

            services.AddSingleton <IScheduler>(scheduler);

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "uPromis.Microservice.Notification", Version = "v1"
                });
            });

            services.AddLogging(loggingBuilder =>
            {
                loggingBuilder.AddSerilog(dispose: true);
            }
                                );
            services.AddAuthentication("Bearer")
            .AddJwtBearer("Bearer", options =>
            {
                options.Authority            = "http://localhost:5000";
                options.RequireHttpsMetadata = false;

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateAudience = false
                };
            });
            services.AddAuthorization(options =>
            {
                options.AddPolicy("ApiScope", policy =>
                {
                    policy.RequireAuthenticatedUser();
                    policy.RequireClaim("scope", "api3");
                });
            });
            services.AddCors(options =>
            {
                options.AddPolicy(MyAllowSpecificOrigins,
                                  builder =>
                {
                    builder.WithOrigins("http://localhost:3000")
                    .AllowAnyMethod()
                    .AllowAnyHeader();
                    //    builder.WithOrigins("https://localhost:3001")
                    //        .AllowAnyMethod()
                    //        .AllowAnyHeader();
                });
            });
        }