Skip to content

Commit 3755fa4

Browse files
authored
Copy current chatrooms to a netcore21 folder (#123)
* Copy current chatrooms to a netcore22 folder * Use netcore21 as it is lts * resolve comments
1 parent 140236b commit 3755fa4

25 files changed

+1009
-15
lines changed

README.md

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,10 @@ More advanced samples are listed as below:
2727
* [Realtime Sign-in Example using Azure SignalR Service](samples/RealtimeSignIn)
2828
* [Flight Map: Realtime Monitoring Dashboard using Azure SignalR Service](samples/FlightMap)
2929

30+
***Archived*** Chat Room Example under AspNetCore 2.1 version:
31+
* [Chat Room Example under AspNetCore 2.1.0](samples/ChatRoom.NetCore21/ChatRoom/README.md)
32+
3033
## ASP.NET SignalR samples
3134

3235
* [Get Started with ASP.NET SignalR: a Chat Room Example](aspnet-samples/ChatRoomLocal)
3336
* [Build Your First Azure SignalR Service Application for ASP.NET SignalR](aspnet-samples/ChatRoom)
34-
35-
## Updates of Azure SignalR Service Runtime and SDK
36-
37-
We have made some changes to Azure SignalR Service Runtime, as well as Service SDK. You have to upgrade to the latest Service SDK to connect to the latest Service Runtime.
38-
39-
### SDK for ASP.NET Core SignalR
40-
In the latest Service SDK for **ASP.NET Core SignalR**, we have made the following changes:
41-
42-
- Dependency injection is built on top of ASP.NET Core SignalR.
43-
- Expose a built-in authentication endpoint to issue access token. User authentication endpoint is not required any more.
44-
- Re-design interfaces for REST API.
45-
- Remove .NET Framework support. We plan to support .NET Framework in later releases.
46-
47-
### SDK for ASP.NET SignalR
48-
The first preview version of Service SDK for **ASP.NET SignalR** is released.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bin/
2+
obj/
3+
.vs/
4+
**.csproj.user
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
<UserSecretsId>chatroom</UserSecretsId>
5+
<RootNamespace>Microsoft.Azure.SignalR.Samples.ChatRoom</RootNamespace>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Folder Include="wwwroot\" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" />
14+
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.*" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
FROM microsoft/dotnet:2.1-sdk-stretch AS build-env
5+
WORKDIR /app
6+
7+
# copy csproj and restore as distinct layers
8+
RUN mkdir ChatRoom && cd ChatRoom/
9+
COPY *.csproj ./
10+
RUN dotnet restore
11+
12+
# copy everything else and build
13+
COPY ./ ./
14+
RUN dotnet publish -c Release -o out
15+
16+
# build runtime image
17+
FROM microsoft/dotnet:2.1-aspnetcore-runtime
18+
WORKDIR /app
19+
COPY --from=build-env /app/out .
20+
ENTRYPOINT ["dotnet", "ChatRoom.dll"]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.AspNetCore.SignalR;
5+
6+
namespace Microsoft.Azure.SignalR.Samples.ChatRoom
7+
{
8+
public class ChatSampleHub : Hub
9+
{
10+
public void BroadcastMessage(string name, string message)
11+
{
12+
Clients.All.SendAsync("broadcastMessage", name, message);
13+
}
14+
15+
public void Echo(string name, string message)
16+
{
17+
Clients.Client(Context.ConnectionId).SendAsync("echo", name, message + " (echo from server)");
18+
}
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.AspNetCore;
5+
using Microsoft.AspNetCore.Hosting;
6+
7+
namespace Microsoft.Azure.SignalR.Samples.ChatRoom
8+
{
9+
public class Program
10+
{
11+
public static void Main(string[] args)
12+
{
13+
CreateWebHostBuilder(args).Build().Run();
14+
}
15+
16+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
17+
WebHost.CreateDefaultBuilder(args)
18+
.UseStartup<Startup>();
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:5000/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"ChatRoom": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:5000/"
25+
}
26+
}
27+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Build Your First Azure SignalR Service Application
2+
3+
In [ChatRoomLocal sample](../ChatRoomLocal/README.md) you have learned how to use SignalR to build a chat room application. In that sample, the SignalR runtime (which manages the client connections and message routing) is running on your local machine. As the number of the clients increases, you'll eventually hit a limit on your machine and you'll need to scale up your machine to handle more clients. This is usually not an easy task. In this tutorial, you'll learn how to use Azure SignalR Service to offload the connection management part to the service so that you don't need to worry about the scaling problem.
4+
5+
## Provision a SignalR Service
6+
7+
First let's provision a SignalR service on Azure.
8+
> If you don't have an Azure subscription, **[start now](https://azure.microsoft.com/en-us/free/)** to create a free account.
9+
10+
1. Open Azure portal, click "Create a resource" and search "SignalR Service".
11+
12+
![signalr-4](../../../docs/images/signalr-4.png)
13+
14+
2. Navigate to "SignalR Service" and click "Create".
15+
16+
![signalr-5](../../../docs/images/signalr-5.png)
17+
18+
3. Fill in basic information including resource name, resource group and location.
19+
20+
![signalr-2](../../../docs/images/signalr-2.png)
21+
22+
Resource name will also be used as the DNS name of your service endpoint. So you'll get a `<resource_name>.service.signalr.net` that your application can connect to.
23+
24+
Select a pricing tier. There're two pricing tiers:
25+
26+
* Free: which can handle 20 connections at the same time and can send and receive 20,000 messages in a day.
27+
* Standard: which has 1000 concurrent connections and one million messages per day limit for *one unit*. You can scale up to 100 units for a single service instance and you'll be charged by the number of units you use.
28+
29+
4. Click "Create", your SignalR service will be created in a few minutes.
30+
31+
![signalr-3](../../../docs/images/signalr-3.png)
32+
33+
After your service is ready, go to the **Keys** page of your service instance and you'll get two connection strings that your application can use to connect to the service.
34+
35+
## Update Chat Room to Use Azure SignalR Service
36+
37+
Then, let's update the chat room sample to use the new service you just created.
38+
39+
Let's look at the key changes:
40+
41+
1. In [Startup.cs](Startup.cs), instead of calling `AddSignalR()` and `UseSignalR()`, you need to call `AddAzureSignalR()` and `UseAzureSignalR()` and pass in connection string to make the application connect to the service instead of hosting SignalR by itself.
42+
43+
```cs
44+
public void ConfigureServices(IServiceCollection services)
45+
{
46+
...
47+
services.AddSignalR()
48+
.AddAzureSignalR();
49+
}
50+
51+
public void Configure(IApplicationBuilder app)
52+
{
53+
...
54+
app.UseAzureSignalR(routes =>
55+
{
56+
routes.MapHub<Chat>("/chat");
57+
});
58+
}
59+
```
60+
61+
You also need to reference the service SDK before using these APIs. This is how that would look in your ChatRoom.csproj file:
62+
63+
```xml
64+
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.*" />
65+
```
66+
67+
Other than these changes, everything else remains the same, you can still use the hub interface you're already familiar with to write business logic.
68+
69+
> Under the hood, an endpoint `/chat/negotiate` is exposed for negotiation by Azure SignalR Service SDK. It will return a special negotiation response when clients try to connect and redirect clients to service endpoint from the connection string. Read more details about the redirection at SignalR's [Negotiation Protocol](https://github.com/aspnet/SignalR/blob/master/specs/TransportProtocols.md#post-endpoint-basenegotiate-request).
70+
71+
72+
Now set the connection string in the [Secret Manager](https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-2.1&tabs=visual-studio#secret-manager) tool for .NET Core, and run this app.
73+
74+
```
75+
dotnet restore
76+
dotnet user-secrets set Azure:SignalR:ConnectionString "<your connection string>"
77+
dotnet run
78+
```
79+
80+
When you open http://localhost:5000, you can see the application runs as usual, just instead of hosting a SignalR runtime by itself, it connects to the SignalR service running on Azure.
81+
82+
In this sample, you have learned how to use Azure SignalR Service to replace your self-hosted SignalR runtime. But you still need a web server to host your hub logic. In the next tutorial you'll learn how to use other Azure services to host your hub logic so you can get everything running in the cloud.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace Microsoft.Azure.SignalR.Samples.ChatRoom
9+
{
10+
public class Startup
11+
{
12+
public Startup(IConfiguration configuration)
13+
{
14+
Configuration = configuration;
15+
}
16+
17+
public IConfiguration Configuration { get; }
18+
19+
public void ConfigureServices(IServiceCollection services)
20+
{
21+
services.AddMvc();
22+
services.AddSignalR()
23+
.AddAzureSignalR();
24+
}
25+
26+
public void Configure(IApplicationBuilder app)
27+
{
28+
app.UseMvc();
29+
app.UseFileServer();
30+
app.UseAzureSignalR(routes =>
31+
{
32+
routes.MapHub<ChatSampleHub>("/chat");
33+
});
34+
}
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"Debug": {
5+
"LogLevel": {
6+
"Default": "Debug"
7+
}
8+
},
9+
"Console": {
10+
"LogLevel": {
11+
"Default": "Debug"
12+
}
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)