Upgraded from project.json to csproj for SimplCommerce

Today, we have upgraded from project.json to csproj for SimplCommerce. It’s simple than what I have thought. In this blog post I will share you this journey.

While working with ASP.NET Core in SimplCommerce, I felt in love with project.json. It’s an innovation, modern and simple. However, in order to make the tooling compatible with other .NET app models (WinForms, WPF, UWP, ASP.NET, iOS, Android, etc.), Microsoft has decided to move .NET Core projects to .csproj/MSBuild. More detail about this can be found here.

Struggling for a while, we finally decided to say goodbye to project.json.

Goodbye project.json

Microsoft doesn’t support .NET Core csproj on VS 2015. So, to work with csproj, we need to install VS 2017 RC or go with command line. I chose to install VS 2017 RC :). I use the lasted version of VS 2017 RC which included .NET Core 1.0 SDK – RC4.

After installed VS 2017 RC. I opened the SimplCommerce.sln. VS 2017 show a dialog and ask for an upgrade. Note it’s an one way upgrade. Once upgraded, you cannot go back.

Goodbye project.json

Took a deep breath and click OK. Whoo! sucessful, the migration report show no error, there was a warning on the solution file but it not important. All the projects are loaded.

Wait “Error occurred while restoring NuGet packages: The operation failed as details for project SimplCommerce.WebHost could not be loaded.” appeared in the Output panel. None of the project can build because all the dependencies could not be loaded.

Right click on the solution and then click “Restore Nuget Package” didn’t work aslo the same error appeared. Tried to workaroud in VS 2017, no luck.

Then I opened command line and typed “dotnet restore”, enter. wow

“C:\Program Files\dotnet\sdk\1.0.0-rc4-004771\NuGet.targets(97,5): error : ‘1.1.0-preview4-final;1.0.0-msbuild3-final’ is not a valid version string. [D:\Projects\SimplCommerce\SimplCommerce.sln]”

Let find this string. It is in the SimplCommerce.WebHost.csproj

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.1.0-preview4-final;1.0.0-msbuild3-final" />

It seem not correct, let remove the “1.1.0-preview4-final;”

All the projects build sucessful. Control + F5 the application run just fine.

Let take a look at what have changed.

  • All the file project.json, **.xproj was removed. The global.json is removed also.
  • The files project.lock.json are renamed and moved here obj\project.assets.json
  • The SimplCommerce.sln just change a little but and seem not important
  • The **csproj look simple and nice

So SimplCommerce has been upgraded to use csproj. There are a couple of things I want to note here:

AppVeyor hasn’t offically support .NET Core csproj. But you can request to join their beta here Travis hasn’t support also. I have submited the request https://github.com/travis-ci/travis-ci/issues/7301

And below are some useful references:

Read More

ASP.NET Core - A beginner guide

In this post I assume that you are .NET developers or at least you have known C# and you want to learn ASP.NET Core. If you are new to .NET or you’re coming from another language, I would recommend you to learn C# first. You can check out this nice tutorial to getting start with C# here

Although you might encounter some issues during the development because the tooling is not stable, .NET Core and ASP.NET Core definitely are the future of .NET. I believe that after the tooling reach RTM and with the release of .NET Core 2.0 and .NET Standard 2.0 (expected in Spring 2017), the adoption rate of .NET Core will increase significantly.

ASP.NET Core in nutshell


ASP.NET Core is very different from the ASP.NET that you have already known. For example: there is no System.Web.dll, HTTP Modules, HTTP Handlers, Global.asax or Web.config. ASP.NET Core is a significant redesign of ASP.NET.

The most important ASP.NET Core principle I think you have to remember is the modularity. ASP.NET Core is a composition of granular NuGet packages and everything is excluded by default. That means you might need to add some packages when you need some features. And by this way you only take the minimal set of packages you need.

1. What I need to install on my PC

An IDE. In theory, you can use any editor to code .NET Core. However, I would recommend you to use Visual Studio. It’s the best IDE to code .NET. My second recommendation is Visual Studio Code. Some of my friends use Visual Studio for Mac, but I haven’t.

.NET Core SDK or NET Core tools for Visual Studio if you use Visual Studio Download here.

2. Create an ASP.NET Core project

Create an asp.net core project and take a quick look at the project structure and generated code.

3. Learn core concepts

4. Follow tutorials

It is also a good idea to getting started by follow tutorials

5. More important concepts

6. Reading code samples

Further reading

Read More

Change primary key for ASP.NET Core Identity and more

By default ASP.NET Core Identity use a string value for the primary keys. In SimplCommerce we decided to use a long value instead. We also changed the name of identity tables and run some custom code every time a user login into the system. In this blog post I am going to show you how we did it.

