Exemple #1
0
        private static Type GetEndpointConfigurationType(HostArguments arguments)
        {
            if (arguments.EndpointConfigurationType != null)
            {
                string t = arguments.EndpointConfigurationType.Value;
                if (t != null)
                {
                    Type endpointType = Type.GetType(t, false);
                    if (endpointType == null)
                        throw new ConfigurationErrorsException(string.Format("Command line argument 'endpointConfigurationType' has specified to use the type '{0}' but that type could not be loaded.", t));

                    return endpointType;
                }
            }

            string endpoint = ConfigurationManager.AppSettings["EndpointConfigurationType"];
            if (endpoint != null)
            {
                var endpointType = Type.GetType(endpoint, false);
                if (endpointType == null)
                    throw new ConfigurationErrorsException(string.Format("The 'EndpointConfigurationType' entry in the NServiceBus.Host.exe.config has specified to use the type '{0}' but that type could not be loaded.", endpoint));

                return endpointType;
            }

            IEnumerable<Type> endpoints = ScanAssembliesForEndpoints();

            ValidateEndpoints(endpoints);

            return endpoints.First();
        }
        static void Main(string[] args)
        {
            var commandLineArguments = Parser.ParseArgs(args);
            var arguments = new HostArguments(commandLineArguments);

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

                return;
            }

            var endpointConfigurationType = GetEndpointConfigurationType(arguments);

            AssertThatEndpointConfigurationTypeHasDefaultConstructor(endpointConfigurationType);

            var endpointConfigurationFile = GetEndpointConfigurationFile(endpointConfigurationType);

            var endpointConfiguration = Activator.CreateInstance(endpointConfigurationType);

            EndpointId = GetEndpointId(endpointConfiguration);

            scannedAssemblies = scannedAssemblies ?? new List<Assembly>();
            var assemblylist = string.Join(";", scannedAssemblies.Select((s => s.ToString())));
            args = args.Concat(new[]{String.Format(@"/scannedAssemblies={0}", assemblylist)}).ToArray();

            AppDomain.CurrentDomain.SetupInformation.AppDomainInitializerArguments = args;

            var 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 : EndpointId);
                x.SetServiceName(arguments.ServiceName != null ? arguments.ServiceName.Value : EndpointId);
                x.SetDescription(arguments.Description != null ? arguments.Description.Value : "NServiceBus Message Endpoint Host Service");
                x.DependencyOnMsmq();

                var serviceCommandLine = commandLineArguments.CustomArguments.AsCommandLine();

                if (arguments.ServiceName != null)
                {
                    serviceCommandLine += " /serviceName:\"" + arguments.ServiceName.Value + "\"";
                }

                x.SetServiceCommandLine(serviceCommandLine);

                if (arguments.DependsOn != null)
                {
                    var dependencies = arguments.DependsOn.Value.Split(',');

                    foreach (var dependency in dependencies)
                    {
                        if (dependency.ToUpper() == KnownServiceNames.Msmq)
                        {
                            continue;
                        }

                        x.DependsOn(dependency);
                    }
                }
            });

            Runner.Host(cfg, args);
        }
Exemple #3
0
        private static void Main(string[] args)
        {
            var commandLineArguments = Parser.ParseArgs(args);
            var arguments            = new HostArguments(commandLineArguments);

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

                return;
            }

            var endpointConfigurationType = GetEndpointConfigurationType(arguments);

            AssertThatEndpointConfigurationTypeHasDefaultConstructor(endpointConfigurationType);

            var endpointConfigurationFile = GetEndpointConfigurationFile(endpointConfigurationType);

            var endpointConfiguration = Activator.CreateInstance(endpointConfigurationType);

            EndpointId = GetEndpointId(endpointConfiguration);

            AppDomain.CurrentDomain.SetupInformation.AppDomainInitializerArguments = args;

            var 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 : EndpointId);
                x.SetServiceName(arguments.ServiceName != null ? arguments.ServiceName.Value : EndpointId);
                x.SetDescription(arguments.Description != null ? arguments.Description.Value : "NServiceBus Message Endpoint Host Service");
                x.DependencyOnMsmq();

                var serviceCommandLine = commandLineArguments.CustomArguments.AsCommandLine();

                if (arguments.ServiceName != null)
                {
                    serviceCommandLine += " /serviceName:\"" + arguments.ServiceName.Value + "\"";
                }

                x.SetServiceCommandLine(serviceCommandLine);

                if (arguments.DependsOn != null)
                {
                    var dependencies = arguments.DependsOn.Value.Split(',');

                    foreach (var dependency in dependencies)
                    {
                        if (dependency.ToUpper() == KnownServiceNames.Msmq)
                        {
                            continue;
                        }

                        x.DependsOn(dependency);
                    }
                }
            });

            Runner.Host(cfg, args);
        }