Exemple #1
0
        /*
         * Helper method to wire up and create our IServiceProvider correctly.
         */
        static IServiceProvider Initialize()
        {
            var services = new ServiceCollection();

            // Initializing slots, first by making sure we retrieve all classes implementin ISlot, and having
            // the SlotAttribute declared as an attribute.
            var slots = AppDomain.CurrentDomain.GetAssemblies()
                        .Where(x => !x.IsDynamic && !x.FullName.StartsWith("Microsoft", StringComparison.InvariantCulture))
                        .SelectMany(s => s.GetTypes())
                        .Where(p => (typeof(ISlot).IsAssignableFrom(p) || typeof(ISlotAsync).IsAssignableFrom(p)) &&
                               !p.IsInterface &&
                               !p.IsAbstract &&
                               p.CustomAttributes.Any(x => x.AttributeType == typeof(SlotAttribute)));

            // Adding each slot type as a transient service.
            foreach (var idx in slots)
            {
                services.AddTransient(idx);
            }

            // Making sure we use the default ISignalsProvider and ISignaler services.
            var provider = new SignalsProvider(slots);

            services.AddSingleton <ISignalsProvider>((svc) => provider);
            services.AddTransient <ISignaler, Signaler>();

            // Building and returning service provider to caller.
            return(services.BuildServiceProvider());
        }
Exemple #2
0
        public static IServiceProvider InitializeServices(Action <Node> functor)
        {
            var services = new ServiceCollection();

            services.AddTransient <ISignaler, Signaler>();

            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            services.AddTransient <ILogger>((svc) =>
            {
                return(new TestLogger(
                           svc.GetService <ISignaler>(),
                           functor));
            });
            var mockConfiguration = new Mock <IConfiguration>();

            mockConfiguration
            .SetupGet(x => x[It.Is <string>(x => x == "magic:databases:default")])
            .Returns(() => "mysql");
            mockConfiguration
            .SetupGet(x => x[It.Is <string>(x => x == "magic:logging:database")])
            .Returns(() => "magic");
            services.AddTransient((svc) => mockConfiguration.Object);
            return(services.BuildServiceProvider());
        }
        static IServiceProvider Initialize()
        {
            var services = new ServiceCollection();

            services.AddTransient <ISignaler, Signaler>();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            var provider = services.BuildServiceProvider();

            return(provider);
        }
        static IServiceProvider Initialize()
        {
            CryptographyContext.Register(typeof(CreatePgpMimeContext));
            var services = new ServiceCollection();

            services.AddTransient <ISignaler, Signaler>();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            var provider = services.BuildServiceProvider();

            return(provider);
        }
        public static ISignaler Initialize(bool config = true)
        {
            var services = new ServiceCollection();

            services.AddTransient <ISignaler, Signaler>();
            services.AddSingleton <IMagicCache, MagicMemoryCache>();
            services.AddTransient <IRootResolver, RootResolver>();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            var provider = services.BuildServiceProvider();

            return(provider.GetService <ISignaler>());
        }
Exemple #6
0
        static IServiceProvider Initialize()
        {
            var configuration = new ConfigurationBuilder().Build();
            var services      = new ServiceCollection();

            services.AddTransient <IConfiguration>((svc) => configuration);
            services.AddTransient <ISignaler, Signaler>();
            services.AddSingleton(typeof(ThreadRunner));
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            var provider = services.BuildServiceProvider();

            return(provider);
        }
        public static ISignaler Initialize()
        {
            var services = new ServiceCollection();

            services.AddTransient <ISignaler, Signaler>();
            var mockConfiguration = new Mock <IConfiguration>();

            mockConfiguration.SetupGet(x => x[It.IsAny <string>()]).Returns("bar-xx");
            services.AddTransient((svc) => mockConfiguration.Object);
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            var provider = services.BuildServiceProvider();

            return(provider.GetService <ISignaler>());
        }