First, when creating new ASP.NET Core Web Application, in the ASP.NET Core template selecting dialog, choose “Web Application”, then click on the “Change Authentication” button, and choose “Individual User Accounts”

Create ASP.NET Core Project

The model

After the project has been created. Open the ApplicationUser.cs in the “Model” folder, you will see that it inherit from IdentityUser which then inherit from IdentityUser<string>. So we need to change that

public class ApplicationUser : IdentityUser<long>

The DbContext

We need to modify a little bit the inheritance of ApplicationDbContext. Instead of inheriting from IdentityDbContext<ApplicationUser>, it should be

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<long>, long>

The Startup.cs

Telling ASP.NET Core Identity that we are using long, not string

services.AddIdentity<ApplicationUser, IdentityRole<long>>()
    .AddEntityFrameworkStores<ApplicationDbContext, long>()

The Migrations

We will need to re-create entity framework migration classes. Delete add the file in folder “Data\Migrations” then go to the Package Manager Console type Add-Migration CreateIdentitySchema


If you would like to change the name of the Indentity tables, go to the OnModelCreating method of ApplicationDbContext and add custom mapping like


In some business cases, you might also need to run some code right after users successfully login . For example, my case in SimplCommerce. We need to migrate the shopping cart of the not login user (guest) to their real user when they loginned. We extended the SignInManager and broadcast a message every time a user login successfully

namespace SimplCommerce.Module.Core.Extensions
    public class SimplSignInManager<TUser> : SignInManager<TUser> where TUser : class
        private readonly IMediator _mediator;

        public SimplSignInManager(UserManager<TUser> userManager,
            IHttpContextAccessor contextAccessor,
            IUserClaimsPrincipalFactory<TUser> claimsFactory,
            IOptions<IdentityOptions> optionsAccessor,
            ILogger<SignInManager<TUser>> logger,
            IMediator mediator)
            : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger)
            _mediator = mediator;

        public override async Task SignInAsync(TUser user, bool isPersistent, string authenticationMethod = null)
            var userId = await UserManager.GetUserIdAsync(user);
            _mediator.Publish(new UserSignedIn {UserId = long.Parse(userId)});
            await base.SignInAsync(user, isPersistent, authenticationMethod);

Then register the SimplSignInManager in ConfigureServices method of Startup.cs

services.AddScoped<SignInManager<User>, SimplSignInManager<User>>();

The source code

The source code of this post can be found here https://github.com/thiennn/AspnetCoreIdentityLong

Thanks for reading

Read More

Dockerizing a real world asp.net core application

SimplCommerce on Docker

This is the second version of dockerizing simpcommerce. Read the first version to have the full story

The day after successfully dockerizing simplcommerce, I started to re-look at the approach, the code. I also received a bug report that the container fail to start again after stopping. The proudest thing of what I have done is that it only need one command to run the entire the application including the database in one container. But it revealed several drawbacks:

  • The dockerfile is big, and it take long time to build. Around 15 minutes in dockerhub
  • Postgres has its own way to initialize the container. I have extended that process to do entity framework migration, import static data and call dotnet run to launch the website. But this process only run once, after the container started for the first time, so this is the root cause of the bug I have mentioned above
  • Putting both the database and the website into one box generally is not a good practice.

I have decided to make changes. The first thing I do is separating the database and the website. For the database I use the default images of postgres without any customization.

“Separating what changes from what stays the same” is always a good practice. So it’s a good idea to separate the source code from the sdk. With this in mind I created simpl-sdk docker image. I need dotnet core 1.1 project json sdk, nodejs, gulp-cli and postgresql client. It make scene to start from microsoft/dotnet:1.1.0-sdk-projectjson then install other stuffs.

simpl-sdk Dockerfile

I created a github repository for it and went to docker hub to created an automated build repository similar to the way I did for simpcommerce earlier

SimplCommerce Dockerfile

Now the Dockerfile for simpcommerce become very small and clean. From the simpl-sdk, copy the source code into the image, restore nugget packages, build entire the projects, call gulp copy-modules to copy the build output of modules to the host. Copy and set the entry point.

The entry point

At this time the connection to database is ready. Run dotnet ef database update to run migration, and use psql connect to the database, if no data found in database then import static data. Finally call dotnet run to start the app

It looks simple and straight forward huh. But it took me a lot of time to made it run smoothly

  • First I am not familiar with writing a shell script.
  • Second, I am also not familiar with psql.
  • Third some weird differences between linux and windows.

The first error that I got when starting the container was

“panic: standard_init_linux.go:175: exec user process caused “no such file or directory” [recovered] panic: standard_init_linux.go:175: exec user process caused “no such file or directory”

