The Project
Be able to create and deploy a Blazor Server application using Basic Authentication with a local DB. I want to be able to deploy the application and the database together.
The Why
I create a lot of small Blazor applications, less than 1000 users, that need to have authentication and are cheap to deploy. Up to this point, I have been using Azure SQL Server. It is costing me $5.00 - $20.00 per month. I was looking for a way to cut the cost even more.
The Solution
So my great idea was to build the Blazor Server application with the basic authentication running the SQL Express database edition.
In theory, this sounds very doable, but in reality, there were several hurdles that I did not think of going into building this solution.
I will start by explaining the solution I built and then the problems I ran into.
The Solution Build
1. Create a Blazor Server Side application with basic Authentication
a. Select to use Docker and with Windows containers
2. Run it and make sure the basic application is working.
3, Create the database
a, Try to register a new user
b. Or run Update Database in the Package Manager console.
4. Create at least 1 user
5. Login with the new user and make sure they can log in
6. Now we will need to move the database file to a location local to the Blazor application.
a. Looking in the app.settings.json file for the give to your database
b. The Database file is located at /uers/yourUser
c. Move the file to a folder under www. This will make sure the deployed Blazor application can access the database file.
7. Update the connection string in app.settings.json
a. replace the name of the database with a token like Database=%%DBNAME%%
b. In the Program.cs we will need to replace the token with the actual path to the database
var connStr = builder.Configuration.GetConnectionString("DefaultConnection");
string rootpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
rootpath = $"{rootpath}\\data\\UserMaintenance.mdf";
string connectionString = connectionString.Replace("%%DBNAME%%", rootpath);
c. Now the new built connection string to passed to the DBContext Service to be used
8. Run and validate
That is it, we have a database local to our application. We just need to deploy it.
The Docker Solution
Using Docker is very straightforward. I do not want to attempt to go through the process of creating the container. There are a bunch of blog posts and YouTube videos on how to do it. Here is a
blog post to get you started.
I do include a Docker file in the GitHub source listed below.
The result of this was a working application until I tried to log in.
The Issues
Issue #1: My Docker was set up to run Linux containers, not Windows. The result was nothing the browser would display.
Fix #1: I had to run a power shell script to switch my Docker to Windows containers and reboot. Once I did that I was able to use the Counter and Fetch Data pages.
Issue #2: Unable to log in. Once I tried to log in, I received a SQL Server error. It was at this point it dawned on me, that I need a SQL Server installed.
So I jumped over to Docker Hub and found an image for SQL Server, but needed a license. That broke the cheaper rule.
Fix #2: None. I will defeat the basic authentication with a local database. I am sure If I looked hard enough I could find an image I could use. But at this point, it becomes a grey area.
Issue #3: I figured I have the application in a Docker container, why not just deploy it to see what that entails.
Turns out you can publish to different Docker registries including Azure. But that is just the image. To actually deploy and run, I have only used Kubernetes. There is no way I am going to try to set up that just for a couple of small projects.
Fix #3: Failed.
Summary
This did not appear to be a successful project from the outside and even on the inside, it is very cloudy. I think Docker is an excellent tool to use for mid and enterprise-level systems. For the smaller companies, having Azure configured correctly would seem to work well and should be cheaper.
I will be the first to admit I may be missing something on the Docker deployment path. I will continue to work with Docker, I do like it. I will continue to try and find a nice solution for a small application with Basic Authentication, so it is back to the drawing board.
Comments
Post a Comment