Skip to content

Commit e8c40cc

Browse files
authored
[WebPubSub]Add isolated-process extension for WebPubSub (Azure#34618)
* Add isolated-process extension for WebPubSub * clean changelog * export API. * fix changelog. * uniform version. * update README and minor fixes. * property fix. * re-factor samples * Add allowed product slug and link fix. * minor description fix. * fix link * Revert eng changes, need separate PR. * Fix cached name would be mixed in the same function. * update sample products tag. * Support polymorphic serialization in output. * Sync to latest extension version. * update sample * Sync sample to fix CI.
1 parent 9164bfd commit e8c40cc

File tree

60 files changed

+2326
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2326
-1
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
<PackageReference Update="Microsoft.AspNetCore.Http" Version="2.1.22" />
152152
<PackageReference Update="Microsoft.AspNetCore.Http.Connections" Version="1.0.15" />
153153
<PackageReference Update="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
154+
<PackageReference Update="Microsoft.Azure.Functions.Worker.Extensions.Abstractions" Version="1.1.0" />
154155
<PackageReference Update="Microsoft.Extensions.Azure" Version="1.6.3" />
155156
<PackageReference Update="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.1.0" />
156157
<PackageReference Update="Microsoft.Extensions.Configuration" Version="2.1.0" />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Release History
2+
3+
## 1.5.0-beta.1 (Unreleased)
4+
5+
### Features Added
6+
7+
### Breaking Changes
8+
9+
### Bugs Fixed
10+
11+
### Other Changes
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<!--
3+
Add any shared properties you want for the projects under this package directory that need to be set before the auto imported Directory.Build.props
4+
-->
5+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., Directory.Build.props))\Directory.Build.props" />
6+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33213.308
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Azure.Functions.Worker.Extensions.WebPubSub", "src\Microsoft.Azure.Functions.Worker.Extensions.WebPubSub.csproj", "{8FD378FF-7228-4A8D-AB9B-65D60A386E94}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{8FD378FF-7228-4A8D-AB9B-65D60A386E94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{8FD378FF-7228-4A8D-AB9B-65D60A386E94}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{8FD378FF-7228-4A8D-AB9B-65D60A386E94}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{8FD378FF-7228-4A8D-AB9B-65D60A386E94}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {6A1FF741-58C3-4C13-A0D5-54902BE0CC44}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# Azure Web PubSub extension of isolated-process Azure Functions client library for .NET
2+
3+
This extension defines the binding types and triggers in the .NET isolated worker process for Azure Functions, allowing you to write functions that respond to any event published to Web PubSub.
4+
5+
[Source code][source] |
6+
Package |
7+
API reference documentation |
8+
[Product documentation](https://aka.ms/awps/doc) |
9+
[Samples]
10+
11+
## Getting started
12+
13+
### Install the package
14+
15+
Install the client library from [NuGet]:
16+
17+
```dotnetcli
18+
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.WebPubSub
19+
```
20+
21+
### Prerequisites
22+
23+
You must have an [Azure subscription][free_subs] and an Azure resource group with a Web PubSub resource. Follow this [step-by-step tutorial][tutorial] to create an Azure Web PubSub instance.
24+
25+
### Authenticate the client
26+
27+
In order to let the extension work with Azure Web PubSub service, you will need to provide a valid `ConnectionString`.
28+
29+
You can find the **Keys** for you Azure Web PubSub service in the [Azure Portal][portal].
30+
31+
The `AzureWebJobsStorage` connection string is used to preserve the processing checkpoint information as required refer to [Storage considerations][storage]
32+
33+
For the local development use the `local.settings.json` file to store the connection string, `<connection-string>` can be set to `WebPubSubConnectionString` as default supported in the extension, or you can set customized names by mapping it with `Connection = <connection-string>` in function binding attributes:
34+
35+
```json
36+
{
37+
"Values": {
38+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
39+
"<connection-string>": "Endpoint=https://<webpubsub-name>.webpubsub.azure.com;AccessKey=<access-key>;Version=1.0;"
40+
}
41+
}
42+
```
43+
When deployed use the [application settings][app_setting] to set the connection string.
44+
45+
## Key concepts
46+
47+
### Using Web PubSub input binding
48+
49+
Please follow the [input binding tutorial](#functions-that-uses-web-pubsub-input-binding) to learn about using this extension for building `WebPubSubConnection` to create Websockets connection to service with input binding.
50+
51+
### Using Web PubSub output binding
52+
53+
Please follow the [output binding tutorial](#functions-that-uses-web-pubsub-output-binding) to learn about using this extension for publishing Web PubSub messages.
54+
55+
### Using Web PubSub trigger
56+
57+
Please follow the [trigger binding tutorial](#functions-that-uses-web-pubsub-trigger) to learn about triggering an Azure Function when an event is sent from service upstream.
58+
59+
In `Connect` and `UserEvent` events, function will respect return values to send back service. Then service will depend on the response to proceed the request or else. The responses and events are paired. For example, `Connect` will only respect `ConnectEventResponse` or `EventErrorResponse`, and ignore other returns. When `EventErrorResponse` is returned, service will drop client connection.
60+
61+
## Examples
62+
63+
### Functions that uses Web PubSub input binding
64+
65+
Use `WebPubSubConnectionInput` to build the client negotiate URL.
66+
67+
```C# Snippet:WebPubSubConnectionInputFunction
68+
[Function("Negotiate")]
69+
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req,
70+
[WebPubSubConnectionInput(Hub = "<web_pubsub_hub>", Connection = "<web_pubsub_connection_name>")] WebPubSubConnection connectionInfo)
71+
{
72+
var response = req.CreateResponse(HttpStatusCode.OK);
73+
response.WriteAsJsonAsync(connectionInfo);
74+
return response;
75+
}
76+
```
77+
78+
Use `WebPubSubContextInput` to read Web PubSub request under `HttpTrigger`. This is useful when work with Static Web Apps which supports `HttpTrigger` functions only.
79+
80+
```C# Snippet:WebPubSubContextInputFunction
81+
// validate method when upstream set as http://<func-host>/api/{event}
82+
[Function("validate")]
83+
public static HttpResponseData Validate(
84+
[HttpTrigger(AuthorizationLevel.Anonymous, "options")] HttpRequestData req,
85+
[WebPubSubContextInput] WebPubSubContext wpsReq)
86+
{
87+
return BuildHttpResponseData(req, wpsReq.Response);
88+
}
89+
90+
// Respond AbuseProtection to put header correctly.
91+
private static HttpResponseData BuildHttpResponseData(HttpRequestData request, SimpleResponse wpsResponse)
92+
{
93+
var response = request.CreateResponse();
94+
response.StatusCode = (HttpStatusCode)wpsResponse.Status;
95+
response.Body = response.Body;
96+
foreach (var header in wpsResponse.Headers)
97+
{
98+
response.Headers.Add(header.Key, header.Value);
99+
}
100+
return response;
101+
}
102+
```
103+
104+
### Functions that uses Web PubSub output binding
105+
106+
```C# Snippet:WebPubSubOutputFunction
107+
[Function("Notification")]
108+
[WebPubSubOutput(Hub = "<web_pubsub_hub>", Connection = "<web_pubsub_connection_name>")]
109+
public static WebPubSubAction Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
110+
{
111+
return new SendToAllAction
112+
{
113+
Data = BinaryData.FromString($"Hello SendToAll."),
114+
DataType = WebPubSubDataType.Text
115+
};
116+
}
117+
```
118+
119+
### Functions that uses Web PubSub trigger
120+
121+
```C# Snippet:WebPubSubTriggerUserEventFunction
122+
[Function("Broadcast")]
123+
public static UserEventResponse Run(
124+
[WebPubSubTrigger("<web_pubsub_hub>", WebPubSubEventType.User, "message")] UserEventRequest request)
125+
{
126+
return new UserEventResponse("[SYSTEM ACK] Received.");
127+
}
128+
```
129+
130+
For more details see the [samples README][samples].
131+
132+
## Troubleshooting
133+
134+
Please refer to [Monitor Azure Functions][monitor] for troubleshooting guidance.
135+
136+
## Next steps
137+
138+
Read the [introduction to Azure Function][func_intro] or [creating an Azure Function guide][create].
139+
140+
## Contributing
141+
142+
See our [CONTRIBUTING.md][contrib] for details on building,
143+
testing, and contributing to this library.
144+
145+
This project welcomes contributions and suggestions. Most contributions require
146+
you to agree to a Contributor License Agreement (CLA) declaring that you have
147+
the right to, and actually do, grant us the rights to use your contribution. For
148+
details, visit [cla.microsoft.com][cla].
149+
150+
This project has adopted the [Microsoft Open Source Code of Conduct][coc].
151+
For more information see the [Code of Conduct FAQ][coc_faq]
152+
or contact [[email protected]][coc_contact] with any
153+
additional questions or comments.
154+
155+
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-net%2Fsdk%2Fsearch%2FMicrosoft.Azure.Functions.Worker.Extensions.WebPubSub%2FREADME.png)
156+
157+
<!-- LINKS -->
158+
<!-- TODO: update to sdk main branch after initial merge -->
159+
[source]: https://github.com/JialinXin/azure-sdk-for-net/tree/awps/isolated-func/sdk/webpubsub/Microsoft.Azure.Functions.Worker.Extensions.WebPubSub/src
160+
<!-- [package] TODO: add after initial release -->
161+
<!-- [api_docs] TODO: add after initial release -->
162+
<!-- TODO: update to sdk main branch after initial merge -->
163+
[samples]: https://github.com/JialinXin/azure-sdk-for-net/tree/awps/isolated-func/sdk/webpubsub/Microsoft.Azure.Functions.Worker.Extensions.WebPubSub/samples
164+
[nuget]: https://www.nuget.org/
165+
[free_subs]: https://azure.microsoft.com/free/dotnet/
166+
[portal]: https://portal.azure.com/
167+
[tutorial]: https://learn.microsoft.com/azure/azure-web-pubsub/howto-develop-create-instance
168+
[storage]: https://learn.microsoft.com/azure/azure-functions/storage-considerations#storage-account-requirements
169+
[app_setting]: https://learn.microsoft.com/azure/azure-functions/functions-how-to-use-azure-function-app-settings
170+
[monitor]: https://learn.microsoft.com/azure/azure-functions/functions-monitoring
171+
[func_intro]: https://learn.microsoft.com/azure/azure-functions/functions-overview
172+
[create]: https://learn.microsoft.com/azure/azure-functions/functions-overview
173+
174+
[contrib]: https://github.com/Azure/azure-sdk-for-net/tree/main/CONTRIBUTING.md
175+
[cla]: https://cla.microsoft.com
176+
[coc]: https://opensource.microsoft.com/codeofconduct/
177+
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/
178+
[coc_contact]: mailto:[email protected]

0 commit comments

Comments
 (0)