Skip to content

Commit 8b92ff8

Browse files
authored
[WebPubSub] Improve README (Azure#27370)
Fix links and change to latest snippet.
1 parent 8020a56 commit 8b92ff8

File tree

2 files changed

+50
-56
lines changed

2 files changed

+50
-56
lines changed

sdk/webpubsub/Microsoft.Azure.WebPubSub.AspNetCore/README.md

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Any scenario that requires real-time publish-subscribe messaging between server
66

77
This library can be used to do the following actions. Details about the terms used here are described in [Key concepts](#key-concepts) section.
88

9-
- Parse upstream request under CloudNative CloudEvents
9+
- Parse upstream requests under CloudNative CloudEvents
1010
- Add validation options for upstream request
1111
- API to add user defined functionality to handle different upstream events
1212

1313
[Source code](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/webpubsub/Microsoft.Azure.WebPubSub.AspNetCore/src) |
14-
Package |
14+
[Package][package_ref] |
1515
[API reference documentation](https://aka.ms/awps/sdk/csharp) |
1616
[Product documentation](https://aka.ms/awps/doc) |
1717
[Samples][sample_ref] |
@@ -20,7 +20,7 @@ Package |
2020

2121
### Install the package
2222

23-
Install the client library from [NuGet](https://www.nuget.org/):
23+
Install the client library from [NuGet][package_ref]
2424

2525
```PowerShell
2626
dotnet add package Microsoft.Azure.WebPubSub.AspNetCore --prerelease
@@ -33,13 +33,30 @@ dotnet add package Microsoft.Azure.WebPubSub.AspNetCore --prerelease
3333

3434
### Authenticate the client
3535

36-
In order to interact with the service, you'll need to create an instance of the WebPubSubServiceClient class. To make this possible, you'll need the connection string or a key, which you can access in the Azure portal.
36+
In order to interact with the service, you'll need to provide the Web PubSub service with a valid credential. To make this possible, you'll need the connection string or a key, which you can access in the Azure portal. Besides, if you want to invoke service REST API, you can call `AddWebPubSubServiceClient<THub>()` where `THub` is user implemented [`WebPubSubHub`](#webpubsubhub) listening to important events.
3737

38-
### Create a `WebPubSubServiceClient`
38+
### Configure Web PubSub service options
3939

40-
```C# Snippet:WebPubSubAuthenticate
41-
// Create a WebPubSubServiceClient that will authenticate using a key credential.
42-
var serviceClient = new WebPubSubServiceClient(new Uri(endpoint), "some_hub", new AzureKeyCredential(key));
40+
```C# Snippet:WebPubSubDependencyInjection
41+
public void ConfigureServices(IServiceCollection services)
42+
{
43+
services.AddWebPubSub(o =>
44+
{
45+
o.ServiceEndpoint = new("<connection-string>");
46+
}).AddWebPubSubServiceClient<SampleHub>();
47+
}
48+
```
49+
50+
### Map `WebPubSubHub` to endpoint routing
51+
52+
```C# Snippet:WebPubSubMapHub
53+
public void Configure(IApplicationBuilder app)
54+
{
55+
app.UseEndpoints(endpoint =>
56+
{
57+
endpoint.MapWebPubSubHub<SampleHub>("/eventhandler");
58+
});
59+
}
4360
```
4461

4562
## Key concepts
@@ -56,42 +73,27 @@ For information about general Web PubSub concepts [Concepts in Azure Web PubSub]
5673
5774
## Examples
5875

59-
### Add Web PubSub service with options
76+
### Handle upstream `Connect` event
6077

61-
```C#
62-
public void ConfigureServices(IServiceCollection services)
78+
```C# Snippet:WebPubSubHubMethods
79+
private sealed class SampleHub : WebPubSubHub
6380
{
64-
services.AddWebPubSub(o =>
65-
{
66-
o.ValidationOptions.Add("<connection-string>");
67-
});
68-
}
69-
```
70-
71-
### Map `WebPubSubHub` to endpoint routing
72-
73-
The path should match the value configured in the Azure Web PubSub service `EventHandler`. For example, if placeholder is using like `/api/{event}`, then the path set in code should be `/api/{event}` as well.
81+
internal WebPubSubServiceClient<SampleHub> _serviceClient;
7482

75-
```C#
76-
public void Configure(IApplicationBuilder app)
77-
{
78-
app.UseEndpoints(endpoint =>
83+
// Need to ensure service client is injected by call `AddServiceHub<SampleHub>` in ConfigureServices.
84+
public SampleHub(WebPubSubServiceClient<SampleHub> serviceClient)
7985
{
80-
endpoint.MapWebPubSubHub<SampleHub>("/eventhander");
81-
});
82-
}
83-
```
86+
_serviceClient = serviceClient;
87+
}
8488

85-
### Handle Upstream event
86-
87-
```C#
88-
public override ValueTask<ConnectEventResponse> OnConnectAsync(ConnectEventRequest request, CancellationToken cancellationToken)
89-
{
90-
var response = new ConnectEventResponse
89+
public override ValueTask<ConnectEventResponse> OnConnectAsync(ConnectEventRequest request, CancellationToken cancellationToken)
9190
{
92-
UserId = request.ConnectionContext.UserId
93-
};
94-
return new ValueTask<ConnectEventResponse>(response);
91+
var response = new ConnectEventResponse
92+
{
93+
UserId = request.ConnectionContext.UserId
94+
};
95+
return new ValueTask<ConnectEventResponse>(response);
96+
}
9597
}
9698
```
9799

@@ -103,9 +105,7 @@ You can also easily [enable console logging](https://github.com/Azure/azure-sdk-
103105

104106
## Next steps
105107

106-
Please take a look at the
107-
[samples][samples_ref]
108-
directory for detailed examples on how to use this library.
108+
Please take a look at the [Samples][sample_ref] directory for detailed examples on how to use this library.
109109

110110
## Contributing
111111

@@ -124,3 +124,4 @@ For more information see the [Code of Conduct FAQ](https://opensource.microsoft.
124124

125125
[azure_sub]: https://azure.microsoft.com/free/dotnet/
126126
[sample_ref]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/webpubsub/Microsoft.Azure.WebPubSub.AspNetCore/tests/Samples/
127+
[package_ref]: https://www.nuget.org/packages/Microsoft.Azure.WebPubSub.AspNetCore/
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
#if NETCOREAPP3_1_OR_GREATER
4+
#if NETCOREAPP3_1_OR_GREATER || SNIPPET
55
using System.Threading;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Builder;
@@ -12,37 +12,37 @@ namespace Microsoft.Azure.WebPubSub.AspNetCore.Tests.Samples
1212
{
1313
public class WebPubSubSample
1414
{
15-
#region Snippet:WebPubSubDependencyInjection
15+
#region Snippet:WebPubSubDependencyInjection
1616
public void ConfigureServices(IServiceCollection services)
1717
{
1818
services.AddWebPubSub(o =>
1919
{
2020
o.ServiceEndpoint = new("<connection-string>");
2121
}).AddWebPubSubServiceClient<SampleHub>();
2222
}
23-
#endregion
23+
#endregion
2424

25-
#region Snippet:WebPubSubMapHub
25+
#region Snippet:WebPubSubMapHub
2626
public void Configure(IApplicationBuilder app)
2727
{
2828
app.UseEndpoints(endpoint =>
2929
{
3030
endpoint.MapWebPubSubHub<SampleHub>("/eventhandler");
3131
});
3232
}
33-
#endregion
33+
#endregion
3434

35+
#region Snippet:WebPubSubHubMethods
3536
private sealed class SampleHub : WebPubSubHub
3637
{
3738
internal WebPubSubServiceClient<SampleHub> _serviceClient;
3839

39-
// Need to ensure is injected by call `AddServiceHub<SampleHub>` in ConfigureServices.
40+
// Need to ensure service client is injected by call `AddServiceHub<SampleHub>` in ConfigureServices.
4041
public SampleHub(WebPubSubServiceClient<SampleHub> serviceClient)
4142
{
4243
_serviceClient = serviceClient;
4344
}
4445

45-
#region Snippet:WebPubSubConnectMethods
4646
public override ValueTask<ConnectEventResponse> OnConnectAsync(ConnectEventRequest request, CancellationToken cancellationToken)
4747
{
4848
var response = new ConnectEventResponse
@@ -51,15 +51,8 @@ public override ValueTask<ConnectEventResponse> OnConnectAsync(ConnectEventReque
5151
};
5252
return new ValueTask<ConnectEventResponse>(response);
5353
}
54-
#endregion
55-
56-
#region Snippet:WebPubSubDefaultMethods
57-
public override ValueTask<UserEventResponse> OnMessageReceivedAsync(UserEventRequest request, CancellationToken cancellationToken)
58-
{
59-
return base.OnMessageReceivedAsync(request, cancellationToken);
60-
}
61-
#endregion
6254
}
55+
#endregion
6356
}
6457
}
6558
#endif

0 commit comments

Comments
 (0)