private static TinyIoCContainer ConfigureContainer(IEnumerable <IDiagnosticsProvider> providers, IRootPathProvider rootPathProvider, IRequestTracing requestTracing, NancyInternalConfiguration configuration, DiagnosticsConfiguration diagnosticsConfiguration)
        {
            var diagContainer = new TinyIoCContainer();

            diagContainer.Register <IInteractiveDiagnostics, InteractiveDiagnostics>();
            diagContainer.Register <IRequestTracing>(requestTracing);
            diagContainer.Register <IRootPathProvider>(rootPathProvider);
            diagContainer.Register <NancyInternalConfiguration>(configuration);
            diagContainer.Register <IModelBinderLocator, DefaultModelBinderLocator>();
            diagContainer.Register <IBinder, DefaultBinder>();
            diagContainer.Register <IFieldNameConverter, DefaultFieldNameConverter>();
            diagContainer.Register <BindingDefaults, BindingDefaults>();
            diagContainer.Register <ISerializer>(new DefaultJsonSerializer {
                RetainCasing = false
            });

            foreach (var diagnosticsProvider in providers)
            {
                var key = string.Concat(
                    diagnosticsProvider.GetType().FullName,
                    "_",
                    diagnosticsProvider.DiagnosticObject.GetType().FullName);

                diagContainer.Register <IDiagnosticsProvider>(diagnosticsProvider, key);
            }

            foreach (var moduleType in AppDomainAssemblyTypeScanner.TypesOf <DiagnosticModule>().ToArray())
            {
                diagContainer.Register(typeof(INancyModule), moduleType, moduleType.FullName).AsMultiInstance();
            }

            return(diagContainer);
        }
Exemple #2
0
        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="args"></param>
        public void Run(string[] args)
        {
            if (this.EnableBanner)
            {
                Console.WriteLine(this.banner);
                Console.WriteLine();
            }
            if (serviceRunning)
            {
                throw new Exception($"服务[{serviceName}]已运行,不能重复运行!");
            }
            this.registedModules =
                from t in AppDomainAssemblyTypeScanner
                .TypesOf <INancyModule>(ScanMode.ExcludeNancyNamespace)
                select new ModuleRegistration(t);

            if (args.Contains("--console"))
            {
                WriteToLog($"服务[{serviceName}]正在使用控制台模式启动");
                this.ServiceRunningMode = RunningMode.Console;
                //run in console
                this.InternalRun();
                this.OnServiceStarted?.Invoke(this);
                WriteToLog($"服务[{serviceName}]使用控制台模式启动成功,按 ENTER 退出控制台程序!");
                Console.ReadLine();
                this.Stop();
            }
            else if (args.Contains("--install"))
            {
                serviceManager.Install();
                serviceManager.Start();
            }
            else if (args.Contains("--uninstall"))
            {
                serviceManager.Stop();
                serviceManager.UnInstall();
            }
            else if (args.Contains("--start"))
            {
                serviceManager.Start();
            }
            else if (args.Contains("--stop"))
            {
                serviceManager.Stop();
            }
            else
            {
                //window service main entry
                var servicesToRun = new ServiceBase[]
                {
                    new InternalService(this)
                };
                WriteToLog($"服务[{serviceName}]正在使用Window服务模式启动");
                this.ServiceRunningMode = RunningMode.WindowsService;
                ServiceBase.Run(servicesToRun);
                this.OnServiceStarted?.Invoke(this);
                WriteToLog($"服务[{serviceName}]使用Window服务模式启动成功");
            }
        }
Exemple #3
0
        private static string[] GetViewEngines()
        {
            var engines =
                AppDomainAssemblyTypeScanner.TypesOf <IViewEngine>();

            return(engines
                   .Select(engine => engine.Name.Split(new [] { "ViewEngine" }, StringSplitOptions.None)[0])
                   .ToArray());
        }
