Exemplo n.º 1
0
        public void EnvDefaultsToDevelopmentConfigValueIfSpecified()
        {
            var vals = new Dictionary<string, string>
            {
                { "ASPNET_ENV", "Staging" }
            };

            var config = new Configuration()
                .Add(new MemoryConfigurationSource(vals));

            var context = new HostingContext
            {
                ServerFactory = this,
                Configuration = config
            };

            var engine = new HostingEngine();

            using (engine.Start(context))
            {
                Assert.Equal("Staging", context.EnvironmentName);
                var env = context.ApplicationServices.GetRequiredService<IHostingEnvironment>();
                Assert.Equal("Staging", env.EnvironmentName);
            }
        }
Exemplo n.º 2
0
        public void ApplicationNameDefaultsToApplicationEnvironmentName()
        {
            var context = new HostingContext
            {
                ServerFactory = this
            };

            var engine = new HostingEngine();

            using (engine.Start(context))
            {
                Assert.Equal("Microsoft.AspNet.Hosting.Tests", context.ApplicationName);
            }
        }
Exemplo n.º 3
0
        public void EnvDefaultsToDevelopmentIfNoConfig()
        {
            var context = new HostingContext
            {
                ServerFactory = this
            };

            var engine = new HostingEngine();

            using (engine.Start(context))
            {
                Assert.Equal("Development", context.EnvironmentName);
                var env = context.ApplicationServices.GetRequiredService<IHostingEnvironment>();
                Assert.Equal("Development", env.EnvironmentName);
            }
        }
Exemplo n.º 4
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.º 5
0
        public void Main(string[] args)
        {
            var applicationRoot = Directory.GetCurrentDirectory();
            var serverPort = 2000;
            var logLevel = LogLevel.Information;
            var hostPID = -1;
            var transportType = TransportType.Http;
            var otherArgs = new List<string>();

            var enumerator = args.GetEnumerator();

            while (enumerator.MoveNext())
            {
                var arg = (string)enumerator.Current;
                if (arg == "-s")
                {
                    enumerator.MoveNext();
                    applicationRoot = Path.GetFullPath((string)enumerator.Current);
                }
                else if (arg == "-p")
                {
                    enumerator.MoveNext();
                    serverPort = int.Parse((string)enumerator.Current);
                }
                else if (arg == "-v")
                {
                    logLevel = LogLevel.Verbose;
                }
                else if (arg == "--hostPID")
                {
                    enumerator.MoveNext();
                    hostPID = int.Parse((string)enumerator.Current);
                }
                else if (arg == "--stdio")
                {
                    transportType = TransportType.Stdio;
                }
                else
                {
                    otherArgs.Add((string)enumerator.Current);
                }
            }

            Environment = new OmnisharpEnvironment(applicationRoot, serverPort, hostPID, logLevel, transportType, otherArgs.ToArray());

            var config = new Configuration()
             .AddCommandLine(new[] { "--server.urls", "http://localhost:" + serverPort });

            var engine = new HostingEngine(_serviceProvider);

            var context = new HostingContext()
            {
                ServerFactoryLocation = "Kestrel",
                Configuration = config,
            };

            var writer = new SharedConsoleWriter();
            context.Services.AddInstance<IOmnisharpEnvironment>(Environment);
            context.Services.AddInstance<ISharedTextWriter>(writer);

            if (transportType == TransportType.Stdio)
            {
                context.Server = null;
                context.ServerFactory = new Stdio.StdioServerFactory(Console.In, writer);
            }

            var serverShutdown = engine.Start(context);

            var appShutdownService = _serviceProvider.GetRequiredService<IApplicationShutdown>();
            var shutdownHandle = new ManualResetEvent(false);

            appShutdownService.ShutdownRequested.Register(() =>
            {
                serverShutdown.Dispose();
                shutdownHandle.Set();
            });

#if DNXCORE50
            var ignored = Task.Run(() =>
            {
                Console.WriteLine("Started");
                Console.ReadLine();
                appShutdownService.RequestShutdown();
            });
#else
            Console.CancelKeyPress += (sender, e) =>
            {
                appShutdownService.RequestShutdown();
            };
#endif

            if (hostPID != -1)
            {
                try
                {
                    var hostProcess = Process.GetProcessById(hostPID);
                    hostProcess.EnableRaisingEvents = true;
                    hostProcess.OnExit(() => appShutdownService.RequestShutdown());
                }
                catch
                {
                    // If the process dies before we get here then request shutdown
                    // immediately
                    appShutdownService.RequestShutdown();
                }
            }

            shutdownHandle.WaitOne();
        }
Exemplo n.º 6
0
        public void MapPath_Facts(string virtualPath, string expectedSuffix)
        {
            var context = new HostingContext
            {
                ServerFactory = this
            };

            var engine = new HostingEngine();

            using (engine.Start(context))
            {
                var env = context.ApplicationServices.GetRequiredService<IHostingEnvironment>();
                var mappedPath = env.MapPath(virtualPath);
                expectedSuffix = expectedSuffix.Replace('/', Path.DirectorySeparatorChar);
                Assert.Equal(Path.Combine(env.WebRootPath, expectedSuffix), mappedPath);
            }
        }
Exemplo n.º 7
0
        public void IsEnvironment_Extension_Is_Case_Insensitive()
        {
            var context = new HostingContext
            {
                ServerFactory = this
            };

            var engine = new HostingEngine();

            using (engine.Start(context))
            {
                var env = context.ApplicationServices.GetRequiredService<IHostingEnvironment>();
                Assert.True(env.IsEnvironment("Development"));
                Assert.True(env.IsEnvironment("developMent"));
            }
        }