@@ -6,12 +6,12 @@ Any scenario that requires real-time publish-subscribe messaging between server
66
77This 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
2626dotnet 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/
0 commit comments