Skip to content

Commit

Permalink
Adding documentation for Azure Functions isolated worker model extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tpeczek committed Sep 4, 2024
1 parent d1dc036 commit 6b97a95
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 39 deletions.
2 changes: 1 addition & 1 deletion docs/DocFx.Net.Http.WebPush/DocFx.Net.Http.WebPush.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="docfx.console" Version="2.59.0">
<PackageReference Include="docfx.console" Version="2.59.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
152 changes: 118 additions & 34 deletions docs/DocFx.Net.Http.WebPush/articles/azure-functions-integration.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,165 @@
# PushServiceClient bindings for Azure Functions

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

## Packages

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.
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.

```
PM> Install-Package Lib.Azure.WebJobs.Extensions.WebPush
```

```
PM> Install-Package Lib.Azure.Functions.Worker.Extensions.WebPush
```

## Input

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.

### Input - language-specific examples

#### Input - C# examples
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.

##### Isolated Worker Model
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.

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).

This section contains the following examples:
- [Azure Cosmos DB trigger, subscriptions from Azure Cosmos DB](#azure-cosmos-db-trigger-subscriptions-from-azure-cosmos-db)
###### Azure Cosmos DB trigger, subscriptions from Azure Cosmos DB
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.

```cs
...

namespace Demo.Azure.Functions.Worker.PushNotifications
{
public class SendNotificationFunction
{
private readonly ILogger _logger;

public SendNotificationFunction(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<SendNotificationFunction>();
}

##### Azure Cosmos DB trigger, subscriptions from Azure Cosmos DB
[Function("SendNotificationFunction")]
public async Task Run([CosmosDBTrigger(
databaseName: "PushNotifications",
containerName: "Notifications",
Connection = "CosmosDBConnection",
LeaseContainerName = "NotificationsLeaseCollection",
CreateLeaseContainerIfNotExists = true)] IReadOnlyList<Notification> notifications,
[CosmosDBInput(
databaseName: "PushNotifications",
containerName: "Subscriptions",
Connection = "CosmosDBConnection")] CosmosClient cosmosClient,
[PushServiceInput(
PublicKeySetting = "ApplicationServerPublicKey",
PrivateKeySetting = "ApplicationServerPrivateKey",
SubjectSetting = "ApplicationServerSubject")] PushServiceClient pushServiceClient)
{
if (notifications != null)
{
Container subscriptionsContainer = cosmosClient.GetDatabase("PushNotifications").GetContainer("Subscriptions");
using (FeedIterator<PushSubscription> subscriptionsIterator = subscriptionsContainer.GetItemQueryIterator<PushSubscription>())
{
while (subscriptionsIterator.HasMoreResults)
{
foreach (PushSubscription subscription in await subscriptionsIterator.ReadNextAsync())
{
foreach (Notification notification in notifications)
{
// Fire-and-forget
pushServiceClient.RequestPushMessageDeliveryAsync(subscription, new PushMessage(notification.Content)
{
Topic = notification.Topic,
TimeToLive = notification.TimeToLive,
Urgency = notification.Urgency
});
}
}
}
}
}
}
}

public class Notification
{
public string? Topic { get; set; }

public string Content { get; set; } = String.Empty;

public int? TimeToLive { get; set; }

public PushMessageUrgency Urgency { get; set; }
}
}
```

##### In-process Model
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.

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).

###### Azure Cosmos DB trigger, subscriptions from Azure Cosmos DB
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.