Exemple #8
0
        public static IServiceProvider Initialize()
        {
            var services          = new ServiceCollection();
            var mockConfiguration = new Mock <IConfiguration>();

            mockConfiguration.SetupGet(x => x[It.IsAny <string>()]).Returns("60");
            services.AddTransient((svc) => mockConfiguration.Object);
            services.AddTransient <ISignaler, Signaler>();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            services.AddTransient <IExecutorAsync, ExecutorAsync>();
            var provider = services.BuildServiceProvider();

            Utilities.RootFolder = AppDomain.CurrentDomain.BaseDirectory;
            return(provider);
        }
Exemple #9
0
        static IServiceProvider Initialize(MockSmtpClient smtp, MockPop3Client pop3)
        {
            var services = new ServiceCollection();

            services.AddTransient <ISignaler, Signaler>();
            services.AddTransient <IRootResolver, RootResolver>();
            if (smtp != null)
            {
                services.AddTransient <ISmtpClient>((svc) => smtp);
            }
            if (pop3 != null)
            {
                services.AddTransient <IPop3Client>((svc) => pop3);
            }

            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            services.AddTransient <IStreamService, StreamService>();
            services.AddTransient <IFileService, FileService>();
            services.AddTransient <ConnectionSettingsSmtp>((svc) => new ConnectionSettingsSmtp
            {
                Host     = "foo2.com",
                Port     = 321,
                Secure   = false,
                Username = "******",
                Password = "******",
                From     = new Sender
                {
                    Name    = "Foo Bar",
                    Address = "*****@*****.**",
                }
            });
            services.AddTransient <ConnectionSettingsPop3>((svc) => new ConnectionSettingsPop3
            {
                Host     = "foo2.com",
                Port     = 321,
                Secure   = false,
                Username = "******",
                Password = "******"
            });

            return(services.BuildServiceProvider());
        }
Exemple #10
0
        static IServiceProvider Initialize(
            FileService fileService     = null,
            FolderService folderService = null,
            StreamService streamService = null)
        {
            var configuration = new ConfigurationBuilder().Build();
            var services      = new ServiceCollection();

            services.AddTransient <IConfiguration>((svc) => configuration);
            services.AddTransient <ISignaler, Signaler>();
            if (fileService == null)
            {
                services.AddTransient <IFileService, file.services.FileService>();
            }
            else
            {
                services.AddTransient <IFileService>(svc => fileService);
            }
            if (folderService == null)
            {
                services.AddTransient <IFolderService, folder.services.FolderService>();
            }
            else
            {
                services.AddTransient <IFolderService>(svc => folderService);
            }
            if (streamService == null)
            {
                services.AddTransient <IStreamService, stream.services.StreamService>();
            }
            else
            {
                services.AddTransient <IStreamService>(svc => streamService);
            }
            services.AddTransient <IRootResolver, RootResolver>();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            var provider = services.BuildServiceProvider();

            return(provider);
        }
        public static ISignaler Initialize(bool createTicket = true, bool config = true)
        {
            var services = new ServiceCollection();

            services.AddTransient <ISignaler, Signaler>();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            services.AddTransient <ITicketProvider, TicketProvider>((svc) => new TicketProvider(createTicket));
            var mockConfiguration = new Mock <IConfiguration>();

            if (config)
            {
                mockConfiguration.SetupGet(x => x[It.Is <string>(x2 => x2 == "magic:auth:secret")]).Returns("some-secret-goes-here");
                mockConfiguration.SetupGet(x => x[It.Is <string>(x2 => x2 == "magic:auth:valid-minutes")]).Returns("20");
            }
            services.AddTransient((svc) => mockConfiguration.Object);
            var provider = services.BuildServiceProvider();

            return(provider.GetService <ISignaler>());
        }
