Beispiel #1
0
        private static HostStub RunWebRole(WebHostArgs webHostArgs)
        {
            var appDomainSetup = new AppDomainSetup
            {
                ApplicationBase   = webHostArgs.SiteBinDirectory,
                ConfigurationFile = DetermineWebConfigPath(webHostArgs)
            };

            StubManagement.CopyStubAssemblyToRoleDirectory(webHostArgs.SiteBinDirectory);

            var appDomain = AppDomain.CreateDomain(
                "LightBlue",
                null,
                appDomainSetup);

            Trace.Listeners.Add(new ConsoleTraceListener());

            var stub = (HostStub)appDomain.CreateInstanceAndUnwrap(
                typeof(HostStub).Assembly.FullName,
                typeof(HostStub).FullName);

            stub.ConfigureTracing(new ConsoleTraceShipper());

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionBehaviour.UnhandledExceptionHandler(webHostArgs.Title);

            return(stub);
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            var webHostArgs = WebHostArgs.ParseArgs(args);

            if (webHostArgs == null)
            {
                return;
            }

            try
            {
                var handle = Process.GetCurrentProcess().MainWindowHandle;

                SetWindowText(handle, webHostArgs.Title);

                ConfigureWebConfig(webHostArgs);

                RunWebSite(webHostArgs);
                RunWebRole(webHostArgs);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToTraceMessage());
            }

            if (!webHostArgs.AllowSilentFail)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "The web host {0} has exited unexpectedly",
                              webHostArgs.Title));
            }
        }
Beispiel #3
0
        private static void ConfigureWebConfig(WebHostArgs webHostArgs)
        {
            var webConfigFilePath = DetermineWebConfigPath(webHostArgs);

            if (!File.Exists(webConfigFilePath))
            {
                throw new ArgumentException("No web.config could be located for the site");
            }
        }
Beispiel #4
0
        private static void ConfigureWebConfig(WebHostArgs webHostArgs)
        {
            var webConfigFilePath = DetermineWebConfigPath(webHostArgs);

            if (!File.Exists(webConfigFilePath))
            {
                throw new ArgumentException("No web.config could be located for the site");
            }

            ConfigurationManipulation.RemoveAzureTraceListenerFromConfiguration(webConfigFilePath);
        }
Beispiel #5
0
        private static void GenerateIisExpressConfigurationFile(WebHostArgs webHostArgs, string configurationFilePath)
        {
            var template = ObtainIisExpressConfigurationTemplate(webHostArgs);

            template = template.Replace("__SITEPATH__", webHostArgs.SiteDirectory);
            template = template.Replace("__PROTOCOL__", webHostArgs.UseSsl ? "https" : "http");
            template = template.Replace("__PORT__", webHostArgs.Port.ToString(CultureInfo.InvariantCulture));
            template = template.Replace("__HOSTNAME__", webHostArgs.Hostname);

            File.WriteAllText(configurationFilePath, template);
        }
Beispiel #6
0
        private static string ObtainIisExpressConfigurationTemplate(WebHostArgs webHostArgs)
        {
            if (!string.IsNullOrWhiteSpace(webHostArgs.IisExpressTemplate))
            {
                return(File.ReadAllText(webHostArgs.IisExpressTemplate));
            }

            var executingAssembly      = Assembly.GetExecutingAssembly();
            var manifestResourceStream = executingAssembly.GetManifestResourceStream("LightBlue.WebHost.IISExpressConfiguration.template");

            if (manifestResourceStream == null)
            {
                throw new InvalidOperationException("Unable to retrieve IIS Express configuration template.");
            }

            var template = new StreamReader(manifestResourceStream).ReadToEnd();

            return(template);
        }
