Beispiel #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();
        }
Beispiel #2
0
        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();
            if ((endpoints.Count() == 0))
            {
                Console.Out.WriteLine(assemblyScannerResults);
                return null;
            }

            AssertThatNotMoreThanOneEndpointIsDefined(endpoints);
            return endpoints.First();
        }
        public void when_invalid_endpoint_type_is_provided_via_hostArgs_it_should_blow_up()
        {
            EndpointTypeDeterminer = new EndpointTypeDeterminer(AssemblyScanner,
                () => ConfigurationManager.AppSettings["EndpointConfigurationType"]);
            hostArguments = new HostArguments(new string[0])
            {
                EndpointConfigurationType = "I am an invalid type name"
            };

            RetrievedEndpointType = EndpointTypeDeterminer.GetEndpointConfigurationTypeForHostedEndpoint(hostArguments).Type;
        }
        public void when_endpoint_type_is_not_provided_via_hostArgs_it_should_fall_through_to_other_modes_of_determining_endpoint_type()
        {
            EndpointTypeDeterminer = new EndpointTypeDeterminer(AssemblyScanner,
                () => ConfigurationManager.AppSettings["EndpointConfigurationType"]);
            hostArguments = new HostArguments(new string[0]);

            // will match with config-based type
            RetrievedEndpointType = EndpointTypeDeterminer.GetEndpointConfigurationTypeForHostedEndpoint(hostArguments).Type;

            Assert.AreEqual(EndpointTypeDefinedInConfigurationFile, RetrievedEndpointType);
        }
        /// <summary>
        /// Returns an instance of <see cref="GenericHost"/>
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        protected override object DoGetInstance(Type serviceType, string key)
        {
            var endpoint = Type.GetType(key,true);

            var arguments = new HostArguments(Args);

            string endpointName = string.Empty;
            if (arguments.EndpointName != null)
                endpointName = arguments.EndpointName;

            return new WindowsHost(endpoint, Args, endpointName, false, arguments.ScannedAssemblies.ToArray());
        }
        public void when_endpoint_type_is_provided_via_hostArgs_it_should_have_first_priority()
        {
            EndpointTypeDeterminer = new EndpointTypeDeterminer(AssemblyScanner,
                () => ConfigurationManager.AppSettings["EndpointConfigurationType"]);
            hostArguments = new HostArguments(new string[0])
            {
                EndpointConfigurationType = typeof(TestEndpointType).AssemblyQualifiedName
            };

            RetrievedEndpointType = EndpointTypeDeterminer.GetEndpointConfigurationTypeForHostedEndpoint(hostArguments).Type;

            Assert.AreEqual(typeof(TestEndpointType), RetrievedEndpointType);
        }
        static void DomainInitializer(string[] args)
        {
            Console.WriteLine("Initializing the installer in the Install AppDomain");
            Parser.Args commandLineArguments = Parser.ParseArgs(args);
            var arguments = new HostArguments(commandLineArguments);

            string endpointName = string.Empty;
            if (arguments.EndpointName != null)
                endpointName = arguments.EndpointName.Value;

            string[] scannedAssemblies = null;
            if (arguments.ScannedAssemblies != null)
                scannedAssemblies = arguments.ScannedAssemblies.Value.Split(';').ToArray();

            host = new WindowsHost(Type.GetType(arguments.EndpointConfigurationType.Value, true), args[0].Split(';').ToArray(), endpointName, commandLineArguments.Install, (arguments.InstallInfrastructure != null), scannedAssemblies);
        }
        /// <summary>
        /// Returns an instance of <see cref="GenericHost"/>
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        protected override object DoGetInstance(Type serviceType, string key)
        {
            var endpoint = Type.GetType(key,true);

            Parser.Args commandLineArguments = Parser.ParseArgs(Args);
            var arguments = new HostArguments(commandLineArguments);

            string endpointName = string.Empty;
            if (arguments.EndpointName != null)
                endpointName = arguments.EndpointName.Value;

            string[] scannedAssemblies = null;
            if (arguments.ScannedAssemblies != null)
                scannedAssemblies = arguments.ScannedAssemblies.Value.Split(';').ToArray();

            return new WindowsHost(endpoint, Args, endpointName, false, false, scannedAssemblies);
        }
Beispiel #9
0
        private static void Main(string[] args)
        {
            Parser.Args commandLineArguments = Parser.ParseArgs(args);
            var arguments = new HostArguments(commandLineArguments);

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

                return;
            }

            Type endpointConfigurationType = GetEndpointConfigurationType(arguments);

            AssertThatEndpointConfigurationTypeHasDefaultConstructor(endpointConfigurationType);

            string endpointConfigurationFile = GetEndpointConfigurationFile(endpointConfigurationType);

            if (!File.Exists(endpointConfigurationFile))
            {
                throw new InvalidOperationException("No configuration file found at: " + endpointConfigurationFile);
            }

            var endpointConfiguration = Activator.CreateInstance(endpointConfigurationType);

            EndpointId = GetEndpointId(endpointConfiguration);

            AppDomain.CurrentDomain.SetupInformation.AppDomainInitializerArguments = args;

            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 : 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);
        }
Beispiel #10
0
        static void Main(string[] args)
        {
            Parser.Args commandLineArguments = Parser.ParseArgs(args);
            var arguments = new HostArguments(commandLineArguments);

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

                return;
            }
            assemblyScannerResults = AssemblyScanner.GetScannableAssemblies();
            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
            if (arguments.EndpointName == null)
                args = args.Concat(new[] { "/endpointName:" + endpointName }).ToArray();

            //Add the ScannedAssemblies name so that the new appdomain can get it
            if (arguments.ScannedAssemblies == null)
                args = args.Concat(new[] { "/scannedassemblies:" + string.Join(";", assemblyScannerResults.Assemblies.Select(s => s.ToString()).ToArray()) }).ToArray();

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

            AppDomain.CurrentDomain.SetupInformation.AppDomainInitializerArguments = args;
            if ((commandLineArguments.Install) || (arguments.InstallInfrastructure != null))
                WindowsInstaller.Install(args, 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 + "\"";
                                                                   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);
        }
Beispiel #11
0
        /// <summary>
        /// Gives a string which serves to identify the endpoint.
        /// </summary>
        /// <param name="endpointConfigurationType"></param>
        /// <param name="arguments"> </param>
        /// <returns></returns>
        static string GetEndpointName(Type endpointConfigurationType, HostArguments arguments)
        {
            var endpointConfiguration = Activator.CreateInstance(endpointConfigurationType);
            var endpointName = endpointConfiguration.GetType().Namespace;

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

            var arr = endpointConfiguration.GetType().GetCustomAttributes(typeof(EndpointNameAttribute), false);
            if (arr.Length == 1)
                endpointName = (arr[0] as EndpointNameAttribute).Name;

            if (endpointConfiguration is INameThisEndpoint)
                endpointName = (endpointConfiguration as INameThisEndpoint).GetName();

            if (arguments.EndpointName != null)
                endpointName = arguments.EndpointName.Value;

            return endpointName;
        }
Beispiel #12
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);
        }