Managing State (Part 4)
Using An In-Memory State Container Service
In the context of
in-memory state containers, we utilize dependency injection to maintain the
service instance in memory for a specified duration (scoped, singleton,
transient). To show how in-memory state works, we will have to implement it a
bit differently depending on hosting.
Implementing Real-Time Updates
On Blazor Server
- In the MyBlog.Shared
project, select the Interfaces folder and press Shift + F2. Name
the file IBlogNotificationService.cs.
- In the MyBlogServerSide project, select the Services folder and press Shift+ F2. Name the file BlazorServerBlogNotificationService.cs. It might seem unnecessary to give the class a name that includes BlazorServer, but it makes sure we can easily tell the classes apart.
- In Startup.cs at the end of ConfigureServices, add the dependency injection-
Services.AddSingleton<IBlogNotificationService,
BlazorServerBlogNottificationService> ();
- We add this dependency injection as a Singleton. When using Blazor Server, this will be the same instance for ALL users, so we must be careful when we use Singleton. In this case, we want the service to notify all the visitors of our blog that the blog post has changed.
- In the MyBlog.Shared project, open Post.razor.
- Add the following code at the top (or close to the top) of the page-
@using MyBlog.Shared.Interfaces
@inject IBlogNotificationService
notificationService
@implements IDisposable
- Open the Pages/Admin/BlogPostEdit.Razor file.
- When we make changes to our blog post, we need to send a notification as well. At the top of the file, add the following-
@using MyBlog.Shared.Interfaces
@inject IBlogNotificationService
notificationService
- In the UpdateHTMLAsync method, add the following just under the Post. Text!=null if statement-
await
notificationService.SendNotification(Post);
- Right-click on MyBlogServerSide, select Set as Startup Project, and run the project by pressing Ctrl+ F5.
- Copy the URL and open another web browser. We should now have two web browser windows open showing us the blog. In the first window, open a blog post (doesn't matter which one), and in the second window, log in and edit the same blog post.
- When we change the text of the blog post in the second window, the change should be reflected in real time in the first window.
Implementing Real-Time Updates On Blazor
WebAssembly
- Right-click on the MyBlogWebAssembly.Server project, select Add new folder, and name the folder Hubs.
- Select the Hubs folder and press Shift+ F2.
Name the file BlogNotificationHub.cs.
- Replace the code.
- In the Startup.cs file at the top of the ConfigureService method, add SignalR and configure the JSON serialization to handle the entity framework.
- Add the following namespace:
using MyBlogWebAssembly.Server.Hubs;
- In app.UseEndpoints, just above endpoints. MapFallbackToFile("index.html");, add the following-
endpoints.MapHub<BlogNotificationHub>("/
BlogNotificationHub");
Here we configure what URL BlogNotificationHub should
use. In this case, we are using the same URL as the name of the hub.
- In the MyBlogWebAssembly.Client project, right-click on the Dependencies node and select Manage NuGet Packages.
- Search for Microsoft.AspNetCore.SignalR.Client and click Install. Select the Services folder and press Shift+ F2. Name the file BlazorWebAssemblyBlogNotificationService.cs. In this file, we will implement the SignalR communication.
- Add the following namespaces-
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;
using
Microsoft.Extensions.DependencyInjection;
using MyBlog.Data.Models;
using MyBlog.Shared.Interfaces;
using System;
using System.Threading.Tasks;
- In the Program.cs file, we need to configure dependency injection. Just above awaitbuilder.Build().RunAsync();, add the following-
builder.Services.AddSingleton();
- Add the following namespace-
MyBlogWebAssembly.Client.Services;
- Now it's time to carry out testing. Right-click on MyBlogWebAssembly.Server, select Set as Startup Project, and run the project by pressing Ctrl+F5.
- Copy the URL and open another web browser. We should now have two web browser windows open showing us the blog. In the first window, open a blog post (it doesn't matter which one), and in the second window, log in and edit the same blog post.
- When we change the text of the blog post in the second window, the change should be reflected in real time in the first window.
Conclusion
We effectively discovered
methods for managing state within our application and utilizing local storage
for data storage, whether encrypted or not. We explored various approaches to
accomplish this and also ensured the integration of SignalR for enabling
real-time communication with the server.
Comments
Post a Comment