Beispiel #7
0
        private static ProcessStartInfo BuildProcessStartInfo(WebHostArgs webHostArgs, string configurationFilePath)
        {
            string path = webHostArgs.Use64Bit
                ? Path.Combine(Environment.GetEnvironmentVariable("ProgramW6432"), @"IIS Express\iisexpress.exe")
                : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"IIS Express\iisexpress.exe");

            var processStartInfo = new ProcessStartInfo
            {
                FileName  = path,
                Arguments = string.Format(
                    CultureInfo.InvariantCulture,
                    "/config:\"{0}\" /site:LightBlue",
                    configurationFilePath),
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                UseShellExecute        = false,
                CreateNoWindow         = true
            };

            processStartInfo.EnvironmentVariables.Add("LightBlueHost", "true");
            processStartInfo.EnvironmentVariables.Add("LightBlueConfigurationPath", webHostArgs.ConfigurationPath);
            processStartInfo.EnvironmentVariables.Add("LightBlueServiceDefinitionPath", webHostArgs.ServiceDefinitionPath);
            processStartInfo.EnvironmentVariables.Add("LightBlueRoleName", webHostArgs.RoleName);
            processStartInfo.EnvironmentVariables.Add("LightBlueUseHostedStorage", webHostArgs.UseHostedStorage.ToString());

            var processId = webHostArgs.RoleName
                            + "-web-"
                            + Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture);

            var temporaryDirectory = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                "LightBlue",
                "temp",
                processId);

            Directory.CreateDirectory(temporaryDirectory);
            processStartInfo.EnvironmentVariables["TMP"]  = temporaryDirectory;
            processStartInfo.EnvironmentVariables["TEMP"] = temporaryDirectory;

            return(processStartInfo);
        }
Beispiel #8
0
        private static ProcessStartInfo BuildProcessStartInfo(WebHostArgs webHostArgs, string configurationFilePath)
        {
            string path = webHostArgs.Use64Bit
                ? Path.Combine(Environment.GetEnvironmentVariable("ProgramW6432"), @"IIS Express\iisexpress.exe")
                : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),@"IIS Express\iisexpress.exe");

            var processStartInfo = new ProcessStartInfo
            {
                FileName = path,
                Arguments = string.Format(
                    CultureInfo.InvariantCulture,
                    "/config:\"{0}\" /site:LightBlue",
                    configurationFilePath),
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };

            processStartInfo.EnvironmentVariables.Add("LightBlueHost", "true");
            processStartInfo.EnvironmentVariables.Add("LightBlueConfigurationPath", webHostArgs.ConfigurationPath);
            processStartInfo.EnvironmentVariables.Add("LightBlueServiceDefinitionPath", webHostArgs.ServiceDefinitionPath);
            processStartInfo.EnvironmentVariables.Add("LightBlueRoleName", webHostArgs.RoleName);
            processStartInfo.EnvironmentVariables.Add("LightBlueUseHostedStorage", webHostArgs.UseHostedStorage.ToString());

            var processId = webHostArgs.RoleName
                + "-web-"
                + Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture);

            var temporaryDirectory = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                "LightBlue",
                "temp",
                processId);

            Directory.CreateDirectory(temporaryDirectory);
            processStartInfo.EnvironmentVariables["TMP"] = temporaryDirectory;
            processStartInfo.EnvironmentVariables["TEMP"] = temporaryDirectory;

            return processStartInfo;
        }
Beispiel #9
0
        private static void RunWebSite(WebHostArgs webHostArgs)
        {
            var iisExpressConfigurationFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".config");

            GenerateIisExpressConfigurationFile(webHostArgs, iisExpressConfigurationFilePath);

            using (var process = Process.Start(BuildProcessStartInfo(webHostArgs, iisExpressConfigurationFilePath)))
            {
                if (process == null)
                {
                    Console.WriteLine("Existing process used.");
                    return;
                }

                process.WaitForExit(2000);

                if (process.HasExited)
                {
                    Console.WriteLine(process.StandardError.ReadToEnd());
                }
            }
        }
Beispiel #10
0
        public static void Main(string[] args)
        {
            var webHostArgs = WebHostArgs.ParseArgs(args);

            if (webHostArgs == null)
            {
                return;
            }

            try
            {
                var handle = Process.GetCurrentProcess().MainWindowHandle;

                SetWindowText(handle, webHostArgs.Title);

                var host = WebHostFactory.Create(webHostArgs);

                host.Run(webHostArgs.Assembly,
                         webHostArgs.ConfigurationPath,
                         webHostArgs.ServiceDefinitionPath,
                         webHostArgs.RoleName,
                         webHostArgs.UseHostedStorage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToTraceMessage());
            }

            if (!webHostArgs.AllowSilentFail)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "The web host {0} has exited unexpectedly",
                              webHostArgs.Title));
            }
        }