```cs
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.Documents.Linq;
using Microsoft.Azure.Documents.Client;
using Lib.Net.Http.WebPush;
using Lib.Azure.WebJobs.Extensions.WebPush.Bindings;
...

namespace Demo.Azure.Funtions.PushNotifications
{
public static class SendNotificationFunction
public class Notification
{
private static readonly Uri _subscriptionsCollectionUri = UriFactory.CreateDocumentCollectionUri("PushNotifications", "SubscriptionsCollection");
public string Topic { get; set; }

public string Content { get; set; }

public int? TimeToLive { get; set; }

public PushMessageUrgency Urgency { get; set; }
}

public static class SendNotificationFunction
{
[FunctionName("SendNotification")]
public static async Task Run([CosmosDBTrigger(
databaseName: "PushNotifications",
collectionName: "NotificationsCollection",
ConnectionStringSetting = "CosmosDBConnection",
LeaseCollectionName = "NotificationsLeaseCollection",
CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<PushMessage> notifications,
containerName: "Notifications",
Connection = "CosmosDBConnection",
LeaseContainerName = "NotificationsLeaseCollection",
CreateLeaseContainerIfNotExists = true)]IReadOnlyList<Notification> notifications,
[CosmosDB(
databaseName: "PushNotifications",
collectionName: "SubscriptionsCollection",
ConnectionStringSetting = "CosmosDBConnection")]DocumentClient cosmosDbClient,
containerName: "Subscriptions",
Connection = "CosmosDBConnection")]CosmosClient cosmosClient,
[PushService(
PublicKeySetting = "ApplicationServerPublicKey",
PrivateKeySetting = "ApplicationServerPrivateKey",
SubjectSetting = "ApplicationServerSubject")]PushServiceClient pushServiceClient)
{
if (notifications != null)
{
IDocumentQuery<PushSubscription> subscriptionQuery = cosmosDbClient.CreateDocumentQuery<PushSubscription>(_subscriptionsCollectionUri, new FeedOptions
Container subscriptionsContainer = cosmosClient.GetDatabase("PushNotifications").GetContainer("Subscriptions");
using (FeedIterator<PushSubscription> subscriptionsIterator = subscriptionsContainer.GetItemQueryIterator<PushSubscription>())
{
EnableCrossPartitionQuery = true,
MaxItemCount = -1
}).AsDocumentQuery();

while (subscriptionQuery.HasMoreResults)
{
foreach (PushSubscription subscription in await subscriptionQuery.ExecuteNextAsync())
while (subscriptionsIterator.HasMoreResults)
{
foreach (PushMessage notification in notifications)
foreach (PushSubscription subscription in await subscriptionsIterator.ReadNextAsync())
{
// Fire-and-forget
pushServiceClient.RequestPushMessageDeliveryAsync(subscription, notification);
foreach (Notification notification in notifications)
{
// Fire-and-forget
pushServiceClient.RequestPushMessageDeliveryAsync(subscription, new PushMessage(notification.Content)
{
Topic = notification.Topic,
TimeToLive = notification.TimeToLive,
Urgency = notification.Urgency
});
}
}
}
}
Expand All @@ -84,7 +169,6 @@ namespace Demo.Azure.Funtions.PushNotifications
}
```


### Input - configuration

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.
Expand Down
5 changes: 3 additions & 2 deletions docs/DocFx.Net.Http.WebPush/docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
"files": [
"src/Lib.Net.Http.WebPush/Lib.Net.Http.WebPush.csproj",
"src/Lib.AspNetCore.WebPush/Lib.AspNetCore.WebPush.csproj",
"src/Lib.Azure.WebJobs.Extensions.WebPush/Lib.Azure.WebJobs.Extensions.WebPush.csproj"
"src/Lib.Azure.WebJobs.Extensions.WebPush/Lib.Azure.WebJobs.Extensions.WebPush.csproj",
"src/Lib.Azure.Functions.Worker.Extensions.WebPush/Lib.Azure.Functions.Worker.Extensions.WebPush.csproj"
],
"exclude": [ "**/bin/**", "**/obj/**" ],
"src": "../.."
}
],
"dest": "api",
"properties": {
"TargetFramework": "netstandard2.0"
"TargetFramework": "net6.0"
}
}
],
Expand Down
10 changes: 8 additions & 2 deletions docs/DocFx.Net.Http.WebPush/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ Lib.Net.Http.WebPush is a library which provides a [*Web Push Protocol*](https:/

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

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.
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.

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.

## Installation

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.
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.

```
PM> Install-Package Lib.Net.Http.WebPush
Expand All @@ -22,6 +24,10 @@ PM> Install-Package Lib.AspNetCore.WebPush
PM> Install-Package Lib.Azure.WebJobs.Extensions.WebPush
```

```
PM> Install-Package Lib.Azure.Functions.Worker.Extensions.WebPush
```

## Demos

There are several demo projects available:
Expand Down

0 comments on commit 6b97a95

Please sign in to comment.