Skip to content

amenayach/CoreConsole.DependencyInjection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

To enable JSON configuration we need the following:

Nuget packages:

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json

Add appsettings.json files “Copy to Output Directory” property should also be set to “Copy if newer” so that the application is able to access it when published.

using System;
using System.IO;
using Microsoft.Extensions.Configuration;

// ...

static void Main(string[] args)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

    IConfigurationRoot configuration = builder.Build();

    Console.WriteLine(configuration["<JsonSection>:<PropertyName>"));
}

To enable DI we need the following:

Nuget packages:

  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Logging (optional)
  • Microsoft.Extensions.Logging.Console (optional)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

// ...

//DI
var serviceProvider = new ServiceCollection()
.AddLogging()
.AddSingleton<IDataScraper, HttpDataScraper>()
.BuildServiceProvider();

//configure console logging
serviceProvider
    .GetService<ILoggerFactory>()
    .AddConsole(LogLevel.Debug);

var logger = serviceProvider.GetService<ILoggerFactory>()
    .CreateLogger<Program>();
logger.LogDebug("Starting application");

logger.LogInformation("Url: " + url);

//do the actual work here
var scraper = serviceProvider.GetService<IDataScraper>();
var data = scraper.GetData(url);

Releases

No releases published

Packages

No packages published

Languages