.Net Core and Docker

Building .net core applications into Docker containers. I've spent a bit of time lately moving some of my older applications into docker containers in dotnet core 2.0 and 2.1.

I've only started exploring docker with dot net core and what we can do and so far it really has changed how I think about what I'm doing and the way I am building things.

Demo site: https://dotnetdocker.serversncode.com/ Source code for this site is available https://github.com/mordin/Serversncode-Docker-demo

With .Net Core being on the edge things change. I've started this post and the https://dotnetdocker.serversncode.com as a way to play around with docker and understand it more.

I've hosted the site on a linode server. For more on setting up linode with docker check out setting up docker on linode and nginx reverse proxy for docker.

Working with Docker in .Net Core

With a dotnet core app we need to add a dockerfile to the project. This will do the build of our application and load it into the docker container and build it.

Docker File

.Net Core 2.0

While 2.0 is not going to be supported in the long term it's where I'll start with that.

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY ServersncodeDemo.Web/ServersncodeDemo.Web.csproj .Web/
RUN dotnet restore

COPY . .
WORKDIR /src/ServersncodeDemo.Web
RUN dotnet build ServersncodeDemo.Web.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish ServersncodeDemo.Web.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ServersncodeDemo.Web.dll"]

.Net Core 2.1

Next we move to .net core 2.1 which will be supported long term.

