public void ConfigureServices(IServiceCollection services) { Configuration = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables().Build(); //services.AddHangfire(configuration => //{ // //使用sql 数据库保存一些定时作业记录 // configuration.UseSqlServerStorage(Configuration.GetSection("sql:connectionString").Value); //}); //services.AddARabbitMqClient(); services.AddSingleton <IConfiguration>(Configuration); services.AddARabbitMqClient(); //发送PEC进入打印机,服务重启重新发送PEC //var dic = Directory.GetCurrentDirectory(); //var filePath = Path.Combine(dic, "BPP_PEC.pec"); //var text = File.ReadAllText(filePath); //var portServices1 = new SendSerialPortServices(); //portServices1.Wirte(text); //使用autofac 容器 var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly.GetEntryAssembly()) .AsImplementedInterfaces(); builder.RegisterInstance <IConfiguration>(Configuration); builder.AddASampleRabbitMq(); Container = builder.Build(); var serviceProvider = new AutofacServiceProvider(Container); // This will succeed. var asampleClient = serviceProvider.GetService <IASampleRabbitMqClient>(); //使用 net core 自己的容器 //var sc = new ServiceCollection(); //new Startup().ConfigureServices(sc); //var provider = sc.BuildServiceProvider(); //// This will succeed. //var asampleClient = provider.GetService<IASampleRabbitMqClient>(); //var portServices2 = new SendSerialPortServices(); //portServices2.Read(); #region mq asampleClient.Subscribe(result => { Console.WriteLine("开始打印登机牌"); //send result to SerialPort var portServices = new SendSerialPortServices(); portServices.Wirte(result); }); #endregion }
// ConfigureContainer is where you can register things directly // with Autofac. This runs after ConfigureServices so the things // here will override registrations made in ConfigureServices. // Don't build the container; that gets done for you. If you // need a reference to the container, you need to use the // "Without ConfigureContainer" mechanism shown later. public void ConfigureContainer(ContainerBuilder builder) { // Register your own things directly with Autofac builder.RegisterAssemblyTypes(Assembly.GetEntryAssembly()) .AsImplementedInterfaces(); builder.RegisterInstance <IConfiguration>(Configuration); //builder.Populate(services); builder.AddDispatchers(); builder.AddASampleRabbitMq(); }
public static IServiceProvider InitStartup(string rabbitMq) { var config = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); //services.Configure<DbOptions>(Configuration.GetSection("sql")); var configuration = config.Build(); var services = new ServiceCollection(); //services.AddOptions<RabbitMqOption>(); //注入IOptions<T>,这样就可以在DI容器中获取IOptions<T>了 //services.Configure<RabbitMqOption>(configuration.GetSection(rabbitMq)); //注入配置数据 //services.Configure<RabbitMqOption>(configuration.GetSection(rabbitMq)); services.AddSingleton <IConfiguration>(configuration); //services.AddSingleton<IUnitOfWork<ASampleSqlServerDbContext>, UnitOfWork<ASampleSqlServerDbContext>>(); services.BuildServiceProvider(); var builder = new ContainerBuilder(); builder.RegisterInstance <IConfiguration>(configuration); ////builder.RegisterInstance<IOptions<RabbitMqOption>>(); //builder.RegisterType<RabbitMqOption>() // .As<IOptions<RabbitMqOption>>() // .InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(Assembly.GetEntryAssembly()) .AsImplementedInterfaces(); //builder.Populate(services); //builder.AddDispatchers(); builder.AddASampleRabbitMq(); Container = builder.Build(); var serviceProvider = new AutofacServiceProvider(Container); return(serviceProvider); }