public HomeController(IUserService userService, IMarkService markService, IDeviceService deviceService, IDataCollectionService dataCollectionService) { _markService = markService; _deviceService = deviceService; _dataCollectionService = dataCollectionService; _userService = userService; }
private static void InitializeApplication() { // Detecte appsetting.json var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfigurationRoot configuration = builder.Build(); // get connection string from json file var connectionString = configuration.GetConnectionString("db"); // create builder option object for initialize EntityFrameWork var optionsBuilder = new DbContextOptionsBuilder<TcpExampleDBContext>(); optionsBuilder.UseSqlServer(connectionString); var serviceProvider = new ServiceCollection().AddLogging() .AddSingleton<IMarkService, MarkService>() .AddSingleton<IDeviceService, DeviceService>() .AddSingleton<IDataCollectionService, DataCollectionService>() .AddSingleton<ITcpExampleDBContext>(provider => new TcpExampleDBContext(optionsBuilder.Options)) .BuildServiceProvider(); // get instanse of services markService = serviceProvider.GetService<IMarkService>(); deviceService = serviceProvider.GetService<IDeviceService>(); dataCollectionService = serviceProvider.GetService<IDataCollectionService>(); }