What the hell is that? After some googling, I fixed by changing the line ending in the docker-entrypoint.sh from CRLF to LF. In Notepad++ select Edit -> EOL Conversion

The second error was

“docker: Error response from daemon: invalid header field value “oci runtime error: container_linux.go:247: starting container process caused "exec: \"/docker-entrypoint.sh\": permission denied"\n”.”

Something related to permission. Continue googling, then I was able to fix it by adding RUN chmod 755 /docker-entrypoint.sh before the ENTRYPOINT ["/docker-entrypoint.sh"]

Bonus - Some usefull psql commands

Connect to a server run a query and print the out to a file
echo 'select count(*) from "Core_User";' | psql -h simpldb --username postgres -d simplcommerce -t > /tmp/varfile.txt
-h: host, -d: database, -t: to get just the tuple value

Run an sql script
psql -h simpldb --username postgres -d simplcommerce -a -f /app/src/Database/StaticData_Postgres.sql'
-h: host, -d: database, -a: echo all queries from scripts, -f: file

Connect to a server
psql -h simpldb --username postgres

When connected

\l : list all databases
\c dbname : connect to dbname database

In a database

\i path.sql : exe sql
\dt : list table
select * from "Core_User"; execute a query, the semicon at the end is required, table name is case sensitive

Quick psql

\q : quick

Read More

Dockerizing a real world asp.net core application, the original version

One single command docker run -d -p 5000:5000 simplcommerce/nightly-build, waiting a couple of minutes for docker to pull the images and start the container. Then open your browser, type localhost:5000 and you got a shopping cart website written by aspnetcore up and running. Yahoo!!!!. In this blog post I will describe how I did it.

As you might know. I am developing an open source, cross platform and modularized ecommerce system built on .NET Core which called simplcommerce.


The system now has basic features of an ecommerce like: manage catalog, shopping cart, orders, review products, manage pages etc. Generally, it is enought for simple stores. Recently, many people come to ask me questions: doesn’t run on Linux? can I deploy them to a container? I think it’s a good idea to have a docker image for simplcommerce. I started to think about it. Below are 2 things that I want to archive:

  • It should be as simple as possible to for anybody who want to try simplcommerce. We have a demo site here, but it will be more interesting to have it run on local, especially in a docker container.
  • The image should be created automatically every time we make a change on the github repo.

Fascinating! But, to be honest. I am new to docker and the Linux world, I have some general knowldege but no real experience. After reseaching for a while, reading many articles, tutorials about putting an asp.net core application to a docker container. But most of them are very basic, just a hello world application. So it took me a while to figure how to dockerizing simplcommerce. Last night, finally I dit it.

Basically, challenges that I need to resolve are:

  1. How to automate this process?
  2. How to deal with database? How to run entity framework migration? How to import some static data? (simplcommerce needs some data in the database before it start)
  3. How to run a gulp task there. The simplcommerce are composed by modules and a gulp task need to be run to copy modules to the host.

For the first challenge. In docker hub, I found that we can create “created automated build” from a github repository. Yeah, that’s cool. So, just create one and then connect to simplcommerce github repository.

Create automated build

In the build setting, I check the option to make the build happen on pushes, and specify the location of the dockerfile.

Automated build setting

For the database, normally people will go with a separate container and may leverage docker compose to wire them together. But this is complicated to me :D. This is not for production so I want thing to be as simple as possible. I decided to bundle everything into one box. From the ubuntu, I installed postgres, then donetcore sdk, then nodejs. But I faced problems with staring postgres and its complidated permission mechanism. Yep! it is really complicated to a newbie as me. Getting stuck for a while. Then with using the postgres as the base image and doing more research, eventually I could make it work. The dockerfile With postgres image, we can inject custom commands to run after the database started by putting *.sh or *.sql file in a directory called “docker-entrypoint-initdb.d”. Basically the dockerfile will do the following steps:

  1. From postgres:9.5
  2. Install some stuff that donetcore sdk and nodejs depend on
  3. Install dotnetcore sdk
  4. Install nodejs
  5. Add custom commands to “docker-entrypoint-initdb.d” directory in the image. Here I use a shell script “dockerinitcontainer.sh”
  6. Copy the simplcommerce source code to the image
  7. Build the all the .netcore project
  8. Install gulp and run “copy-modules” task to copy modules to the host

After the containers started, and the postgres engine started. The file “[dockerinitcontainer.sh]” is called which will

  1. Run entity framework migration
  2. Import some static data by using psql
  3. Finally start the app.

Yeah, a real world dotnet app run perfectly on a docker container. One command to setup everything and here we go.

This is my first baby step to docker and linux, It may be not perfect. So if you can give me suggestions, I will be more than happy.

Read More