Exemplo n.º 1
0
        public Task<int> Main(string[] args)
        {
            //Add command line configuration source to read command line parameters.
            var config = new Configuration();
            config.AddCommandLine(args);

            var serviceCollection = new ServiceCollection();
            serviceCollection.Add(HostingServices.GetDefaultServices(config));
            var services = serviceCollection.BuildServiceProvider(_hostServiceProvider);

            var context = new HostingContext()
            {
                Services = services,
                Configuration = config,
                ServerName = "Microsoft.AspNet.Server.WebListener",
                ApplicationName = "BugTracker"
            };

            var engine = services.GetService<IHostingEngine>();
            if (engine == null)
            {
                throw new Exception("TODO: IHostingEngine service not available exception");
            }

            using (engine.Start(context))
            {
                Console.WriteLine("Started the server..");
                Console.WriteLine("Press any key to stop the server");
                Console.ReadLine();
            }
            return Task.FromResult(0);
        }
Exemplo n.º 2
0
        public void Main(string[] args)
        {
            var config = new Configuration();
            if (File.Exists(HostingIniFile))
            {
                config.AddIniFile(HostingIniFile);
            }
            config.AddEnvironmentVariables();
            config.AddCommandLine(args);

            var context = new HostingContext()
            {
                Configuration = config,
                ServerFactoryLocation = config.Get("server"),
                ApplicationName = config.Get("app")
            };

            var engine = new HostingEngine(_serviceProvider);

            var serverShutdown = engine.Start(context);
            var loggerFactory = context.ApplicationServices.GetRequiredService<ILoggerFactory>();
            var appShutdownService = context.ApplicationServices.GetRequiredService<IApplicationShutdown>();
            var shutdownHandle = new ManualResetEvent(false);

            appShutdownService.ShutdownRequested.Register(() =>
            {
                try
                {
                    serverShutdown.Dispose();
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger<Program>();
                    logger.LogError("Dispose threw an exception.", ex);
                }
                shutdownHandle.Set();
            });

            var ignored = Task.Run(() =>
            {
                Console.WriteLine("Started");
                Console.ReadLine();
                appShutdownService.RequestShutdown();
            });

            shutdownHandle.WaitOne();
        }
Exemplo n.º 3
0
        public void Main(string[] args)
        {
            var configuration = new Configuration();
            Console.WriteLine("Initial Config Sources: " + configuration.Sources.Count());

            var defaultSettings = new MemoryConfigurationSource();
            defaultSettings.Set("username", "Guest");
            configuration.Add(defaultSettings);
            Console.WriteLine("Added Memory Source. Sources: " + configuration.Sources.Count());

            configuration.AddCommandLine(args);
            Console.WriteLine("Added Command Line Source. Sources: " + configuration.Sources.Count());

            string username = configuration.Get("username");

            Console.WriteLine($"Hello, {username}!");
        }
Exemplo n.º 4
0
        public void CommitConfiguration()
        {
            var toolMock = new Mock<MyMigrationTool>() { CallBase = true };
            var args
                = new[]
                      {
                          "--ConfigFile=MyConfig.ini",
                          "--ContextAssembly=EntityFramework.Design.Tests.dll",
                          "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext",
                          "--MigrationAssembly=EntityFramework.Design.Tests.dll",
                          "--MigrationNamespace=MyNamespace",
                          "--MigrationDirectory=MyDirectory",
                          "--References=Ref1;Ref2;Ref3"
                      };
            var configSourceMock = new Mock<IniFileConfigurationSource>("Foo") { CallBase = true };
            var tool = toolMock.Object;
            var configuration = new Configuration();

            configSourceMock
                .Setup(m => m.Load());

            configSourceMock
                .Setup(m => m.Commit())
                .Callback(
                    () =>
                    {
                        Assert.Equal(configSourceMock.Object.Data["ContextAssembly"], "EntityFramework.Design.Tests.dll");
                        Assert.Equal(configSourceMock.Object.Data["ContextType"], "Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext");
                        Assert.Equal(configSourceMock.Object.Data["MigrationAssembly"], "EntityFramework.Design.Tests.dll");
                        Assert.Equal(configSourceMock.Object.Data["MigrationNamespace"], "MyNamespace");
                        Assert.Equal(configSourceMock.Object.Data["MigrationDirectory"], "MyDirectory");
                        Assert.Equal(configSourceMock.Object.Data["References"], "Ref1;Ref2;Ref3");
                    });

            toolMock.Protected()
                .Setup<IniFileConfigurationSource>("CreateIniFileConfigurationSource", ItExpr.IsAny<string>())
                .Callback<string>(AssertConfigFileCallback1)
                .Returns(configSourceMock.Object);

            configuration.AddCommandLine(args);

            tool.CommitConfiguration(configuration);

            configSourceMock.Verify(m => m.Commit(), Times.Once);
            toolMock.Protected().Verify<IniFileConfigurationSource>("CreateIniFileConfigurationSource", Times.Once(), ItExpr.IsAny<string>());
        }
