You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -21,40 +21,26 @@ This article explains how to get started with WebSockets in ASP.NET Core. [WebSo
21
21
22
22
[ASP.NET Core SignalR](xref:signalr/introduction) is a library that simplifies adding real-time web functionality to apps. It uses WebSockets whenever possible.
23
23
24
-
For most applications, we recommend SignalR over raw WebSockets. SignalR provides transport fallback for environments where WebSockets is not available. It also provides a simple remote procedure call app model. And in most scenarios, SignalR has no significant performance disadvantage compared to using raw WebSockets.
24
+
For most applications, we recommend SignalR over raw WebSockets. SignalR provides transport fallback for environments where WebSockets is not available. It also provides a basic remote procedure call app model. And in most scenarios, SignalR has no significant performance disadvantage compared to using raw WebSockets.
25
+
26
+
For some apps, [gRPC on .NET](xref:grpc/index) provides an alternative to WebSockets.
25
27
26
28
## Prerequisites
27
29
28
-
* ASP.NET Core 1.1 or later
29
-
* Any OS that supports ASP.NET Core:
30
-
30
+
* Any OS that supports ASP.NET Core:
31
31
* Windows 7 / Windows Server 2008 or later
32
32
* Linux
33
-
* macOS
34
-
33
+
* macOS
35
34
* If the app runs on Windows with IIS:
36
-
37
35
* Windows 8 / Windows Server 2012 or later
38
36
* IIS 8 / IIS 8 Express
39
-
* WebSockets must be enabled (See the [IIS/IIS Express support](#iisiis-express-support) section.).
40
-
37
+
* WebSockets must be enabled. See the [IIS/IIS Express support](#iisiis-express-support) section.
41
38
* If the app runs on [HTTP.sys](xref:fundamentals/servers/httpsys):
42
-
43
39
* Windows 8 / Windows Server 2012 or later
44
-
45
40
* For supported browsers, see https://caniuse.com/#feat=websockets.
46
41
47
-
::: moniker range="< aspnetcore-2.1"
48
-
49
-
## NuGet package
50
-
51
-
Install the [Microsoft.AspNetCore.WebSockets](https://www.nuget.org/packages/Microsoft.AspNetCore.WebSockets/) package.
52
-
53
-
::: moniker-end
54
-
55
42
## Configure the middleware
56
43
57
-
58
44
Add the WebSockets middleware in the `Configure` method of the `Startup` class:
If you're using a background service to write data to a WebSocket, make sure you keep the middleware pipeline running. Do this by using a <xref:System.Threading.Tasks.TaskCompletionSource%601>. Pass the `TaskCompletionSource` to your background service and have it call <xref:System.Threading.Tasks.TaskCompletionSource%601.TrySetResult%2A> when you finish with the WebSocket. Then `await` the <xref:System.Threading.Tasks.TaskCompletionSource%601.Task> property during the request, as shown in the following example:
The WebSocket closed exception can also happen if you return too soon from an action method. If you accept a socket in an action method, wait for the code that uses the socket to complete before returning from the action method.
90
+
The WebSocket closed exception can also happen when returning too soon from an action method. When accepting a socket in an action method, wait for the code that uses the socket to complete before returning from the action method.
113
91
114
-
Never use `Task.Wait()`, `Task.Result`, or similar blocking calls to wait for the socket to complete, as that can cause serious threading issues. Always use `await`.
92
+
Never use `Task.Wait`, `Task.Result`, or similar blocking calls to wait for the socket to complete, as that can cause serious threading issues. Always use `await`.
0 commit comments