Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

add dependency injection .net core console app

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

public class Program
{
    public static void Main(string[] args)
    {
        //setup our DI
        var serviceProvider = new ServiceCollection()
            .AddLogging()
            .AddSingleton<IFooService, FooService>()
            .AddSingleton<IBarService, BarService>()
            .BuildServiceProvider();

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

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

        //do the actual work here
        var bar = serviceProvider.GetService<IBarService>();
        bar.DoSomeRealWork();

        logger.LogDebug("All done!");

    }
}
Comment

add dependency injection .net core console app

{
  "dependencies": {
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.DependencyInjection": "1.0.0"
  }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to convert int to float in c# 
Csharp :: .net c# print object 
Csharp :: mvc get base url 
Csharp :: c# print console 
Csharp :: string isnullorempty vs isnullorwhitespace 
Csharp :: change button color in script unity 
Csharp :: unity c# foreach 
Csharp :: failed to read the request form. missing content-type boundary .net core 
Csharp :: bitmap to imagesource c# 
Csharp :: c# csv read write 
Csharp :: c# remove items from one list that are in another 
Csharp :: c# use hashtable check if key exists 
Csharp :: c# enum to int array 
Csharp :: c# convert int to string 
Csharp :: Razor if-else statement 
Csharp :: Throw index out of range C# 
Csharp :: c# int array length 
Csharp :: c# new object without class 
Csharp :: how to get text from textbox in windows form c# 
Csharp :: how to check that string has only alphabet in c# 
Csharp :: rock paper scissors c# 
Csharp :: roman to int 
Csharp :: c# list 
Csharp :: generate qr code c# 
Csharp :: all possible substrings of a string 
Csharp :: c# create tasks and wait all 
Csharp :: c# get certain character from string 
Csharp :: postasjsonasync reference c# 
Csharp :: minimize maximize restore wpf buttons 
Csharp :: c# enum 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =