Skip to content

Commit 2c4283a

Browse files
authored
Update local chatroom to 3.1 (#122)
1 parent 3755fa4 commit 2c4283a

File tree

6 files changed

+43
-37
lines changed

6 files changed

+43
-37
lines changed
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.2</TargetFramework>
3+
<TargetFramework>netcoreapp3.1</TargetFramework>
44
<UserSecretsId>chatroom</UserSecretsId>
55
<RootNamespace>Microsoft.Azure.SignalR.Samples.ChatRoom</RootNamespace>
66
</PropertyGroup>
77

88
<ItemGroup>
99
<Folder Include="wwwroot\" />
1010
</ItemGroup>
11-
12-
<ItemGroup>
13-
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" />
14-
</ItemGroup>
1511
</Project>

samples/ChatRoomLocal/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using Microsoft.AspNetCore;
54
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Hosting;
66

77
namespace Microsoft.Azure.SignalR.Samples.ChatRoom
88
{
99
public class Program
1010
{
1111
public static void Main(string[] args)
1212
{
13-
CreateWebHostBuilder(args).Build().Run();
13+
CreateHostBuilder(args).Build().Run();
1414
}
1515

16-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
17-
WebHost.CreateDefaultBuilder(args)
18-
.UseStartup<Startup>();
16+
public static IHostBuilder CreateHostBuilder(string[] args) =>
17+
Host.CreateDefaultBuilder(args)
18+
.ConfigureWebHostDefaults(webBuilder =>
19+
{
20+
webBuilder.UseStartup<Startup>();
21+
});
1922
}
2023
}

samples/ChatRoomLocal/Properties/launchSettings.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:5000/",
7-
"sslPort": 0
6+
"applicationUrl": "http://localhost:56058",
7+
"sslPort": 44395
88
}
99
},
1010
"profiles": {
@@ -18,10 +18,10 @@
1818
"ChatRoomLocal": {
1919
"commandName": "Project",
2020
"launchBrowser": true,
21+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
2122
"environmentVariables": {
2223
"ASPNETCORE_ENVIRONMENT": "Development"
23-
},
24-
"applicationUrl": "http://localhost:5000/"
24+
}
2525
}
2626
}
2727
}

samples/ChatRoomLocal/README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Let's implement this feature step by step.
2424
dotnet new web
2525
```
2626
27-
> Before you start, make sure you installed the latest [.NET Core 2.1 SDK](https://www.microsoft.com/net/download/dotnet-core/sdk-2.1.300).
27+
> Before you start, make sure you installed the latest [.NET Core 3.1 SDK](https://dotnet.microsoft.com/download/dotnet-core/3.1).
2828
2929
2. Create a `Chat.cs` that defines a `Chat` hub class.
3030
@@ -45,7 +45,7 @@ Let's implement this feature step by step.
4545
}
4646
```
4747
48-
> SignalR SDK is already *included* in `Microsoft.AspNetCore.App` package reference in ChatRoomLocal.csproj file.
48+
> SignalR feature is already *available* as part of the `Microsoft.AspNetCore.App` shared framework.
4949
5050
Hub is the core concept in SignalR which exposes a set of methods that can be called from clients. Here we define two methods: `Broadcast()` which broadcasts the message to all clients and `Echo()` which sends the message back to the caller.
5151
@@ -59,30 +59,35 @@ Let's implement this feature step by step.
5959
services.AddSignalR();
6060
}
6161
62-
public void Configure(IApplicationBuilder app)
62+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6363
{
6464
...
65-
app.UseSignalR(routes =>
65+
app.UseRouting();
66+
app.UseEndpoints(endpoints =>
6667
{
67-
routes.MapHub<Chat>("/chat");
68+
endpoints.MapHub<Chat>("/chat");
6869
});
70+
...
6971
}
7072
```
7173
72-
> Make sure you remove `app.Run(...)` from `Configure()`, which will always give you a Hello World page.
74+
> Make sure you remove `endpoints.MapGet("/", async context =>...` from inside `app.UseEndpoint` in method `Configure()`, which will always give you a Hello World page.
7375
7476
The key changes here are `AddSignalR()` which initializes the SignalR runtime and `MapHub()` which maps the hub to the `/chat` endpoint so clients can access the hub using this url.
7577
7678
4. The last step is to create the UI of the chat room. In this sample, we will use HTML and Javascript to build a web application:
7779
7880
Copy the HTML and script files from [wwwroot](wwwroot/) of the sample project to the `wwwroot` folder of your project.
79-
Add the following code to `Startup.cs` to make the application serve the pages:
81+
Add the following code to `Startup.cs` above `app.UseRouting()` to make the application serve the pages:
8082
8183
```cs
8284
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
8385
{
8486
...
85-
app.UseFileServer();
87+
app.UseDefaultFiles();
88+
app.UseStaticFiles();
89+
app.UseRouting();
90+
...
8691
}
8792
```
8893

samples/ChatRoomLocal/Startup.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,35 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using Microsoft.AspNetCore.Builder;
5-
using Microsoft.Extensions.Configuration;
5+
using Microsoft.AspNetCore.Hosting;
66
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
78

89
namespace Microsoft.Azure.SignalR.Samples.ChatRoom
910
{
1011
public class Startup
1112
{
12-
public Startup(IConfiguration configuration)
13-
{
14-
Configuration = configuration;
15-
}
16-
17-
public IConfiguration Configuration { get; }
18-
13+
// This method gets called by the runtime. Use this method to add services to the container.
14+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
1915
public void ConfigureServices(IServiceCollection services)
2016
{
21-
services.AddMvc();
2217
services.AddSignalR();
2318
}
2419

25-
public void Configure(IApplicationBuilder app)
20+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
21+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2622
{
27-
app.UseMvc();
28-
app.UseFileServer();
29-
app.UseSignalR(routes =>
23+
if (env.IsDevelopment())
24+
{
25+
app.UseDeveloperExceptionPage();
26+
}
27+
28+
app.UseDefaultFiles();
29+
app.UseStaticFiles();
30+
app.UseRouting();
31+
app.UseEndpoints(endpoints =>
3032
{
31-
routes.MapHub<Chat>("/chat");
33+
endpoints.MapHub<Chat>("/chat");
3234
});
3335
}
3436
}

samples/ChatRoomLocal/wwwroot/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ <h2 class="text-center" style="margin-top: 0; padding-top: 30px; padding-bottom:
3535
</div>
3636
<!--Script references. -->
3737
<!--Reference the SignalR library. -->
38-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@aspnet/signalr@1.0.0/dist/browser/signalr.min.js"></script>
38+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@microsoft/signalr@3.1.0/dist/browser/signalr.min.js"></script>
3939

4040
<!--Add script to update the page and send messages.-->
4141
<script type="text/javascript">

0 commit comments

Comments
 (0)