Quickly Convert a Console App Into a Single-Point Web App in C#
I found myself in need of quickly converting my C# Console application into a Web application, primarily, I was asked to expose our Console app’s logic through an API with very little time to manage it.
This article will cover on how I eventually exposed our Console app on port 5005 of the localhost, our internal network, for bonus, I’ll throw in the containerised Docker version of the app, too!
Let’s start with a simple console app:
mkdir ConsoleToWeb
cd ConsoleToWeb
dotnet new console
dotnet run
The source of this app will look something like this:
To convert this into a ASP.NET Web App, we need to do 3 things:
- Convert the console project into a web project (csproj)
- Introduce a Generic Host to host our Web App
- Rewrite the Main method to run our WebHost
Converting a Console csproj into a Web csproj
Our original Console csproj looks something like the following:
For a Web project, we need to target a different SDK; Microsoft.NET.Sdk.Web
Also, the OutputType does not need to be an Exe.
Modify the csproj file to look like this:
Introduce a Generic Host to host our Web App
The IHost class is a class designed for the new app hosting system of .NET. It handles core functionality like Dependency Injection, Configuration, Logging, …
We will use the HostBuilder to create a vanilla Web Host that will have 1 Run method which will call the code from our Console app.
Add the following code to your Program.cs file:
Don’t forget about the necessary using statements:
using Microsoft.Extensions.Hosting;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;
Rewrite the Main method to run our WebHost
We’re almost there, we just need to tie it all up in the Main method of the Program.cs:
Let’s examine the full working example:
That is it! Now you can run the app using dotnet run
and see the result in your browser at http://localhost:5005
Dockerize it!
Since we’re on a roll here, let’s quickly Dockerize this app so that we can package and ship it.
Add a Dockerfile to the root of the ConsoleToWeb directory.
touch Dockerfile
The contents of the Dockerfile should be something like:
Now that is done, we can build our image locally and tag it:
docker build . -t consoletoweb
After the Docker image is built, we can run the image in a container using docker run -p 5005:5005 consoletoweb
And the effect should be the same on http://localhost:5005
Quickly adding services using FuncR
Still going strong now, let’s introduce a service, add some small implementation and have that service injected into our Web App using Dependency Injection!
Introduce the IFooService:
Now, we could implement this service using a new class, or we could write just the code we need for string Foo(int numberOfFoos) method to work using a function with FuncR.
FuncR is a small .NET standard library that enables you to register functions against interfaces in C#.
First we need to add FuncR to the project
dotnet add package FuncR
To use FuncR, add a ConfigureServices method to your Program.cs file:
In this ConfigureServices method, you can register any service agains the DI container of ASP.NET, more concretely, the code above registers a Proxy for IFooService and hooks up the string Foo(int numberOfFoos) method to the function that is provided on lines 7 and 8, cool huh!?
Now we just need to tell the HostBuilder to use this method to configure its services and we’re ready to have that service injected:
If we now dotnet run
and navigate to http://localhost:5005 we finally see some different output:
That’s a lot of Foo’s.
And naturally, the Docker example also still works after a rebuild and run:
docker build . --no-cache -t consoletoweb
docker run -p 5005:5005 consoletoweb
Wrapping up
In this article we saw how easy and quickly a console app can be converted into a ASP.NET web app, and we got an introduction to FuncR to speed up our service implementations. The full code, and more is available through GitHub: