In this tutorial I will show you how to develop a simple cross-platform Electron application from server-side Blazor. The application retrieves data from the Northwind database and renders results in a table.
The solution also allows you to do the following:
- export data to a CSV file
- setup the solution as a separate desktop application
Source code : https://github.com/medhatelmasry/ElectronServerBlazorEf
Companion Video: https://youtu.be/WxfnWqwJPlI
What is Electron?
Electron is a framework that supports development of apps using web technologies such as Chromium rendering engine and Node.js runtime. The platform supports Windows, MacOS and Linux. Some very popular applications that run on Electron are Visual Studio Code, Discord, Skype, GitHub Desktop and many others. The official site for Electron is https://www.electronjs.org/.
What is Electron.NET?
Running a docker container with SQL-Server Northwind sample database
Docker image: | kcornwall/sqlnorthwind |
Container Name (--name): | nw |
Ports (-p): | Port 1433 in container is exposed as port 1444 on the host computer |
Password: | The sa password is Passw0rd2018. This was determined from the Docker Hub page for the image. |
-d: | Starts the container in detached mode |
Setup our application
At the time of writing this article, I was using .NET version 5.0.101 on a Windows 10 computer running version 1909
Let us create an ASP.NET server-side Blazor app named ElectronServerBlazorEf with the following terminal window commands:
mkdir ElectronServerBlazorEfcd ElectronServerBlazorEfdotnet new blazorserver
We need two .NET tools. Run the following commands from within a terminal window to install ElectronNET.CLI and the dotnet-ef:
dotnet tool install -g ElectronNET.CLIdotnet tool install -g dotnet-ef
Continue by adding these packages to your project:
dotnet add package Microsoft.EntityFrameworkCore.Designdotnet add package Microsoft.EntityFrameworkCore.SqlServerdotnet add package Microsoft.EntityFrameworkCore.Toolsdotnet add package ElectronNET.API
ElectronNET.API is the Electron.NET package.
Finally, let's open our project in VS Code. To do that, simply execute the following command from the same terminal window:code .
Open Program.cs in the editor and add the following statements to the CreateHostBuilder() method right before webBuilder.UseStartup<Startup>() :
webBuilder.UseElectron(args);webBuilder.UseEnvironment("Development");
Add the following method to Startup.cs:
public async void ElectronBootstrap() {
var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions
{
Width = 1152,
Height = 940,
Show = false
});
await browserWindow.WebContents.Session.ClearCacheAsync();
browserWindow.OnReadyToShow += () => browserWindow.Show();
browserWindow.SetTitle("Electron.NET with Blazor!");
}
Add the following code to the bottom of the Configure() method in Startup.cs:
if (HybridSupport.IsElectronActive) {
ElectronBootstrap();
}
That's it. Your ASP.NET application is now electron-ized. To see the fruits of your labor, type the following command in the terminal window:
electronize initelectronize start
electronize init is a one-time command that creates a manifest file named electron.manifest.json and adds it to your project.
electronize start launches the Electron app. Note that it takes a little longer the first time and the content now appears in an application window, not a browser.
Interacting with the Northwind database
Close the Electron app with File >> Exit.
Let us reverse engineer the database with the following command so that it generates a DbContext class and classes representing the Category & Product database entities in a folder named NW:
dotnet-ef dbcontext scaffold "Data Source=localhost,1444;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=Passw0rd2018" Microsoft.EntityFrameworkCore.SqlServer -c NorthwindContext -o NW --table Products --table Categories
Add the following connection string to the top of appsettings.json just before "Logging":
"ConnectionStrings": { "NW": "Data Source=localhost,1444;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=Passw0rd2018"},
Open NW/NorthwindContext.cs and delete the OnConfiguring() method so that we do not have confidential connection string information embedded in source code.
Add the following to ConfigureServices() method in Startup.cs:
services.AddDbContext<NorthwindContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("NW"));});
Add a class file named NorthwindService.cs in the Data folder. Replace the class definition with the following code:
public class NorthwindService { private readonly NorthwindContext _context; public NorthwindService(NorthwindContext context) { _context = context; } public async Task<List<object>> GetCategoriesByProductAsync () { var query = _context.Products .Include (c => c.Category) .GroupBy (p => p.Category.CategoryName) .Select (g => new { Name = g.Key, Count = g.Count () }) .OrderByDescending(cp => cp.Count);
return await query.ToListAsync<object> (); }}
We need to configure the NorthwindService class as scoped so that we can use dependency injection. Add the following statement to the ConfigureServices() method in Startup.cs:
// Scoped creates an instance for each user
services.AddScoped<NorthwindService>();
Make a duplicate copy of the FetchData.razor file in the Pages node and name the new file Report.razor. Replace its contents with the following code:
@page "/report"@inject ElectronServerBlazorEf.Data.NorthwindService service
<h1>Categories by product</h1>
@if (data == null) { <p><em>Loading...</em></p>} else { <table class='table table-hover'> <thead> <tr> <th>Category</th> <th># of products</th> </tr> </thead> <tbody> @foreach (var item in data) { <tr> <td>@item.GetType().GetProperty("Name").GetValue(item)</td> <td>@item.GetType().GetProperty("Count").GetValue(item)</td> </tr> } </tbody> </table>}
@code { List<object> data;
protected override async Task OnInitializedAsync() { data = await service.GetCategoriesByProductAsync(); }}
Let us add a menu item to the left-side navigation of our Blazor application. Open Shared/NavMenu.razor in the editor and add the following <li> to the <ul> block (around line 24):
<li class="nav-item px-3"> <NavLink class="nav-link" href="report"> <span class="oi oi-list-rich" aria-hidden="true"></span> Report </NavLink></li>
Type in the following command in a terminal window to test the Electron.NET / Blazor application:
electronize start
Click on the Report menu item on the left-side. You should see the following output:
Click on the Report menu item on the left-side. You should see the following output:
Save data to file system as CSV file
Menu customization
// necessary when routing parameter includes a .endpoints.MapFallbackToPage("/saveas/{filepath}", "/_Host");
Build for specific platform:
Exit the application with File >> Exit.
You can produce a setup application for Windows, macOS & Linux. To generate the setup application for Windows, execute the following command from a terminal window:
electronize build /target win /PublishReadyToRun false The result is a setup application located in bin/Desktop that you can distribute. Be patient because it takes time to generate.
If you run the setup exe file, it will install a desktop application on your computer that you can easily uninstall.I hope you found this article useful and hope you build great Electron.NET / Server-side Blazor apps.
Reference: https://blog.jetbrains.com/dotnet/2020/11/05/run-blazor-apps-within-electron-shell/
The result is a setup application located in bin/Desktop that you can distribute. Be patient because it takes time to generate.
If you run the setup exe file, it will install a desktop application on your computer that you can easily uninstall.
I hope you found this article useful and hope you build great Electron.NET / Server-side Blazor apps.
Reference:
No comments:
Post a Comment