Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Parser.Args commandLineArguments = Parser.ParseArgs(args);
            var         arguments            = new HostArguments(commandLineArguments);

            if (arguments.Help != null)
            {
                DisplayHelpContent();

                return;
            }

            var endpointConfigurationType = GetEndpointConfigurationType(arguments);

            if (endpointConfigurationType == null)
            {
                if (arguments.InstallInfrastructure == null)
                {
                    throw new InvalidOperationException("No endpoint configuration found in scanned assemblies. " +
                                                        "This usually happens when NServiceBus fails to load your assembly containing IConfigureThisEndpoint." +
                                                        " Try specifying the type explicitly in the NServiceBus.Host.exe.config using the appsetting key: EndpointConfigurationType, " +
                                                        "Scanned path: " + AppDomain.CurrentDomain.BaseDirectory);
                }

                Console.WriteLine("Running infrastructure installers and exiting (ignoring other command line parameters if exist).");
                InstallInfrastructure();
                return;
            }

            AssertThatEndpointConfigurationTypeHasDefaultConstructor(endpointConfigurationType);
            string endpointConfigurationFile = GetEndpointConfigurationFile(endpointConfigurationType);

            var endpointName    = GetEndpointName(endpointConfigurationType, arguments);
            var endpointVersion = GetEndpointVersion(endpointConfigurationType);

            var serviceName = endpointName;

            if (arguments.ServiceName != null)
            {
                serviceName = arguments.ServiceName.Value;
            }

            var displayName = serviceName + "-" + endpointVersion;

            if (arguments.SideBySide != null)
            {
                serviceName += "-" + endpointVersion;

                displayName += " (SideBySide)";
            }

            //add the endpoint name so that the new appdomain can get it
            args = args.Concat(new[] { endpointName }).ToArray();

            AppDomain.CurrentDomain.SetupInformation.AppDomainInitializerArguments = args;
            if ((commandLineArguments.Install) || (arguments.InstallInfrastructure != null))
            {
                WindowsInstaller.Install(args, endpointConfigurationType, endpointName, endpointConfigurationFile,
                                         commandLineArguments.Install, arguments.InstallInfrastructure != null);
            }


            IRunConfiguration cfg = RunnerConfigurator.New(x =>
            {
                x.ConfigureServiceInIsolation <WindowsHost>(endpointConfigurationType.AssemblyQualifiedName, c =>
                {
                    c.ConfigurationFile(endpointConfigurationFile);
                    c.CommandLineArguments(args, () => SetHostServiceLocatorArgs);
                    c.WhenStarted(service => service.Start());
                    c.WhenStopped(service => service.Stop());
                    c.CreateServiceLocator(() => new HostServiceLocator());
                });

                if (arguments.Username != null && arguments.Password != null)
                {
                    x.RunAs(arguments.Username.Value, arguments.Password.Value);
                }
                else
                {
                    x.RunAsLocalSystem();
                }

                if (arguments.StartManually != null)
                {
                    x.DoNotStartAutomatically();
                }

                x.SetDisplayName(arguments.DisplayName != null ? arguments.DisplayName.Value : displayName);
                x.SetServiceName(serviceName);
                x.SetDescription(arguments.Description != null ? arguments.Description.Value : "NServiceBus Message Endpoint Host Service for " + displayName);

                var serviceCommandLine = commandLineArguments.CustomArguments.AsCommandLine();
                serviceCommandLine    += " /serviceName:\"" + serviceName + "\"";
                serviceCommandLine    += " /endpointName:\"" + endpointName + "\"";

                x.SetServiceCommandLine(serviceCommandLine);

                if (arguments.DependsOn == null)
                {
                    x.DependencyOnMsmq();
                }
                else
                {
                    foreach (var dependency in arguments.DependsOn.Value.Split(','))
                    {
                        x.DependsOn(dependency);
                    }
                }
            });

            Runner.Host(cfg, args);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var arguments = new HostArguments(args);

            if (arguments.Help)
            {
                arguments.PrintUsage();
                return;
            }

            assemblyScannerResults = AssemblyScanner.GetScannableAssemblies();

            var endpointTypeDeterminer    = new EndpointTypeDeterminer(assemblyScannerResults, () => ConfigurationManager.AppSettings["EndpointConfigurationType"]);
            var endpointConfigurationType = endpointTypeDeterminer.GetEndpointConfigurationTypeForHostedEndpoint(arguments);

            var endpointConfigurationFile = endpointConfigurationType.EndpointConfigurationFile;
            var endpointName    = endpointConfigurationType.EndpointName;
            var serviceName     = endpointConfigurationType.ServiceName;
            var endpointVersion = endpointConfigurationType.EndpointVersion;
            var displayName     = serviceName + "-" + endpointVersion;

            if (arguments.SideBySide)
            {
                serviceName += "-" + endpointVersion;
            }

            //Add the endpoint name so that the new appdomain can get it
            if (arguments.EndpointName == null && !String.IsNullOrEmpty(endpointName))
            {
                args = args.Concat(new[] { String.Format(@"/endpointName={0}", endpointName) }).ToArray();
            }

            //Add the ScannedAssemblies name so that the new appdomain can get it
            if (arguments.ScannedAssemblies.Count == 0)
            {
                args = assemblyScannerResults.Assemblies.Select(s => s.ToString()).Aggregate(args, (current, result) => current.Concat(new[] { String.Format(@"/scannedAssemblies={0}", result) }).ToArray());
            }

            //Add the endpointConfigurationType name so that the new appdomain can get it
            if (arguments.EndpointConfigurationType == null)
            {
                args = args.Concat(new[] { String.Format(@"/endpointConfigurationType={0}", endpointConfigurationType.AssemblyQualifiedName) }).ToArray();
            }

            if (arguments.Install)
            {
                WindowsInstaller.Install(args, endpointConfigurationFile);
            }

            IRunConfiguration cfg = RunnerConfigurator.New(x =>
            {
                x.ConfigureServiceInIsolation <WindowsHost>(endpointConfigurationType.AssemblyQualifiedName, c =>
                {
                    c.ConfigurationFile(endpointConfigurationFile);
                    c.WhenStarted(service => service.Start());
                    c.WhenStopped(service => service.Stop());
                    c.CommandLineArguments(args, () => SetHostServiceLocatorArgs);
                    c.CreateServiceLocator(() => new HostServiceLocator());
                });

                if (arguments.Username != null && arguments.Password != null)
                {
                    x.RunAs(arguments.Username, arguments.Password);
                }
                else
                {
                    x.RunAsLocalSystem();
                }

                if (arguments.StartManually)
                {
                    x.DoNotStartAutomatically();
                }

                x.SetDisplayName(arguments.DisplayName ?? displayName);
                x.SetServiceName(serviceName);
                x.SetDescription(arguments.Description ?? string.Format("NServiceBus Endpoint Host Service for {0}", displayName));

                var serviceCommandLine = new List <string>();

                if (!String.IsNullOrEmpty(arguments.EndpointConfigurationType))
                {
                    serviceCommandLine.Add(String.Format(@"/endpointConfigurationType:""{0}""", arguments.EndpointConfigurationType));
                }

                if (!String.IsNullOrEmpty(endpointName))
                {
                    serviceCommandLine.Add(String.Format(@"/endpointName:""{0}""", endpointName));
                }

                if (!String.IsNullOrEmpty(serviceName))
                {
                    serviceCommandLine.Add(String.Format(@"/serviceName:""{0}""", serviceName));
                }

                if (arguments.ScannedAssemblies.Count > 0)
                {
                    serviceCommandLine.AddRange(arguments.ScannedAssemblies.Select(assembly => String.Format(@"/scannedAssemblies:""{0}""", assembly)));
                }

                if (arguments.OtherArgs.Any())
                {
                    serviceCommandLine.AddRange(arguments.OtherArgs);
                }

                var commandLine = String.Join(" ", serviceCommandLine);
                x.SetServiceCommandLine(commandLine);

                if (arguments.DependsOn == null)
                {
                    x.DependencyOnMsmq();
                }
                else
                {
                    foreach (var dependency in arguments.DependsOn)
                    {
                        x.DependsOn(dependency);
                    }
                }
            });

            try
            {
                Runner.Host(cfg, args);
            }
            catch (StateMachineException exception)
            {
                var innerException = exception.InnerException;
                innerException.PreserveStackTrace();
                throw innerException;
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Parser.Args commandLineArguments = Parser.ParseArgs(args);
            var         arguments            = new HostArguments(commandLineArguments);

            if (arguments.Help != null)
            {
                DisplayHelpContent();

                return;
            }

            var endpointConfigurationType = GetEndpointConfigurationType(arguments);

            AssertThatEndpointConfigurationTypeHasDefaultConstructor(endpointConfigurationType);

            string endpointConfigurationFile = GetEndpointConfigurationFile(endpointConfigurationType);


            var endpointName    = GetEndpointName(endpointConfigurationType);
            var endpointVersion = GetEndpointVersion(endpointConfigurationType);

            if (arguments.ServiceName != null)
            {
                endpointName = arguments.ServiceName.Value;
            }

            var serviceName = endpointName;

            var displayName = serviceName + "-" + endpointVersion;

            //add the endpoint name so that the new appdomain can get it
            args = args.Concat(new[] { endpointName }).ToArray();

            AppDomain.CurrentDomain.SetupInformation.AppDomainInitializerArguments = args;

            if (commandLineArguments.Install)
            {
                WindowsInstaller.Install(args, endpointConfigurationType, endpointName, endpointConfigurationFile);
            }

            IRunConfiguration cfg = RunnerConfigurator.New(x =>
            {
                x.ConfigureServiceInIsolation <WindowsHost>(endpointConfigurationType.AssemblyQualifiedName, c =>
                {
                    c.ConfigurationFile(endpointConfigurationFile);
                    c.CommandLineArguments(args, () => SetHostServiceLocatorArgs);
                    c.WhenStarted(service => service.Start());
                    c.WhenStopped(service => service.Stop());
                    c.CreateServiceLocator(() => new HostServiceLocator());
                });

                if (arguments.Username != null && arguments.Password != null)
                {
                    x.RunAs(arguments.Username.Value, arguments.Password.Value);
                }
                else
                {
                    x.RunAsLocalSystem();
                }

                if (arguments.StartManually != null)
                {
                    x.DoNotStartAutomatically();
                }

                x.SetDisplayName(arguments.DisplayName != null ? arguments.DisplayName.Value : displayName);
                x.SetServiceName(serviceName);
                x.SetDescription(arguments.Description != null ? arguments.Description.Value : "NServiceBus Message Endpoint Host Service for " + displayName);

                var serviceCommandLine = commandLineArguments.CustomArguments.AsCommandLine();
                serviceCommandLine    += " /serviceName:\"" + serviceName + "\"";

                x.SetServiceCommandLine(serviceCommandLine);

                if (arguments.DependsOn == null)
                {
                    x.DependencyOnMsmq();
                }
                else
                {
                    foreach (var dependency in arguments.DependsOn.Value.Split(','))
                    {
                        x.DependsOn(dependency);
                    }
                }
            });

            Runner.Host(cfg, args);
        }