Web API with .NET 6 with EF Core SQLite Plus Azure Web App Deployment
My new career focus of being a coding tutor (now that I have retired from developer roles) means I need to learn new stuff, but, just enough to tutor my students.
Web API is one of those things I teach constantly. In fact, teaching people how to build simple API is how I have made tons of money. touch wood and god’s blessings, hope it continues.
This also means, now that a new version of .NET 6 is here, the way to build APIs will be same, but also comes with several changes. So, I decided to build and deploy a simple web api with .NET 6.
As always, the full code is available, here.
And, let me make a note of a few things that I noticed.
Startup.cs is gone!
That’s the first thing. Startup.cs is no more. You can obviously still use it. .NET is always backward compatible.
But, yes, Startup.cs is gone now.
In it’s place, you now have, Program.cs.
All Roads begin from CreateBuilder
So, startup.cs has a lot of things. Most of the things are still here, but they are used differently. Now, everything starts with this.
var builder = WebApplication.CreateBuilder(args);
For instance, including the db context, looks something like this.
builder.Services.AddDbContext<KaijuDBContext>(options =>options.UseSqlite("Data Source=kaijusman.db"));
Additional Notes about EF Core SQLite plus Azure Web App
One of the things that always bummed me out is making sure that when deployed, the API works just fine.
First up, remove the developer environment stuff.
if (app.Environment.IsDevelopment()){ app.UseSwagger(); app.UseSwaggerUI();}
When you deploy, the environment changes to ‘release’ and swagger stops loading. To make swagger show up, move things out, like this.
app.UseSwagger();app.UseSwaggerUI();
Second up, ensure SQLite DB is created and seeded. Many times, DB creation fails after deployment. When running locally, you have control over these things. Deployment, not so much.
You can do something like this. (I am sorry about the loss of formatting. medium does not retain formatting on pasting)
public void CreateDbIfNotExists(WebApplication app){using (var scope = app.Services.CreateScope()){var services = scope.ServiceProvider;try{var context = services.GetRequiredService<KaijuDBContext>();DbInitializer.Initialize(context);}catch (Exception ex){var logger = services.GetRequiredService<ILogger<Program>>();logger.LogError(ex, "An error occurred creating the DB.");}}}
Of course, make sure you call this from your Program.cs
var tempOtherStuff = new OtherStuff();tempOtherStuff.CreateDbIfNotExists(app);
Third up, Use static files to show some nice landing page. If you dont do this, when you deploy, Azure will give a error page. You might mistake this to think that your deployment failed.
The truth is, your swagger is at /swagger/index.html. So, your app is definitely deployed. However, .NET 6 does not include a landing page by default. You may wish to avoid this problem.
You can load up your landing page with simple HTML and CSS by using something like this.
app.UseStaticFiles();
and ensure that you have a folder called wwwroot. Put your index.html and connected files in that. Easy.
Final Note
So, there it is. A simple Web Api with .NET with EF Core with SQLite plus notes about deploying the app to a Azure Web App.
I work as a full time freelance coding tutor. Hire me at UpWork or Fiverr or Stack Overflow. My personal website is here. Find more of my art at Behance and Unsplash.