.NET CORE CONSOLE TO GET VALUES FROM APPSETTING.JSON

Open your Visual Studio then select Create a new project.

Select Console App.

Name our new project GetAppSettingJsonValues.

For the Framework, i choose .NET 8, because that is the only .NET version i do have on my computer. You might want to choose other version.

Click Create button. A window with two panels will be opened side by side. The right panel is Solution Explorer as you can see below. By default the .NET CORE console app will generate Program.cs file for you.

The left panel of the window is for editing Program.cs. If you are on lower version of .NET CORE, you may still get the Main() static method as a pregenerated code. Because i am on .NET CORE 8, the format has changed or simplified like seen below.

Go back to Solution Explorer panel again, right click your mouse on the project name and select Add, New Item, Text File, change the name of the file to appsetting.json followed by clicking Add button.

On the left panel of your window, your new appsetting.json is opened and copy the following json data into that file.

{
"smtp": {
"host": "192.168.57.31",
"port": 25,
"username": "johndoe",
"password": "nutrient"
}
}

Select your project name on Solution Explorer panel. Right click, select Manage Nuget Package. A nuget package manager window will appear on your left panel. Click the Browse tab. and type Microsoft.Extensions.Configuration on the search text box. You would see Microsoft.Extension.Configuration package on the search result. Select that package and install it to your project.

You need to click Apply button to confirm your package installation.

The next package you need is Microsoft.Extensions.Configuration.Binder. You need to repeat the process. Type that package name on search text box, select it from the search result list and then install it.

The third package you need is Microsoft.Extensions.Configuration.Json.

Copy the following C# code below to Program.cs file on the left panel of the window.

using Microsoft.Extensions.Configuration;

namespace MyApp
{
internal class Program
{
static void Main(string[] args)
{
IConfiguration builder = new ConfigurationBuilder().AddJsonFile("appsetting.json").Build();
ConfigurationManager manager = new ConfigurationManager();
manager.AddConfiguration(builder);
string? host = manager.GetSection("smtp").GetValue<string>("host");
int? port = manager.GetSection("smtp").GetValue<int>("port");
string? username = manager.GetSection("smtp").GetValue<string>("username");
string? password = manager.GetSection("smtp").GetValue<string>("password");
Console.WriteLine("Host => {0}", host);
Console.WriteLine("Port => {0}", port);
Console.WriteLine("Username => {0}", username);
Console.WriteLine("Password => {0}", password);
}
}
}

On the Solution Explorer, select the project name, right click and select Build. You will see a bin folder being built. This folder has sub folder which named by default based on your .NET CORE version. Because i am on version 8, the sub folder i am getting net8.0.

Before we can run our console application, we need to copy our appsetting.json from project root folder to that sub folder. Once you have done it, press F5 to run the application.

You can download the project code from this link.

BARCHART USING SCOTTPLOT

Run your Visual Studio IDE, select Console App follow by clicking NEXT button.

Name our new project ScottPlotBarChart. Click NEXT.

Select any framework that suit you, in may case is .NET 8. Click NEXT.

On Solution Explorer, right click and select Manage Nuget Package.

On the pop up window, select Browser tab and type ScottPlot on text box. Soon after that select ScottPlot nuget package from the list below.

Now cut and paste the following code below and click F5 to run.

using ScottPlot;
using ScottPlot.Legends;

namespace ScottPlotBarChart
{
class Program
{
static void Main(string[] args)
{
Bar bar1 = new Bar();
bar1.Value = 75000;
bar1.Position = 1;
bar1.Size = 0.5;
bar1.FillColor = ScottPlot.Color.FromHex("AC92EB");

Bar bar2 = new Bar();
bar2.Value = 58000;
bar2.Position = 2;
bar2.FillColor = ScottPlot.Color.FromHex("4FC1E8");
bar2.Size = 0.5;

Bar bar3 = new Bar();
bar3.Value = 45000;
bar3.Position = 3;
bar3.FillColor = ScottPlot.Color.FromHex("A0D568");
bar3.Size = 0.5;

Bar bar4 = new Bar();
bar4.Value = 91500;
bar4.Position = 4;
bar4.FillColor = ScottPlot.Color.FromHex("FFCE54");
bar4.Size = 0.5;

Bar bar5 = new Bar();
bar5.Value = 67500;
bar5.Position = 5;
bar5.FillColor = ScottPlot.Color.FromHex("ED5564");
bar5.Size = 0.5;

Tick[] ticks =
{
new(1, "John"),
new(2, "Marry"),
new(3, "Martha"),
new(4, "Isac"),
new(5, "Harleen"),
};

Plot myPlot = new();
myPlot.HideGrid();
myPlot.Title("Employee Salaries", 14);
myPlot.XLabel("Employees");
myPlot.YLabel("Salaries");
myPlot.Add.Bar(bar1);
myPlot.Add.Bar(bar2);
myPlot.Add.Bar(bar3);
myPlot.Add.Bar(bar4);
myPlot.Add.Bar(bar5);

myPlot.Axes.Bottom.TickGenerator = new ScottPlot.TickGenerators.NumericManual(ticks);
myPlot.Axes.Bottom.MajorTickStyle.Length = 0;
myPlot.Axes.Margins(bottom: 0);
myPlot.SavePng(@"c:\ScottPlotBarChart.png", 800, 600);
myPlot.Dispose();
}
}
}