Exemplo n.º 5
0
        public Startup()
        {
            var configuration = new Configuration()
                 .AddJsonFile("config.json");

            if (Program.Environment.OtherArgs != null)
            {
                configuration.AddCommandLine(Program.Environment.OtherArgs);
            }

            // Use the local omnisharp config if there's any in the root path
            if (File.Exists(Program.Environment.ConfigurationPath))
            {
                configuration.AddJsonFile(Program.Environment.ConfigurationPath);
            }

            configuration.AddEnvironmentVariables();

            Configuration = configuration;
        }
Exemplo n.º 6
0
        public Task<int> Main(string[] args)
        {
            //Add command line configuration source to read command line parameters.
            var config = new Configuration();
            config.AddCommandLine(args);

            var context = new HostingContext()
            {
                Configuration = config,
                ServerFactoryLocation = "Microsoft.AspNet.Server.WebListener",
                ApplicationName = "MusicStore"
            };

            using (new HostingEngine().Start(context))
            {
                Console.WriteLine("Started the server..");
                Console.WriteLine("Press any key to stop the server");
                Console.ReadLine();
            }
            return Task.FromResult(0);
        }
Exemplo n.º 7
0
        public void CommitConfiguration_with_default_config_file()
        {
            var toolMock = new Mock<MyMigrationTool>() { CallBase = true };
            var args
                = new[]
                      {
                          "--ContextAssembly=EntityFramework.Design.Tests.dll",
                      };
            var configSourceMock = new Mock<IniFileConfigurationSource>("Foo") { CallBase = true };
            var tool = toolMock.Object;
            var configuration = new Configuration();

            configSourceMock.Setup(m => m.Load());
            configSourceMock.Setup(m => m.Commit());

            toolMock.Protected()
                .Setup<IniFileConfigurationSource>("CreateIniFileConfigurationSource", ItExpr.IsAny<string>())
                .Callback<string>(AssertConfigFileCallback2)
                .Returns(configSourceMock.Object);

            configuration.AddCommandLine(args);

            tool.CommitConfiguration(configuration);

            toolMock.Protected().Verify<IniFileConfigurationSource>("CreateIniFileConfigurationSource", Times.Once(), ItExpr.IsAny<string>());
        }
Exemplo n.º 8
0
        public void LoadContext_throws_if_context_type_not_specified_and_multiple_DbContext_found_in_assembly()
        {
            var tool = new MyMigrationTool();
            var configuration = new Configuration();

            configuration.AddCommandLine(
                new[]
                    {
                        "--ContextAssembly=EntityFramework.Design.Tests.dll"
                    });

            Assert.Equal(
                Strings.FormatAssemblyContainsMultipleDbContext(Assembly.GetExecutingAssembly().FullName),
                Assert.Throws<InvalidOperationException>(() => tool.LoadContext(configuration)).Message);
        }
Exemplo n.º 9
0
        public void LoadContext_throws_if_context_type_not_specified_and_no_DbContext_found_in_assembly()
        {
            var toolMock = new Mock<MyMigrationTool> { CallBase = true };
            var tool = toolMock.Object;
            var configuration = new Configuration();

            toolMock.Setup(t => t.GetContextTypes(It.IsAny<Assembly>())).Returns(new Type[0]);

            configuration.AddCommandLine(
                new[]
                    {
                        "--ContextAssembly=EntityFramework.Design.Tests.dll"
                    });

            Assert.Equal(
                Strings.FormatAssemblyDoesNotContainDbContext(Assembly.GetExecutingAssembly().FullName),
                Assert.Throws<InvalidOperationException>(() => tool.LoadContext(configuration)).Message);
        }
