Exemple #1
0
        /// <summary>
        /// Inject required service once in lifetime
        /// </summary>
        public static void EnsureInject()
        {
            if (_isInit)
            {
                return;
            }

            //init logger
            LoadLoggerConfig();

            // //创建logger
            var logger = LogManager.GetLogger(typeof(Program));

            logger.Debug("Program start");

            // Set up configuration sources.
            var cfBuilder = new ConfigurationBuilder()
                            .SetBasePath(Path.Combine(AppContext.BaseDirectory))
                            //.AddJsonFile("appsettings.json", false)
                            .AddJsonFile("wegousharpsettings.json", false);
            var config = cfBuilder.Build();

            var sp = new ServiceCollection()
                     .AddSingleton <IConfiguration>(config)
                     .AddSingleton(logger)
                     .AddSingleton(config.GetSection("YunDaMa").Get <YunDaMaConfig>())
                     .AddScoped <IDecoder, OnlineDecoder>()
                     .AddScoped <WeGouService, WeGouService>()
                     .AddSingleton <Browser, Browser>()
                     .BuildServiceProvider();

            ServiceProviderAccessor.SetServiceProvider(sp);

            _isInit = true;
        }
Exemple #2
0
        public WeGouService()
        {
            Program.EnsureInject();

            var logger  = ServiceProviderAccessor.ResolveService <ILog>();
            var browser = ServiceProviderAccessor.ResolveService <Browser>();
            var conf    = ServiceProviderAccessor.ResolveService <IConfiguration>();

            _wechatSogouApi = new WechatSogouApi(logger, browser, conf);
        }