Exemple #12
0
        public static ISignaler Initialize(bool config = true)
        {
            var services          = new ServiceCollection();
            var mockConfiguration = new Mock <IConfiguration>();

            mockConfiguration
            .SetupGet(x => x[It.Is <string>(x => x == "magic:caching:expiration")])
            .Returns(() => config ? "5" : null);
            mockConfiguration
            .SetupGet(x => x[It.Is <string>(x => x == "magic:caching:expiration-type")])
            .Returns(() => config ? "sliding" : null);
            services.AddTransient((svc) => mockConfiguration.Object);
            services.AddTransient <ISignaler, Signaler>();
            services.AddMemoryCache();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            var provider = services.BuildServiceProvider();

            return(provider.GetService <ISignaler>());
        }
Exemple #13
0
        public static IServiceProvider Initialize()
        {
            var services = new ServiceCollection();

            var mockConfiguration = new Mock <IMagicConfiguration>();

            mockConfiguration.SetupGet(x => x[It.IsAny <string>()]).Returns("60");
            services.AddTransient((svc) => mockConfiguration.Object);

            services.AddTransient <ISignaler, Signaler>();
            services.AddTransient <IHttpArgumentsHandler, HttpArgumentsHandler>();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            services.AddTransient <IHttpExecutorAsync, HttpExecutorAsync>();
            services.AddTransient <IFileService, FileService>();
            services.AddTransient <IFolderService, FolderService>();
            services.AddTransient <IRootResolver, RootResolver>();

            return(services.BuildServiceProvider());
        }
Exemple #14
0
        FilesController CreateController(Type authType = null)
        {
            var services = new ServiceCollection();

            services.AddTransient <IFileService, FileService>();
            services.AddTransient <FilesController>();

            var mockConfiguration = new Mock <IConfiguration>();

            mockConfiguration.SetupGet(x => x[It.IsAny <string>()]).Returns("~/");
            services.AddTransient((svc) => mockConfiguration.Object);
            services.AddTransient <ISignaler, Signaler>();
            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);

            if (authType == typeof(AuthorizeOnlyRoles))
            {
                services.AddSingleton <IAuthorize>((svc) => new AuthorizeOnlyRoles("foo"));
            }
            else if (authType == typeof(AuthorizeLambda))
            {
                services.AddSingleton <IAuthorize>(new AuthorizeLambda((path, username, roles, accessType) => {
                    if (path.EndsWith("error.txt"))
                    {
                        return(false);
                    }
                    return(true);
                }));
            }
            else if (authType != null)
            {
                services.AddTransient(typeof(IAuthorize), authType);
            }


            var provider = services.BuildServiceProvider();

            return(provider.GetService(typeof(FilesController)) as FilesController);
        }
Exemple #15
0
        static IServiceProvider Initialize(MockSmtpClient smtp, MockPop3Client pop3)
        {
            var services = new ServiceCollection();

            services.AddTransient <ISignaler, Signaler>();
            var mockConfiguration = new Mock <IConfiguration>();

            if (smtp != null)
            {
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:smtp:host")]).Returns("foo2.com");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:smtp:port")]).Returns("321");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:smtp:secure")]).Returns("false");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:smtp:username")]).Returns("xxx2");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:smtp:password")]).Returns("yyy2");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:smtp:from:name")]).Returns("Foo Bar");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:smtp:from:address")]).Returns("*****@*****.**");
                services.AddTransient <ISmtpClient>((svc) => smtp);
            }
            if (pop3 != null)
            {
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:pop3:host")]).Returns("foo2.com");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:pop3:port")]).Returns("321");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:pop3:secure")]).Returns("false");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:pop3:username")]).Returns("xxx2");
                mockConfiguration.SetupGet(x => x[It.Is <string>(y => y == "magic:pop3:password")]).Returns("yyy2");
                services.AddTransient <IPop3Client>((svc) => pop3);
            }
            services.AddTransient((svc) => mockConfiguration.Object);

            var types = new SignalsProvider(InstantiateAllTypes <ISlot>(services));

            services.AddTransient <ISignalsProvider>((svc) => types);
            var provider = services.BuildServiceProvider();

            return(provider);
        }