Exemplo n.º 10
0
        public void CreateMigration_with_default_migration_directory()
        {
            var tool = new MyMigrationTool();
            var configuration = new Configuration();

            configuration.AddCommandLine(
                new[]
                    {
                        "--MigrationName=MyMigration", 
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext", 
                        "--MigrationAssembly=EntityFramework.Design.Tests.dll", 
                        "--MigrationNamespace=MyNamespace"
                    });

            var scaffoldedMigration = tool.CreateMigration(configuration);

            Assert.Equal("MyNamespace", scaffoldedMigration.MigrationNamespace);
            Assert.Equal("MyMigration", scaffoldedMigration.MigrationClass);
            Assert.Equal("MyContextModelSnapshot", scaffoldedMigration.SnapshotModelClass);
            Assert.True(scaffoldedMigration.MigrationFile.EndsWith("MyMigration.cs"));
            Assert.True(scaffoldedMigration.MigrationMetadataFile.EndsWith("MyMigration.Designer.cs"));
            Assert.True(scaffoldedMigration.SnapshotModelFile.EndsWith("MyContextModelSnapshot.cs"));
            Assert.True(Path.IsPathRooted(scaffoldedMigration.MigrationFile));
            Assert.True(Path.IsPathRooted(scaffoldedMigration.MigrationMetadataFile));
            Assert.True(Path.IsPathRooted(scaffoldedMigration.SnapshotModelFile));
        }
Exemplo n.º 11
0
        public void LoadContext_throws_if_context_type_not_found()
        {
            var tool = new MyMigrationTool();
            var configuration = new Configuration();

            configuration.AddCommandLine(
                new[]
                    {
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.Vuvuzelas"
                    });

            Assert.Equal(
                Strings.FormatAssemblyDoesNotContainType(
                    Assembly.GetExecutingAssembly().FullName,
                    "Microsoft.Data.Entity.Design.Tests.Vuvuzelas"),
                Assert.Throws<InvalidOperationException>(() => tool.LoadContext(configuration)).Message);
        }
Exemplo n.º 12
0
        public void LoadContext_loads_references_before_instantiating_context_type()
        {
            var toolMock = new Mock<MyMigrationTool> { CallBase = true };
            var tool = toolMock.Object;
            var configuration = new Configuration();

            toolMock.Protected()
                .Setup<DbContext>("CreateContext", ItExpr.IsAny<Type>())
                .Callback(() => toolMock.Protected()
                    .Verify<Assembly>("LoadAssembly", Times.Exactly(4), ItExpr.IsAny<string>()));

            configuration.AddCommandLine(
                new[]
                    {
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext",
                        "--References=Ref1;Ref2;Ref3"
                    });

            tool.LoadContext(configuration);
        }
Exemplo n.º 13
0
        public void UpdateDatabase_with_target_migration()
        {
            var toolMock = new Mock<MyMigrationTool> { CallBase = true };
            var migratorMock = new Mock<Migrator>();
            var tool = toolMock.Object;
            var configuration = new Configuration();

            toolMock.Protected()
                .Setup<Migrator>("GetMigrator", ItExpr.IsAny<DbContextConfiguration>())
                .Returns(migratorMock.Object);

            configuration.AddCommandLine(
                new[]
                    {
                        "--TargetMigration=MyMigrationName",
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext"
                    });

            tool.UpdateDatabase(configuration);

            migratorMock.Verify(m => m.UpdateDatabase("MyMigrationName"), Times.Once);
        }
