Just like ChatRoom sample, you have learned how to use Azure SignalR Service to build a chat room application. This sample demonstrates a chat room with more general features in a chat application. In this tutorial, you'll learn how to use Azure Storage Service and Azure Web App Service to get everything running in the cloud.
Now the sample supports:
- Broadcast
- Joined / Left Broadcast
- New message notification
- Send to User in a private chat room
- Replay history messages
We support the dependency injection (DI) software design pattern for the storage of messages and sessions.
Sometimes we don't need to store data in a real database (For example, in a test environment.)
You should register the service in the app's Startup.ConfigureServices
method.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<ISessionHandler, InMemorySessionStorage>();
services.AddSingleton<IMessageHandler, InMemoryMessageStorage>();
}
If you don't have an Azure Storage Account, start now to create one for your project.
In Startup.ConfigureServices
method, instead of registering InMemorySessionStorage
and InMemoryMessageStorage
, you need to use AzureTableSessionStorage
and AzureTableMessageStorage
and pass in connection string to make the application connect to the service.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<ISessionHandler, AzureTableSessionStorage>();
services.AddSingleton<IMessageHandler, AzureTableMessageStorage>();
}
Now set the connection string in the Secret Manager tool for .NET Core, and run this app.
dotnet restore
dotnet user-secrets set ConnectionStrings:AzureStorage "<Your Azure Storage's connection string>"
dotnet user-secrets set Azure:SignalR:ConnectionString "<Your Azure SignalR's connection string>"
dotnet run
You can also deploy this sample via existing docker image
docker run -e Azure__SignalR__ConnectionString="<signalr-connection-string>" \
-e STORAGE_CONN_STRING="<storage-connection-string>" \
-p 5000:80 mcr.microsoft.com/signalrsamples/reliablechatroom:latest
If you want to use your own database to store the messages and sessions, you should create a class which implements ISessionHandler and another one implementing IMessageHandler.
Then, register your services in Startup.ConfigureServices
like above and run the app
When you open http://localhost:5000, you can see the application using the configured storage services.
-
Create a web app service in Azure Create an ASP.NET Core web app in Azure
-
Go to your web app's Configuration page.
Add your Azure Storage's connection string AzureStorage
.
Similarly, add your Azure SignalR's connection string as Azure__SignalR__ConnectionString
.
Then restart your web app.
Congratulations! Your chat room web app is running live in Azure App Service.