Skip to content

Commit 1e78fac

Browse files
committed
Adding documentation for Azure Functions isolated worker model extension
1 parent 9789e42 commit 1e78fac

File tree

4 files changed

+130
-39
lines changed

4 files changed

+130
-39
lines changed

docs/DocFx.Net.Http.WebPush/DocFx.Net.Http.WebPush.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Folder Include="wwwroot\" />
1010
</ItemGroup>
1111
<ItemGroup>
12-
<PackageReference Include="docfx.console" Version="2.59.0">
12+
<PackageReference Include="docfx.console" Version="2.59.4">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
1515
</PackageReference>

docs/DocFx.Net.Http.WebPush/articles/azure-functions-integration.md

Lines changed: 118 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,165 @@
11
# PushServiceClient bindings for Azure Functions
22

3-
The [`PushServiceClient`](../api/Lib.Net.Http.WebPush.PushServiceClient.html) bindings for Azure Functions supports input binding.
3+
The [`PushServiceClient`](../api/Lib.Net.Http.WebPush.PushServiceClient.html) extensions for Azure Functions supports input binding.
44

55
## Packages
66

7-
The [`PushServiceClient`](../api/Lib.Net.Http.WebPush.PushServiceClient.html) bindings for Azure Functions are provided in the [Lib.Azure.WebJobs.Extensions.WebPush](https://www.nuget.org/packages/Lib.Azure.WebJobs.Extensions.WebPush) NuGet package.
7+
The [`PushServiceClient`](../api/Lib.Net.Http.WebPush.PushServiceClient.html) extensions for Azure Functions are provided in the [Lib.Azure.WebJobs.Extensions.WebPush](https://www.nuget.org/packages/Lib.Azure.WebJobs.Extensions.WebPush) (in-process model) and [Lib.Azure.Functions.Worker.Extensions.WebPush](https://www.nuget.org/packages/Lib.Azure.Functions.Worker.Extensions.WebPush) (isolated worker model) NuGet packages.
88

99
```
1010
PM> Install-Package Lib.Azure.WebJobs.Extensions.WebPush
1111
```
1212

13+
```
14+
PM> Install-Package Lib.Azure.Functions.Worker.Extensions.WebPush
15+
```
16+
1317
## Input
1418

1519
The [`PushServiceClient`](../api/Lib.Net.Http.WebPush.PushServiceClient.html) input binding uses `HttpClientFactory` to retrieve [`PushServiceClient`](../api/Lib.Net.Http.WebPush.PushServiceClient.html) instance and passes it to the input parameter of the function.
1620

1721
### Input - language-specific examples
1822

1923
#### Input - C# examples
20-
In [C# class libraries](https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library), use the [`PushService`](../api/Lib.Azure.WebJobs.Extensions.WebPush.Bindings.PushServiceAttribute.html) attribute.
24+
25+
##### Isolated Worker Model
26+
In [the isolated worker model functions](https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide?tabs=windows), use the [`PushServiceInput`](../api/Lib.Azure.Functions.Worker.Extensions.WebPush.PushServiceInputAttribute.html) attribute.
2127

2228
The attribute's constructor takes the application server public key and application server private key. For information about those settings and other properties that you can configure, see [Input - configuration](#input---configuration).
2329

24-
This section contains the following examples:
25-
- [Azure Cosmos DB trigger, subscriptions from Azure Cosmos DB](#azure-cosmos-db-trigger-subscriptions-from-azure-cosmos-db)
30+
###### Azure Cosmos DB trigger, subscriptions from Azure Cosmos DB
31+
The following example shows a [C# function](https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library) that broadcasts a notification to all known subscriptions. The function is triggered by a change in Azure Cosmos DB collection and retrieves subscriptions also from Azure Cosmos DB.
32+
33+
```cs
34+
...
35+
36+
namespace Demo.Azure.Functions.Worker.PushNotifications
37+
{
38+
public class SendNotificationFunction
39+
{
40+
private readonly ILogger _logger;
41+
42+
public SendNotificationFunction(ILoggerFactory loggerFactory)
43+
{
44+
_logger = loggerFactory.CreateLogger<SendNotificationFunction>();
45+
}
2646

27-
##### Azure Cosmos DB trigger, subscriptions from Azure Cosmos DB
47+
[Function("SendNotificationFunction")]
48+
public async Task Run([CosmosDBTrigger(
49+
databaseName: "PushNotifications",
50+
containerName: "Notifications",
51+
Connection = "CosmosDBConnection",
52+
LeaseContainerName = "NotificationsLeaseCollection",
53+
CreateLeaseContainerIfNotExists = true)] IReadOnlyList<Notification> notifications,
54+
[CosmosDBInput(
55+
databaseName: "PushNotifications",
56+
containerName: "Subscriptions",
57+
Connection = "CosmosDBConnection")] CosmosClient cosmosClient,
58+
[PushServiceInput(
59+
PublicKeySetting = "ApplicationServerPublicKey",
60+
PrivateKeySetting = "ApplicationServerPrivateKey",
61+
SubjectSetting = "ApplicationServerSubject")] PushServiceClient pushServiceClient)
62+
{
63+
if (notifications != null)
64+
{
65+
Container subscriptionsContainer = cosmosClient.GetDatabase("PushNotifications").GetContainer("Subscriptions");
66+
using (FeedIterator<PushSubscription> subscriptionsIterator = subscriptionsContainer.GetItemQueryIterator<PushSubscription>())
67+
{
68+
while (subscriptionsIterator.HasMoreResults)
69+
{
70+
foreach (PushSubscription subscription in await subscriptionsIterator.ReadNextAsync())
71+
{
72+
foreach (Notification notification in notifications)
73+
{
74+
// Fire-and-forget
75+
pushServiceClient.RequestPushMessageDeliveryAsync(subscription, new PushMessage(notification.Content)
76+
{
77+
Topic = notification.Topic,
78+
TimeToLive = notification.TimeToLive,
79+
Urgency = notification.Urgency
80+
});
81+
}
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
89+
public class Notification
90+
{
91+
public string? Topic { get; set; }
92+
93+
public string Content { get; set; } = String.Empty;
94+
95+
public int? TimeToLive { get; set; }
96+
97+
public PushMessageUrgency Urgency { get; set; }
98+
}
99+
}
100+
```
101+
102+
##### In-process Model
103+
In [C# class libraries](https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library), use the [`PushService`](../api/Lib.Azure.WebJobs.Extensions.WebPush.Bindings.PushServiceAttribute.html) attribute.
104+
105+
The attribute's constructor takes the application server public key and application server private key. For information about those settings and other properties that you can configure, see [Input - configuration](#input---configuration).
106+
107+
###### Azure Cosmos DB trigger, subscriptions from Azure Cosmos DB
28108
The following example shows a [C# function](https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library) that broadcasts a notification to all known subscriptions. The function is triggered by a change in Azure Cosmos DB collection and retrieves subscriptions also from Azure Cosmos DB.
29109

30110
```cs
31-
using System;
32-
using System.Threading.Tasks;
33-
using System.Collections.Generic;
34-
using Microsoft.Azure.WebJobs;
35-
using Microsoft.Azure.Documents.Linq;
36-
using Microsoft.Azure.Documents.Client;
37-
using Lib.Net.Http.WebPush;
38-
using Lib.Azure.WebJobs.Extensions.WebPush.Bindings;
111+
...
39112

40113
namespace Demo.Azure.Funtions.PushNotifications
41114
{
42-
public static class SendNotificationFunction
115+
public class Notification
43116
{
44-
private static readonly Uri _subscriptionsCollectionUri = UriFactory.CreateDocumentCollectionUri("PushNotifications", "SubscriptionsCollection");
117+
public string Topic { get; set; }
45118

119+
public string Content { get; set; }
120+
121+
public int? TimeToLive { get; set; }
122+
123+
public PushMessageUrgency Urgency { get; set; }
124+
}
125+
126+
public static class SendNotificationFunction
127+
{
46128
[FunctionName("SendNotification")]
47129
public static async Task Run([CosmosDBTrigger(
48130
databaseName: "PushNotifications",
49-
collectionName: "NotificationsCollection",
50-
ConnectionStringSetting = "CosmosDBConnection",
51-
LeaseCollectionName = "NotificationsLeaseCollection",
52-
CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<PushMessage> notifications,
131+
containerName: "Notifications",
132+
Connection = "CosmosDBConnection",
133+
LeaseContainerName = "NotificationsLeaseCollection",
134+
CreateLeaseContainerIfNotExists = true)]IReadOnlyList<Notification> notifications,
53135
[CosmosDB(
54136
databaseName: "PushNotifications",
55-
collectionName: "SubscriptionsCollection",
56-
ConnectionStringSetting = "CosmosDBConnection")]DocumentClient cosmosDbClient,
137+
containerName: "Subscriptions",
138+
Connection = "CosmosDBConnection")]CosmosClient cosmosClient,
57139
[PushService(
58140
PublicKeySetting = "ApplicationServerPublicKey",
59141
PrivateKeySetting = "ApplicationServerPrivateKey",
60142
SubjectSetting = "ApplicationServerSubject")]PushServiceClient pushServiceClient)
61143
{
62144
if (notifications != null)
63145
{
64-
IDocumentQuery<PushSubscription> subscriptionQuery = cosmosDbClient.CreateDocumentQuery<PushSubscription>(_subscriptionsCollectionUri, new FeedOptions
146+
Container subscriptionsContainer = cosmosClient.GetDatabase("PushNotifications").GetContainer("Subscriptions");
147+
using (FeedIterator<PushSubscription> subscriptionsIterator = subscriptionsContainer.GetItemQueryIterator<PushSubscription>())
65148
{
66-
EnableCrossPartitionQuery = true,
67-
MaxItemCount = -1
68-
}).AsDocumentQuery();
69-
70-
while (subscriptionQuery.HasMoreResults)
71-
{
72-
foreach (PushSubscription subscription in await subscriptionQuery.ExecuteNextAsync())
149+
while (subscriptionsIterator.HasMoreResults)
73150
{
74-
foreach (PushMessage notification in notifications)
151+
foreach (PushSubscription subscription in await subscriptionsIterator.ReadNextAsync())
75152
{
76-
// Fire-and-forget
77-
pushServiceClient.RequestPushMessageDeliveryAsync(subscription, notification);
153+
foreach (Notification notification in notifications)
154+
{
155+
// Fire-and-forget
156+
pushServiceClient.RequestPushMessageDeliveryAsync(subscription, new PushMessage(notification.Content)
157+
{
158+
Topic = notification.Topic,
159+
TimeToLive = notification.TimeToLive,
160+
Urgency = notification.Urgency
161+
});
162+
}
78163
}
79164
}
80165
}
@@ -84,7 +169,6 @@ namespace Demo.Azure.Funtions.PushNotifications
84169
}
85170
```
86171

87-
88172
### Input - configuration
89173

90174
The following table explains the binding configuration properties that you set in the *function.json* file and the [`PushService`](../api/Lib.Azure.WebJobs.Extensions.WebPush.Bindings.PushServiceAttribute.html) attribute.

docs/DocFx.Net.Http.WebPush/docfx.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
"files": [
77
"src/Lib.Net.Http.WebPush/Lib.Net.Http.WebPush.csproj",
88
"src/Lib.AspNetCore.WebPush/Lib.AspNetCore.WebPush.csproj",
9-
"src/Lib.Azure.WebJobs.Extensions.WebPush/Lib.Azure.WebJobs.Extensions.WebPush.csproj"
9+
"src/Lib.Azure.WebJobs.Extensions.WebPush/Lib.Azure.WebJobs.Extensions.WebPush.csproj",
10+
"src/Lib.Azure.Functions.Worker.Extensions.WebPush/Lib.Azure.Functions.Worker.Extensions.WebPush.csproj"
1011
],
1112
"exclude": [ "**/bin/**", "**/obj/**" ],
1213
"src": "../.."
1314
}
1415
],
1516
"dest": "api",
1617
"properties": {
17-
"TargetFramework": "netstandard2.0"
18+
"TargetFramework": "net6.0"
1819
}
1920
}
2021
],

docs/DocFx.Net.Http.WebPush/index.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ Lib.Net.Http.WebPush is a library which provides a [*Web Push Protocol*](https:/
44

55
Lib.AspNetCore.WebPush is a library which provides ASP.NET Core extensions for Web Push Protocol based client for Push Service.
66

7-
Lib.Azure.WebJobs.Extensions.WebPush is a library which provides [Azure Functions](https://functions.azure.com/) and [Azure WebJobs](https://docs.microsoft.com/en-us/azure/app-service/web-sites-create-web-jobs) binding extensions for Web Push Protocol based client for Push Service.
7+
Lib.Azure.WebJobs.Extensions.WebPush is a library which provides [Azure Functions](https://functions.azure.com/) in-process model and [Azure WebJobs](https://docs.microsoft.com/en-us/azure/app-service/web-sites-create-web-jobs) binding extensions for Web Push Protocol based client for Push Service.
8+
9+
Lib.Azure.Functions.Worker.Extensions.WebPush is a library which provides [Azure Functions](https://functions.azure.com/) isolated worker model extensions for Web Push Protocol based client for Push Service.
810

911
## Installation
1012

11-
You can install [Lib.Net.Http.WebPush](https://www.nuget.org/packages/Lib.Net.Http.WebPush), [Lib.AspNetCore.WebPush](https://www.nuget.org/packages/Lib.AspNetCore.WebPush), and [Lib.Azure.WebJobs.Extensions.WebPush](https://www.nuget.org/packages/Lib.Azure.WebJobs.Extensions.WebPush) from NuGet.
13+
You can install [Lib.Net.Http.WebPush](https://www.nuget.org/packages/Lib.Net.Http.WebPush), [Lib.AspNetCore.WebPush](https://www.nuget.org/packages/Lib.AspNetCore.WebPush), [Lib.Azure.WebJobs.Extensions.WebPush](https://www.nuget.org/packages/Lib.Azure.WebJobs.Extensions.WebPush), and [Lib.Azure.Functions.Worker.Extensions.WebPush](https://www.nuget.org/packages/Lib.Azure.Functions.Worker.Extensions.WebPush) from NuGet.
1214

1315
```
1416
PM> Install-Package Lib.Net.Http.WebPush
@@ -22,6 +24,10 @@ PM> Install-Package Lib.AspNetCore.WebPush
2224
PM> Install-Package Lib.Azure.WebJobs.Extensions.WebPush
2325
```
2426

27+
```
28+
PM> Install-Package Lib.Azure.Functions.Worker.Extensions.WebPush
29+
```
30+
2531
## Demos
2632

2733
There are several demo projects available:

0 commit comments

Comments
 (0)