Beispiel #1
0
        public void CanReceiveMessage()
        {
            // arrange
            var smtpServer     = new SmtpServer(_optionsBuilder.Build());
            var smtpClient     = new SmtpClient();
            var smtpServerTask = smtpServer.StartAsync(_cancellationTokenSource.Token);

            var message = new MimeKit.MimeMessage();

            message.From.Add(new MailboxAddress("*****@*****.**"));
            message.To.Add(new MailboxAddress("*****@*****.**"));
            message.Subject = "Test";
            message.Body    = new TextPart("plain")
            {
                Text = "Test Message"
            };

            // act
            smtpClient.Connect("localhost", 25, false);
            smtpClient.Send(message);

            // assert
            Assert.Equal(1, _messageStore.Messages.Count);
            Assert.Equal("*****@*****.**", _messageStore.Messages[0].From.AsAddress());
            Assert.Equal(1, _messageStore.Messages[0].To.Count);
            Assert.Equal("*****@*****.**", _messageStore.Messages[0].To[0].AsAddress());

            Wait(smtpServerTask);
        }
        /// <summary>
        /// Create a running instance of a server.
        /// </summary>
        /// <param name="configuration">The configuration to apply to run the server.</param>
        /// <returns>A disposable instance which will close and release the server instance.</returns>
        SmtpServerDisposable CreateServer(Action <OptionsBuilder> configuration)
        {
            var options = new OptionsBuilder()
                          .ServerName("localhost")
                          .Port(9025)
                          .MessageStore(MessageStore);

            configuration(options);

            var server         = new SmtpServer(options.Build());
            var smtpServerTask = server.StartAsync(CancellationTokenSource.Token);

            return(new SmtpServerDisposable(server, () =>
            {
                CancellationTokenSource.Cancel();

                try
                {
                    smtpServerTask.Wait();
                }
                catch (AggregateException e)
                {
                    e.Handle(exception => exception is OperationCanceledException);
                }
            }));
        }
        public static IServiceCollection AddScriptAPI(this IServiceCollection services, Action <OptionsBuilder> optionsBuilder)
        {
            var builder = new OptionsBuilder();

            optionsBuilder(builder);
            var options = builder.Build();

            services.AddSingleton <ScriptAPI.ServiceConfiguration.IOptions>(options);

            services.AddSingleton <ScriptAPIService>();

            var procedures = Assembly.GetEntryAssembly().GetTypes()
                             .Where(x => x.IsClass && !x.IsAbstract && typeof(IProcedure).IsAssignableFrom(x))
                             .ToArray();

            foreach (var procedure in procedures)
            {
                services.AddTransient(procedure);
            }

            services.AddSingleton(new ProcedureMapping(procedures.Select(x => (Name: x.Name, Type: x)).ToArray()));

            services.AddSingleton <ProcedureLocator>(serviceProvider => (Type type) => serviceProvider.GetRequiredService(type));

            services.AddSingleton(serviceProvider => new Host(
                                      serviceProvider.GetRequiredService <ProcedureLocator>(),
                                      serviceProvider.GetRequiredService <ProcedureMapping>())
                                  );

            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            return(services);
        }
Beispiel #4
0
        /// <summary>
        /// Builds command options.
        /// </summary>
        public static TCommand Options <TCommand>(this TCommand command, Action <IOptionsBuilder> optionsBuilder) where TCommand : CommandLineApplication
        {
            var builder = new OptionsBuilder(command);

            optionsBuilder(builder);
            builder.Build();
            return(command);
        }
        /// <summary>
        /// Creates a <see cref="DefaultSqlExecutor"/> using connections from <paramref name="connectionFactory"/>.
        /// The <paramref name="options"/> parameter allows for additional configuration.
        /// </summary>
        public DefaultSqlExecutor(Func <DbConnection> connectionFactory, Action <OptionsBuilder> options)
        {
            Throw.IfNull(connectionFactory, "connectionFactory");
            Throw.IfNull(options, "options");

            this.connectionFactory = connectionFactory;
            var builder = new OptionsBuilder();

            options(builder);
            this.options = builder.Build();
        }
Beispiel #6
0
        public void CanReceiveMessage()
        {
            // arrange
            var smtpServer     = new SmtpServer(_optionsBuilder.Build());
            var smtpClient     = new SmtpClient("localhost", 25);
            var smtpServerTask = smtpServer.StartAsync(_cancellationTokenSource.Token);

            // act
            smtpClient.Send("*****@*****.**", "*****@*****.**", "Test", "Test Message");

            // assert
            Assert.Equal(1, _messageStore.Messages.Count);
            Assert.Equal("*****@*****.**", _messageStore.Messages[0].From.AsAddress());
            Assert.Equal(1, _messageStore.Messages[0].To.Count);
            Assert.Equal("*****@*****.**", _messageStore.Messages[0].To[0].AsAddress());

            Wait(smtpServerTask);
        }
        public static IApplicationBuilder UseScriptAPI(this IApplicationBuilder app, Action <OptionsBuilder> optionsBuilder)
        {
            var builder = new OptionsBuilder();

            optionsBuilder(builder);
            var options = builder.Build();

            app.MapWhen(
                (HttpContext httpContext) => httpContext.Request.Path.StartsWithSegments(options.EndpointPath, StringComparison.OrdinalIgnoreCase),
                (IApplicationBuilder appBuilder) =>
            {
                appBuilder.Run(async(HttpContext httpContext) =>
                {
                    var scriptApiService = httpContext.RequestServices.GetRequiredService <ScriptAPIService>();
                    var result           = await scriptApiService.ExecuteAsync(httpContext.Request.Body);
                    var resultJson       = JsonConvert.SerializeObject(result, _jsonSerializerSettings);
                    await httpContext.Response.WriteAsync(resultJson);
                });
            });

            return(app);
        }