Exemplo n.º 14
0
        public void GenerateScript_with_target_migration()
        {
            var toolMock = new Mock<MyMigrationTool> { CallBase = true };
            var migratorMock = new Mock<Migrator>();
            var tool = toolMock.Object;
            var configuration = new Configuration();

            migratorMock.Setup(m => m.GenerateUpdateDatabaseSql(It.IsAny<string>())).Returns(
                new[]
                    {
                        new SqlStatement("Script")
                    });

            toolMock.Protected()
                .Setup<Migrator>("GetMigrator", ItExpr.IsAny<DbContextConfiguration>())
                .Returns(migratorMock.Object);

            configuration.AddCommandLine(
                new[]
                    {
                        "--TargetMigration=MyMigrationName",
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext"
                    });

            var statements = tool.GenerateScript(configuration);

            migratorMock.Verify(m => m.GenerateUpdateDatabaseSql("MyMigrationName"), Times.Once);
            Assert.Equal(1, statements.Count);
            Assert.Equal("Script", statements[0].Sql);
        }
Exemplo n.º 15
0
        public void GetMigrations_throws_if_invalid_source()
        {
            var tool = new MyMigrationTool();
            var configuration = new Configuration();

            configuration.AddCommandLine(
                new[]
                    {
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext", 
                        "--MigrationSource=Foo"
                    });

            Assert.Equal(
                Strings.InvalidMigrationSource,
                Assert.Throws<InvalidOperationException>(() => tool.GetMigrations(configuration)).Message);
        }
Exemplo n.º 16
0
        public void GetMigrations_with_source_pending()
        {
            var toolMock = new Mock<MyMigrationTool> { CallBase = true };
            var migratorMock = new Mock<Migrator>();
            var tool = toolMock.Object;
            var configuration = new Configuration();

            migratorMock.Setup(m => m.GetPendingMigrations()).Returns(
                new[]
                    {
                        new MigrationMetadata("000000000000002_M2")
                    });

            toolMock.Protected()
                .Setup<Migrator>("GetMigrator", ItExpr.IsAny<DbContextConfiguration>())
                .Returns(migratorMock.Object);

            configuration.AddCommandLine(
                new[]
                    {
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext", 
                        "--MigrationSource=Pending"
                    });

            var migrations = tool.GetMigrations(configuration);

            Assert.Equal(1, migrations.Count);
            Assert.Equal("000000000000002_M2", migrations[0].MigrationId);
        }
Exemplo n.º 17
0
        public void LoadContext_throws_if_context_type_is_not_DbContext()
        {
            var tool = new MyMigrationTool();
            var configuration = new Configuration();

            configuration.AddCommandLine(
                new[]
                    {
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+NotAContext"
                    });

            Assert.Equal(
                Strings.FormatTypeIsNotDbContext("Microsoft.Data.Entity.Design.Tests.MigrationToolTest+NotAContext"),
                Assert.Throws<InvalidOperationException>(() => tool.LoadContext(configuration)).Message);
        }
Exemplo n.º 18
0
        public void CreateMigration()
        {
            var tool = new MyMigrationTool();
            var configuration = new Configuration();

            configuration.AddCommandLine(
                new[]
                    {
                        "--MigrationName=MyMigration", 
                        "--ContextAssembly=EntityFramework.Design.Tests.dll", 
                        "--ContextType=Microsoft.Data.Entity.Design.Tests.MigrationToolTest+MyContext", 
                        "--MigrationAssembly=EntityFramework.Design.Tests.dll", 
                        "--MigrationNamespace=MyNamespace", 
                        "--MigrationDirectory=C:\\MyDirectory"
                    });

            var scaffoldedMigration = tool.CreateMigration(configuration);

            Assert.Equal("MyNamespace", scaffoldedMigration.MigrationNamespace);
            Assert.Equal("MyMigration", scaffoldedMigration.MigrationClass);
            Assert.Equal("MyContextModelSnapshot", scaffoldedMigration.SnapshotModelClass);
            Assert.Equal("C:\\MyDirectory\\MyMigration.cs", scaffoldedMigration.MigrationFile);
            Assert.Equal("C:\\MyDirectory\\MyMigration.Designer.cs", scaffoldedMigration.MigrationMetadataFile);
            Assert.Equal("C:\\MyDirectory\\MyContextModelSnapshot.cs", scaffoldedMigration.SnapshotModelFile);
        }