Beispiel #1
0
        public static void Configure(IConfiguration configuration)
        {
            IServiceCollection serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            serviceCollection.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Trace));
            serviceCollection.AddLogging(builder => builder.AddDynamicConsole());
            LoggerFactory = serviceCollection.BuildServiceProvider().GetService <ILoggerFactory>();
        }
Beispiel #2
0
 public static void ConfigureServices(ServiceCollection services)
 {
     services.AddLogging();
     services.AddOptions();
     //			services.Configure<FBoxClientParameters>(_configuration.GetSection("FBox"));
     services.AddTransient <FBoxDemo>();
 }
Beispiel #3
0
        public static IAuthorizationService BuildAuthorizationService(Action <IServiceCollection> setupServices = null)
        {
            var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            services.AddAuthorization();
            services.AddOptions();
            services.AddLogging();
            setupServices?.Invoke(services);
            return(services.BuildServiceProvider().GetRequiredService <IAuthorizationService>());
        }
        public static ServiceProvider CreateServiceProvider(Action <IServiceCollection> addServices)
        {
            var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            services.AddLogging((loggingBuilder) => {
                loggingBuilder.AddDebug();
            });
            if (addServices != null)
            {
                addServices(services);
            }
            return(services.BuildServiceProvider(true));
        }
        private static IServiceCollection CreateServices()
        {
            var services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            services.AddOptions();
            services.AddLogging((builder) => {
                builder.AddConsole();
            });
            services.AddHttpClientGenerator((configure) => {
                configure.AddConfiguration("github", (httpClientConfiguration) => {
                    httpClientConfiguration.BaseAddress = "https://www.github.com";
                });
            });
            return(services);
        }
Beispiel #6
0
        } // End Task RunAsPlatformIndependentService

        // static async System.Threading.Tasks.Task MainTask(string[] args)
        static async System.Threading.Tasks.Task RunAsWindowsService(string[] args)
        {
            Microsoft.Extensions.DependencyInjection.IServiceCollection services =
                new Microsoft.Extensions.DependencyInjection.ServiceCollection();

            // Create configuration builder
            Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder =
                new Microsoft.Extensions.Configuration.ConfigurationBuilder()
                .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                // .AddIniFile(@"D:\inetpub\LdapService\LdapService.ini")
                //.AddJsonFile("appsettings.json")
            ;

            // Inject configuration
            services.AddSingleton <Microsoft.Extensions.Configuration.IConfiguration>(provider =>
            {
                return(configurationBuilder.Build());
            });


            // Inject Serilog
            services.AddLogging(options =>
            {
                options.SetMinimumLevel(LogLevel.Information);
                options.AddFilter(x => x >= LogLevel.Trace);

                options.AddConsole();
                options.AddDebug();

                /*
                 * options.AddSerilog(
                 *  new LoggerConfiguration()
                 *             .ReadFrom.Configuration(configurationBuilder.Build())
                 *             .CreateLogger()
                 *  );
                 */
            });


            // Inject common service
            services.AddSingleton(typeof(ICommonService), typeof(CommonSampleService));

            // Inject concrete implementation of the service
            services.AddSingleton(typeof(System.ServiceProcess.ServiceBase), typeof(GenericService));

            // My configuration
            services.AddSingleton(new MyConfig());

            services.Configure <SmtpConfig>(
                delegate(SmtpConfig config)
            {
                config.Server = "hello world";
                return;
            }
                );


            /*
             * // IConfiguration Configuration = null;
             *
             * // services.Configure<SmtpConfig>(Configuration.GetSection("Smtp"));
             * // IConfiguration iconf = Configuration.GetSection("Smtp");
             * Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure<SmtpConfig>(
             *  services, iconf
             * );
             */


            // Build DI provider
            Microsoft.Extensions.DependencyInjection.ServiceProvider serviceProvider = services.BuildServiceProvider();


            if (System.Diagnostics.Debugger.IsAttached)
            {
                // Console Debug mode

                /*
                 * try
                 * {
                 *  IConfiguration confy = serviceProvider.GetService<IConfiguration>();
                 *  System.Console.WriteLine(confy);
                 *  // var sec = confy.GetSection("ErrorMail2");
                 *  // https://stackoverflow.com/questions/39169701/how-to-extract-a-list-from-appsettings-json-in-net-core
                 *  // foreach (var section in confy.GetChildren()) section.GetChildren();
                 *
                 *
                 *  // Fixes to IniStreamConfigurationProvider.cs:if (separator < 0)
                 *  var jobs = confy.GetSection("Jobs").Get<System.Collections.Generic.Dictionary<string, string>>();
                 *  var sec = confy.GetSection("ErrorMail2").Get<System.Collections.Generic.Dictionary<string, string>>();
                 *
                 *  // sec.Keys, sec.Values
                 *
                 *  System.Console.WriteLine(sec);
                 *  System.Console.WriteLine(jobs);
                 *
                 *
                 * }
                 * catch (System.Exception ex)
                 * {
                 *  System.Console.WriteLine(ex.Message);
                 * }
                 */



                GenericService svc = serviceProvider.GetService <System.ServiceProcess.ServiceBase>() as GenericService;
                svc.StartService(args);

                // System.Console.ReadLine();

                System.ConsoleKey cc = default(System.ConsoleKey);
                do
                {
                    // THIS IS MADNESS!!!   Madness huh?   THIS IS SPARTA!!!!!!!
                    while (!System.Console.KeyAvailable)
                    {
                        // System.Threading.Thread.Sleep(100);
                        await System.Threading.Tasks.Task.Delay(100);
                    }

                    cc = System.Console.ReadKey().Key;

                    if (cc == System.ConsoleKey.C)
                    {
                        System.Console.Clear();
                    }
                } while (cc != System.ConsoleKey.Enter);

                svc.Stop();
            }
            else
            {
                // Start service
                System.ServiceProcess.ServiceBase[] servicesToRun;
                servicesToRun = new System.ServiceProcess.ServiceBase[]
                {
                    serviceProvider.GetService <System.ServiceProcess.ServiceBase>()
                };

                System.ServiceProcess.ServiceBase.Run(servicesToRun);
            } // End else of if (System.Diagnostics.Debugger.IsAttached)

            // await System.Threading.Tasks.Task.CompletedTask;
        } // End Task RunAsWindowsService