FROM microsoft/dotnet:2.1.402-sdk-alpine3.7 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY ServersncodeDemo.Web/*.*.csproj ./ServersncodeDemo.Web/
COPY ServersncodeDemo.Data/*.*.csproj ./ServersncodeDemo.Data/
RUN dotnet restore

# copy everything else and build app
COPY ServersncodeDemo.Web/. ./ServersncodeDemo.Web/
COPY ServersncodeDemo.Data/. ./ServersncodeDemo.Data/
WORKDIR /app/ServersncodeDemo.Web
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.1.4-aspnetcore-runtime-alpine3.7  AS runtime
WORKDIR /app
COPY --from=build /app/ServersncodeDemo.Web/out ./
ENTRYPOINT ["dotnet", "ServersncodeDemo.Web.dll"]

Docker Build

I have a blog post https://serversncode.com/docker-commands/ that covers common commands.

First up we build our docker image and give it a name.

docker build -t NAME .

Next we run it locally.

docker run --name NAME -d -p 8080:80 NAME

That's it, I've run a container and pointed the port 8080 to the default port of 80. In my docker file I never changed the port from 80. To change the port of your docker file use the

EXPOSE 8040

command to tell it to use a different port than 80.

What next

Next I'll start getting into the details of the docker file how it's created and what it all means.

Building .net core applications into Docker containers. I've spent a bit of time lately moving some of my older applications into docker containers in dotnet core 2.0 and 2.1.I've only started exploring docker with dot net core and what we can do and so far it really…

Read More

ASP .NET Core RTM Roundup

As of this post in August 2016, .NET core is in RTM I wanted to round up a bunch of things as of now and update my earlier post on ASP .NET Core but also I intend to use this post as a way of gathering content I think is useful, I've made sure to keep things here that are about .NET core RTM or RC2 where noted.

Important note, this blog post serves as a collection of some things I've found on .NET core RTM. It will change as I add new things.

Roadmap Blog post from MSDN

Getting started

First and foremost and just to start us off right, we have getting started. The new docs site for ASP is brillant

Create an MVC Net Core web app in Visual studio.

Getting started with Visual Studio

Want to use the command line? Well you can do that now.

Simple as

mkdir aspnetcoreapp
cd aspnetcoreapp
dotnet new

Get started from the command line

Building stuff

Scott Hanselman walks through creating a small Web API in .NET Core in Exploring a minimal WebAPI with ASP.NET Core another from Scott where he jumps into Nancy FX Exploring a minimal WebAPI with .NET Core and NancyFX

David Paquette covers using View Components in ASP.NET core LOADING VIEW COMPONENTS FROM A CLASS LIBRARY IN ASP.NET CORE MVC

For more on View Components David has us covered with Goodbye Child Actions, Hello View Components

Chris Myers takes us into building a Web API that runs in Docker, it's a really great step by step guide Stateful Microservice with .NET Core, Docker, and PostreSQL

Laurent Kempe goes wild with .NET core in Docker on Azure Build, ship and run ASP.NET Core on Microsoft Azure using Docker Cloud

Jerrie Pelser gets into Tag helpers and using them in a web app Accessing the Request object inside a Tag Helper in ASP.NET Core

Joonas Westlin gives us a look using Azure AD in .NET Core ASP.NET Core 1.0 Azure AD Authentication

Matthew Jones with some dependency injection in ASP .NET core Getting Started with Dependency Injection in ASP.NET Core

More Dependency injection this time from Shawn Wildermuth ASP.NET Core Dependency Injection

Ben Cull shows how you can store session variables in ASP.​NET Core, and also use a backing store such as Redis or SQL Server Cache instead of the default in-memory implementation. Using Sessions and HttpContext in ASP.NET Core and MVC Core

This post on the ComponentOne blog is another in an ongoing series on how to build your own Tag Helper. TagHelpers : Authoring Nested TagHelpers in ASP.NET Core MVC

Jon Hilton shows how to Compile your changes on the fly with .NET Core Watch

Radu Matei has an Introduction to ASP.NET Core MVC API

Marius Schulz shows us how to go about Simulating Latency in ASP.NET Core

Ben Cull using Global Routes for ASP.NET Core MVC

Jürgen Gutsch how you can configure the length of time that static content such as Javascript files, images and stylesheets are cached on the client side. Add HTTP headers to static files in ASP.​NET Core

As of this post in August 2016, .NET core is in RTM I wanted to round up a bunch of things as of now and update my earlier post on ASP .NET Core but also I intend to use this post as a way of gathering content I think is…

Read More

ASP MVC HTTPS and 301 Redirect

Last time around I explained how to get Lets encrypt up and running on Windows Azure to catch up click here

Now I have it running I need to get my sites to use HTTPS all the time and also use 301 Redirect for SEO goodness

All below works as of today on Visual studio 2015 and ASP 4.6 working site https://cloudstatus.eu

Force HTTPS

First step was to always require HTTPS. This in MVC 4 + is straight forward enough, in the controller. Simply add the [RequireHttps] to the top of each controller you want to force the SSL in.

[RequireHttps]
public class HomeController : Controller

Every time you open the controller it will send you to the HTTPS version of the page

IISExpress does have an issure here but it's easy to sort. You need to tell IIS Express it's ok to use SSL.

With the web project selected hit F4 to bring up the "Project Properties pane"

You should see an option "SSL Enabled" select True and then when you run the website it will open on the port set.

This now means that all requests to the site will be using the https. If a visitor uses http:// then it will be sent to the [RequireHttps] this will then send it with a redirect.

301 or 302 Redirect

RequireHttps sends a 302 redirect you can test your redirect here

302 Redirect is fine I think in most cases but it's a temporary redirect and Google isn't kind to that.

For SEO we need a 301 redirect but we can do that.

To sort this we need to create a custom RequireHttps attribute.

public class CustomRequireHttpsFilter : RequireHttpsAttribute
{
    protected override void HandleNonHttpsRequest(AuthorizationContext filterContext)
    {
        // If the request does not have a GET or HEAD verb then we throw it away.
        if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)
            && !String.Equals(filterContext.HttpContext.Request.HttpMethod, "HEAD", StringComparison.OrdinalIgnoreCase))
        {
            base.HandleNonHttpsRequest(filterContext);
        }

        // Update the URL to use https instead of http
        string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;

        // If it's localhost we are using IIS Express which has two different ports.
        // update the url with the IIS Express port for redirect on local machine.
        if (string.Equals(filterContext.HttpContext.Request.Url.Host, "localhost", StringComparison.OrdinalIgnoreCase))
        {
            url = "https://" + filterContext.HttpContext.Request.Url.Host + ":44363" + filterContext.HttpContext.Request.RawUrl;
        }

        // Build the httpContext Response
        // Setting a status of 301 and then telling the browser what the correct route is.
        filterContext.HttpContext.Response.StatusCode = 301;
        filterContext.HttpContext.Response.AppendHeader("Location", url);
        filterContext.HttpContext.Response.End();
    }
}

Then we make a change to our filterConfig to call our CustomerRequirehttps

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new CustomRequireHttpsFilter());         
    }
}

And that's it, all HTTP:// calls are sent into the custom Require and will return a 301 with the new URL.

Last time around I explained how to get Lets encrypt up and running on Windows Azure to catch up click here Now I have it running I need to get my sites to use HTTPS all the time and also use 301 Redirect for SEO goodness All below works as…

Read More