10 January, 2022

ASP.NET Core Project

I use Visual Studio Code because it's faster at startup and has a well integrated terminal with the PowerShell console. It is also easier to reproduce the commands by copy/paste than showing the steps to use the graphical UI.

To start a new ASP.NET Core project you need to have the .NET 6 SDK installed. Check if you have the latest version installed with: dotnet --version, currently it's version 6.0.101 called .NET 6.0.

Create and start a project by using templates, here web:

dotnet new web -o TemplateWeb
cd TemplateWeb
dotnet run

Other common templates are webapp webapi nunit classlib. List all available templates with dotnet new

ASP.NET Backend

Create a webapi project and and MySql for Entity Framework:

dotnet new webapi -o Teach.WebApi
cd Teach.WebApi
dotnet add package Pomelo.EntityFrameworkCore.MySql
dotnet run

Alternative: dotnet add package MySql.Data.EntityFrameworkCore

todo: Install ASP.NET code generator for scaffolding code:

dotnet tool install -g dotnet-aspnet-codegenerator

Update ASP.NET code generator:

dotnet tool update -g dotnet-aspnet-codegenerator

dotnet aspnet-codegenerator

ASP.NET Web-API

Create a new project:

dotnet new web --name AuthorizationServer

Add dependencies:

cd AuthorizationServer
dotnet add package OpenIddict
dotnet add package OpenIddict.AspNetCore
dotnet add package OpenIddict.EntityFrameworkCore
dotnet add package Pomelo.EntityFrameworkCore.MySql
dotnet add package Microsoft.EntityFrameworkCore.Relational

Add ConnectionString to AuthorizationServer/appsettings.json:

{
  "ConnectionStrings": {
    "ConnectionString": "server=localhost;user id=wisecards;password=YouKnowPassword123;database=wisecards"
  }
}
dotnet ef dbcontext scaffold "server=localhost;user id=wisecards;password=YouKnowPassword123;database=wisecards" "Pomelo.EntityFrameworkCore.MySql"

Run:

dotnet run --project AuthorizationServer.csproj

Unit Test

Test a Controller using Moq to mock data-access.

using Microsoft.AspNetCore.Mvc;
using Moq;
using NUnit.Framework;
using System.Threading.Tasks;
using Wisecards.Business.Services;
using Wisecards.DataAccess;
using Wisecards.Domain;
using Wisecards.WebApi.Controllers;

namespace Wisecards.WebApi.Tests;

public class DecksControllerTest
{
  private Mock<IDeckAccess> deckAccessMock = new Mock<IDeckAccess>();

  [SetUp]
  public void Setup()
  {
  }

  [Test]
  public async Task DeleteDeck()
  {
    // arrange
    var deck = new Deck();
    deckAccessMock.Setup(m => m.GetById(It.IsAny<int>())).Returns(Task.FromResult<Deck?>(deck));
    deckAccessMock.Setup(m => m.Update(It.IsAny<Deck>())).Returns(Task.FromResult<int>(1));
    var deckService = new DeckService(null!, deckAccessMock.Object);
    var controller = new DecksController(deckService);

    // act
    var result = await controller.DeleteDeckById(1);

    // assert
    Assert.NotNull(result);
    Assert.IsTrue(deck.IsDeleted);
    Assert.AreEqual(202, (result?.Result as OkResult)?.StatusCode);
  }
}

[1] https://github.com/openiddict/openiddict-core

[2] https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

[3] https://docs.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-6.0