Could not create an instance of type ‘Microsoft.AspNetCore.Http.HttpContext’

The new version of dotnet core cannot handle the HttpContext in the MVC model.

An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Could not create an instance of type ‘Microsoft.AspNetCore.Http.HttpContext’. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, set the ‘HttpContext’ property to a non-null value in the ‘…’ constructor.

Solution

Remove the line from the model:

public HttpContext HttpContext { get; set; }

Remove the line from the controller:

model.HttpContext = HttpContext;

Cities: Skylines undo

The Cities: Skylines game does not have a usual undo function, but by pressing the F1 key we can save the current state into a “Quick Save” file before a change.

To load the state from the Quick Save file, select it in the list of the Load Game menu item

The files are saved on your hard drive at

C:\Users\YOUR_USER_NAME\AppData\Local\Colossal Order\Cities_Skylines\Saves

Error: Failed to instantiate provider “aws” to obtain schema: Incompatible API version with plugin. Plugin version: 4, Client versions: [5]

When a provider has been updated outside of Terraform, (for example in Terraformer) Terraform can get out of sync with that provider version. terraform init works, but terraform plan throws the error message:

Error: Failed to instantiate provider “aws” to obtain schema: Incompatible API version with plugin. Plugin version: 4, Client versions: [5]

To update Terraform to be able to use the new provider execute

terraform init -upgrade

Cities: Skylines game frequently crashes

The Cities: Skylines game keeps objects in memory even if the object is not used. Even a 16GB system can crash frequently if the memory usage is not managed.

“Oops! The game crashed

The memory usage is high, even when the map is not too complex.

This page explains the issue and how to solve the problem: https://steamcommunity.com/app/255710/discussions/0/1488866180604646672/

This is the summary of the steps you can take to stabilize the game on your computer

Get jstack and other Java utilities without installing the Java JDK

  • Open a Windows command prompt
  • Create a directory for the volume and start the openjdk container
md c:\windows\temp\openjdk
docker run -it --rm -v c:\windows\temp\openjdk:c:\temp openjdk:8
  • In the Java container copy the contents of the Java bin directory to the mapped volume
cd openjdk-8\bin
copy *.* c:\temp

Docker in Windows 10 via VMware Fusion on a MacBook Pro

To run Windows Docker containers on your Macintosh computer, use the following setup. This will introduce a performance hit, because you run virtualization in virtualization.

Besides the usual setup process, you need to enable hypervisor applications to avoid the error

Error response from daemon: container … encountered an error during CreateContainer: failure in a Windows system call:  No hypervisor is present on this system

  • Install VMware Fusion on your MacBook Pro
  • Create a Windows 10 virtual machine
  • Open the Settings of the Windows 10 virtual machine
  • Select Processors & Memory
  • In the Advanced region check Enable hypervisor applications in this virtual machine

Turn off the GDPR cookie notification in a dotnet core MVC web application

The dotnet core MVC web application template adds the GDPR cookie acceptance message to the header of the page template. When the site is hosted with HTTP the accept button does not clear the message resulting an infinite loop, when the user cannot log in and the site continuously asks for the acceptance.

When the site is hosted in a Docker container it is also simpler to instal the SSL certificate on the load balancer and host the site with HTTP on port 80.

We use cookies. To be able to use this site please click the Accept button.

To remove the feature delete the

<partial name="_CookieConsentPartial" />

line from the Views\Shared\Layout.cshtml file.

Without the acknowledgement the dotnet core framework will not save any non essential cookies in the browser, so the session state will stop working.

To tell the dotnet core framework, that the application does not require the user to allow the saving of the cookies set the value of CheckConsentNeeded to false in the ConfigureServices() method, services.Configure<CookiePolicyOptions> { section

options.CheckConsentNeeded = context => false;

To make sure the session cookie is always saved even if the GDPR confirmation is enabled, declare it to essential in the ConfigureServices() method, services.AddSession(options => { section

options.Cookie.IsEssential = true;

Read the database connection string and other secrets from environment variables

It is not good practice to store secrets in the source code repository. The sample C# dotnet core application code stores the database connection string in the appsettings.json file which eventually will be pushed into GitHub. To follow the Twelve-Factor App methodology, store the configuration values in environment variables and load them runtime. This method enables your application to run in a Docker container and in a Kubernetes pod.

If the connection string is not specified, we get the error message

System.ArgumentNullException: ‘Value cannot be null.
Parameter name: connectionString’

To read the connection string from an environment variable

Add the builders to the Startup.cs file

       public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            Configuration = configuration;
            Env = env;

            // Read the connection string from the "ASPNETCORE_ConnectionStrings:DefaultConnection" environment variable
            // RESTART Visual Studio if you add or edit environment variables for the change to take effect.
            var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            // This one needs to be last for the environment variables to have the highest priority
            builder.AddEnvironmentVariables("ASPNETCORE_");
            Configuration = builder.Build();
        }

Create the ASPNETCORE_ConnectionStrings__DefaultConnection environment variable. The double underscore will be converted to a colon ( : ) . This variable name works on Windows, and also in Linux where the environment variable name cannot contain colons.

Server=MY_SERVER_URL;Port=5432;Database=MY_DATABASE;Username=MY_USER;Password=MY_PASSWORD

IMPORTANT: If you add or change an environment variable, restart Visual Studio for the change to take effect. It is not enough to restart the application.

Mount NFS volume from macOS

When we try to mount an NFS with

docker run --rm -i -v MY_MOUNT:/data alpine

from macOS we get the error message:

docker: Error response from daemon: error while mounting volume ‘/var/lib/docker/volumes/…/_data’: failed to mount local volume: mount :/data:/var/lib/docker/volumes/…/_data, data: addr=…,vers=4: operation not permitted.

or

docker: Error response from daemon: error while mounting volume ‘/var/lib/docker/volumes/…/_data’: failed to mount local volume: mount :/data:/var/lib/docker/volumes/…/_data, data: addr=…,vers=4: connection refused.

To be able to mount the volume from macOS, use the insecure option in /etc/exports

MY_MOUNT 10.0.0.0/8(rw,sync,no_root_squash,no_all_squash,insecure)