MySQL Error: : ‘Access denied for user ‘root’@’localhost’

sudo mysql
-- for MySQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

-- for MariaDB
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');

With a single query we are changing the auth_plugin to mysql_native_password and setting the root password to root and there isn’t any need to restart mysqld or start it with special privileges.

References
https://stackoverflow.com/questions/41645309/mysql-error-access-denied-for-user-rootlocalhost

Dependency Injection in ASP.NET Core

Creating the View Model

Create the Models folder and create the following viewModel.

public class ProductViewModel
{
    public int Id { get; set; }
    public string Name { get; internal set; }
}

Adding the Service

Create the folder Services and add a class with the name ProductService.cs.

using DependencyInjection.Models;
using System.Collections.Generic;
 
namespace DependencyInjection.Service
{
 
    public interface IProductService
    {
        List<ProductViewModel> getAll();
    }
 
    public class ProductService : IProductService
    {
        public List<ProductViewModel> getAll()
        {
            return new List<ProductViewModel>
            {
                new ProductViewModel {Id = 1, Name = "Pen Drive" },
                new ProductViewModel {Id = 2, Name = "Memory Card" },
                new ProductViewModel {Id = 3, Name = "Mobile Phone" },
                new ProductViewModel {Id = 4, Name = "Tablet" },
                new ProductViewModel {Id = 5, Name = "Desktop PC" } ,
            };
        }
     }
}

Using the Service in Controller

using DependencyInjection.Service;
using Microsoft.AspNetCore.Mvc;
 
namespace DependencyInjection.Controllers
{
    public class HomeController : Controller
    {
        private IProductService _productService;
 
        public HomeController(IProductService productService)
        {
            _productService = productService;
        }
 
        public IActionResult Index()
        {
            return View(_productService.getAll());
        }
    }
}

Register the Service

The last step is to register the service in the Dependency Injection container.

Open the startup.cs and goto to ConfigureServices method. This is where all services are configured for dependency injection.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddTransient<IProductService, ProductService>();
}

Managing the Service Lifetime

There are three ways, by which you can do that. And it in turn decides how the DI Framework manages the lifecycle of the services.

  1. Transient: creates a new instance of the service, every time you request it.
  2. Scoped: creates a new instance for every scope. (Each request is a Scope). Within the scope, it reuses the existing service.
  3. Singleton: Creates a new Service only once during the application lifetime, and uses it everywhere
services.AddTransient<ITransientService, SomeService>();
services.AddScoped<IScopedService, SomeService>();
services.AddSingleton<ISingletonService, SomeService>();

References
https://www.tektutorialshub.com/asp-net-core/asp-net-core-dependency-injection/
https://www.tektutorialshub.com/asp-net-core/asp-net-core-dependency-injection-lifetime/

Convert MP4 video to MP3 audio with FFmpeg

ffmpeg -i filename.mp4 filename.mp3

fixed bit rate :

ffmpeg -i video.mp4 -b:a 192K -vn music.mp3

variable bit rate :

ffmpeg -i in.mp4 -q:a 0 -map a out.mp3

variable bit rate of 165 with libmp3lame encoder :

ffmpeg -vn -sn -dn -i input.mp4 -codec:a libmp3lame -qscale:a 4 output.mp3
  • -vn disables all video-streams from the input
  • -sn disables all subtitle-streams from the input
  • -dn disables all data-streams from the input
  • -i specifies the input file
  • -codec:a libmp3lame specifies the encoder
  • -qscale:a 4 specifies the quality target for libmp3lame
  • The last argument is the output-file. Note that the file-extension might be used to infer additional information.

References
https://superuser.com/questions/332347/how-can-i-convert-mp4-video-to-mp3-audio-with-ffmpeg
https://superuser.com/questions/1463710/convert-mp4-to-mp3-with-ffmpeg

Minimum Log Level in Serilog

Serilog defines several levels of log events. From low to high, these are Verbose, Debug, Information, Warning, Error and Fatal.

Level Usage
Verbose Verbose is the noisiest level, rarely (if ever) enabled for a production app.
Debug Debug is used for internal system events that are not necessarily observable from the outside, but useful when determining how something happened.
Information Information events describe things happening in the system that correspond to its responsibilities and functions. Generally these are the observable actions the system can perform.
Warning When service is degraded, endangered, or may be behaving outside of its expected parameters, Warning level events are used.
Error When functionality is unavailable or expectations broken, an Error event is used.
Fatal The most critical level, Fatal events demand immediate attention.

Default Level – if no MinimumLevel is specified, then Information level events and higher will be processed.

References
https://github.com/serilog/serilog/wiki/Configuration-Basics