After you ran the code above you would see a png file been generated.

If you want the source code, please click on this link

SIMPLE CHARTING ON .NET FRAMEWORK 4.7.2

Creating a simple chart and save it as an image file is very handy in .NET FRAMEWORK 4.7.2. On your project you just need to add a package called Microsoft.Chart.Controls.

This nuget package supports upto 34 different chart type.

Copy and paste the following code below on your .NET Framework console project.

using System.IO;
using System.Drawing;
using System.Runtime.Serialization.Json;
using System.Windows.Forms.DataVisualization.Charting;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a chart object
            Chart chart = new Chart();

            // Set chart title and dimensions
            chart.Titles.Add("Alarm History");
            chart.Size = new Size(1000, 800);

            // Create a chart area and add it to the chart
            ChartArea chartArea = new ChartArea();
            chartArea.Name = "Sample Chart Area";
            chart.ChartAreas.Add(chartArea);

            // Create a data series and add it to the chart
            Series series1 = new Series();
            series1.LegendText = "Front Freezer";
            series1.Color = Color.Red;
            series1.BorderWidth = 5;
            series1.Points.AddXY("08/22", 700);
            series1.Points.AddXY("09/22", 450);
            series1.Points.AddXY("10/22", 550);
            series1.Points.AddXY("11/22", 518);
            series1.Points.AddXY("12/22", 460);
            series1.Points.AddXY("01/23", 670);
            series1.Points.AddXY("02/23", 745);
            series1.ChartType = SeriesChartType.Line;

            Series series2 = new Series();
            series2.LegendText = "Cool Room";
            series2.Color = Color.Blue;
            series2.BorderWidth = 5;
            series2.Points.AddXY("08/22", 530);
            series2.Points.AddXY("09/22", 600);
            series2.Points.AddXY("10/22", 800);
            series2.Points.AddXY("11/22", 828);
            series2.Points.AddXY("12/22", 850);
            series2.Points.AddXY("01/23", 810);
            series2.Points.AddXY("02/23", 805);
            series2.ChartType = SeriesChartType.Line;
            chart.Series.Add(series1);
            chart.Series.Add(series2);

            Legend legend1 = new Legend();
            legend1.Enabled = true;
            legend1.Docking = Docking.Bottom;
            chart.Legends.Add(legend1);

            // Save the chart as an image file
            using (MemoryStream ms = new MemoryStream())
            {
                chart.SaveImage(ms, ChartImageFormat.Png);
                byte[] imageBytes = ms.ToArray();
                File.WriteAllBytes(@"c:\sample_chart.png", imageBytes);
            }
        }
    }
}

The output of the running code look like this.

Click here to download the code.

ADDING DEPENDENCY INJECTION ON CONSOLE APPLICATIONS

To implement DEPENDENCY INJECTION natively on .NET CORE, we need to import one of Microsoft Extensions library, in this case Microsoft.Extensions.DependencyInjection. In this article i am using Visual Studio 2019 and .NET CORE 5.

Open your Visual Studio 2019 and select Console Application.

I this case i am going to use C#. Click Next.

Name the project DI_On_Console

Choose .NET 5.0 as our Target Framework. Then click Create.

Your Visual Studio will greet you with a simple main program for you to start with.

using System;

namespace DI_On_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

On Solution Explorer, select Dependencies. Right click then select Manage Nuget Packages.

On Browse, search for Microsoft.Extensions.DependencyInjection. Select that one and click Enter.

The next step is to create an interface on new file named as IHelloWorld,cs

namespace DI_On_Console
{
    public interface IHelloWorld
    {

        public string GetString();
    }
}

Create a class to implent that interface. Name this file HelloWorld.cs.

namespace DI_On_Console
{
    class HelloWorld : IHelloWorld
    {
        public string GetString()
        {
            return "Hello World!";
        }
    }
}

We need ServiceCollection to register our newly interface and its implemented class and use GetService method create an instance of that class.

using System;
using Microsoft.Extensions.DependencyInjection;

namespace DI_On_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            IServiceCollection services = new ServiceCollection();
            services.AddScoped<IHelloWorld, HelloWorld>();

            IServiceProvider provider = services.BuildServiceProvider();
            IHelloWorld instance = provider.GetService<IHelloWorld>();
            string value = instance.GetString();
            Console.WriteLine(value);
            Console.ReadLine();
        }
    }
}

If you run a the code about, it would give the Hello World! as seen below.

Click here to download the source code