Exemple #4
0
        /// <summary>
        /// Locates all the default conventions and calls them in
        /// turn to build up the default conventions.
        /// </summary>
        private void BuildDefaultConventions()
        {
            this.conventions = AppDomainAssemblyTypeScanner
                               .TypesOf <IConvention>()
                               .Select(t => (IConvention)Activator.CreateInstance(t));

            foreach (var convention in this.conventions)
            {
                convention.Initialise(this);
            }
        }
        public void Should_ingore_assemblies_specified_in_AppDomainAssemblyTypeScanner()
        {
            // Given
            // When
            AppDomainAssemblyTypeScanner.IgnoredAssemblies =
                new Func <Assembly, bool>[]
            {
                asm => asm.FullName.StartsWith("mscorlib")
            };

            // Then
            AppDomainAssemblyTypeScanner.TypesOf <IEnumerable>().Where(t => t.Assembly.FullName.StartsWith("mscorlib")).Count().ShouldEqual(0);
        }
Exemple #6
0
        /// <summary>
        /// Locates all the default conventions and calls them in
        /// turn to build up the default conventions.
        /// </summary>
        private void BuildDefaultConventions()
        {
            var defaultConventions =
                AppDomainAssemblyTypeScanner.TypesOf <IConvention>(ScanMode.OnlyNancy);

            this.conventions = defaultConventions
                               .Union(AppDomainAssemblyTypeScanner.TypesOf <IConvention>(ScanMode.ExcludeNancy))
                               .Select(t => (IConvention)Activator.CreateInstance(t));

            foreach (var convention in this.conventions)
            {
                convention.Initialise(this);
            }
        }
        private static bool GetDebugMode()
        {
            try
            {
                var assembliesInDebug = AppDomainAssemblyTypeScanner
                                        .TypesOf <INancyModule>(ScanMode.ExcludeNancy)
                                        .Select(x => x.Assembly.GetCustomAttributes(typeof(DebuggableAttribute), true))
                                        .Where(x => x.Length != 0);

                return(assembliesInDebug.Any(d => ((DebuggableAttribute)d[0]).IsJITTrackingEnabled));
            }
            catch
            {
                return(false);
            }
        }
        public static RestExceptionRepackagingRegistrar Configure(Action <RestExceptionConfiguration> config)
        {
            var configurer = new RestExceptionConfiguration();

            var repackagers =
                AppDomainAssemblyTypeScanner.TypesOf <IExceptionRepackager>(ScanMode.ExcludeNancy);

            repackagers.ToList().ForEach(
                x => configurer.WithRepackager((IExceptionRepackager)Activator.CreateInstance(x)));

            configurer.WithDefault(new InternalServerExceptionRepackager());

            config(configurer);

            return(new RestExceptionRepackagingRegistrar(configurer));
        }
Exemple #9
0
        private static bool GetDebugMode()
        {
            try
            {
                //Get all non-nancy assemblies, and select the custom attributes
                var assembliesInDebug
                    = AppDomainAssemblyTypeScanner.TypesOf <INancyModule>(ScanMode.ExcludeNancy)
                      .Select(x => x.Assembly.GetCustomAttributes(typeof(DebuggableAttribute), true))
                      .Where(x => x.Length != 0);

                //if there are any, then return the IsJITTrackingEnabled
                //else if the collection is empty it returns false
                return(assembliesInDebug.Any(d => ((DebuggableAttribute)d[0]).IsJITTrackingEnabled));
            }
            catch (Exception)
            {
                // Evil catch all - don't want to blow up trying to detect debug mode!
                return(false);
            }
        }
        private static bool GetDebugMode()
        {
            try
            {
                var assembly = AppDomainAssemblyTypeScanner.TypesOf <NancyModule>(true).FirstOrDefault().Assembly;

                var attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), true);

                if (attributes.Length == 0)
                {
                    return(false);
                }

                var d = (DebuggableAttribute)attributes[0];

                return(d.IsJITTrackingEnabled);
            }
            catch (Exception)
            {
                // Evil catch all - don't want to blow up trying to detect debug mode!
                return(false);
            }
        }