Beispiel #11
0
 private static string DetermineWebConfigPath(WebHostArgs webHostArgs)
 {
     return(Path.Combine(webHostArgs.SiteDirectory, "web.config"));
 }
Beispiel #12
0
        private static void GenerateIisExpressConfigurationFile(WebHostArgs webHostArgs, string configurationFilePath)
        {
            var template = ObtainIisExpressConfigurationTemplate(webHostArgs);

            template = template.Replace("__SITEPATH__", webHostArgs.SiteDirectory);
            template = template.Replace("__PROTOCOL__", webHostArgs.UseSsl ? "https" : "http");
            template = template.Replace("__PORT__", webHostArgs.Port.ToString(CultureInfo.InvariantCulture));
            template = template.Replace("__HOSTNAME__", webHostArgs.Hostname);

            File.WriteAllText(configurationFilePath, template);
        }
Beispiel #13
0
        private static string ObtainIisExpressConfigurationTemplate(WebHostArgs webHostArgs)
        {
            if (!string.IsNullOrWhiteSpace(webHostArgs.IisExpressTemplate))
            {
                return File.ReadAllText(webHostArgs.IisExpressTemplate);
            }

            var executingAssembly = Assembly.GetExecutingAssembly();
            var manifestResourceStream = executingAssembly.GetManifestResourceStream("LightBlue.WebHost.IISExpressConfiguration.template");
            if (manifestResourceStream == null)
            {
                throw new InvalidOperationException("Unable to retrieve IIS Express configuration template.");
            }

            var template = new StreamReader(manifestResourceStream).ReadToEnd();
            return template;
        }
Beispiel #14
0
 public static HostStub Create(WebHostArgs webHostArgs)
 {
     ConfigureWebConfig(webHostArgs);
     RunWebSite(webHostArgs);
     return(RunWebRole(webHostArgs));
 }
Beispiel #15
0
 private static string DetermineWebConfigPath(WebHostArgs webHostArgs)
 {
     return Path.Combine(webHostArgs.SiteDirectory, "web.config");
 }
Beispiel #16
0
        private static void ConfigureWebConfig(WebHostArgs webHostArgs)
        {
            var webConfigFilePath = DetermineWebConfigPath(webHostArgs);
            if (!File.Exists(webConfigFilePath))
            {
                throw new ArgumentException("No web.config could be located for the site");
            }

            ConfigurationManipulation.RemoveAzureTraceListenerFromConfiguration(webConfigFilePath);
        }
Beispiel #17
0
        private static void RunWebSite(WebHostArgs webHostArgs)
        {
            var iisExpressConfigurationFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".config");
            GenerateIisExpressConfigurationFile(webHostArgs, iisExpressConfigurationFilePath);

            using (var process = Process.Start(BuildProcessStartInfo(webHostArgs, iisExpressConfigurationFilePath)))
            {
                if (process == null)
                {
                    Console.WriteLine("Existing process used.");
                    return;
                }

                process.WaitForExit(2000);

                if (process.HasExited)
                {
                    Console.WriteLine(process.StandardError.ReadToEnd());
                }
            }
        }
Beispiel #18
0
        private static void RunWebRole(WebHostArgs webHostArgs)
        {
            var appDomainSetup = new AppDomainSetup
            {
                ApplicationBase = webHostArgs.SiteBinDirectory,
                ConfigurationFile = DetermineWebConfigPath(webHostArgs)
            };

            StubManagement.CopyStubAssemblyToRoleDirectory(webHostArgs.SiteBinDirectory);

            var appDomain = AppDomain.CreateDomain(
                "LightBlue",
                null,
                appDomainSetup);

            Trace.Listeners.Add(new ConsoleTraceListener());

            var stub = (HostStub) appDomain.CreateInstanceAndUnwrap(
                typeof(HostStub).Assembly.FullName,
                typeof(HostStub).FullName);

            stub.ConfigureTracing(new ConsoleTraceShipper());

            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionBehaviour.UnhandledExceptionHandler(webHostArgs.Title);

            stub.Run(workerRoleAssembly: webHostArgs.Assembly,
                configurationPath: webHostArgs.ConfigurationPath,
                serviceDefinitionPath: webHostArgs.ServiceDefinitionPath,
                roleName: webHostArgs.RoleName,
                useHostedStorage: webHostArgs.UseHostedStorage);
        }