Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions samples/WorkerBindingSamples/Cosmos/CosmosInputBindingFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace SampleApp
{
/// <summary>
/// Samples demonstrating binding to the <see cref="CosmosClient"/>, <see cref="Database"/>, and <see cref="Container"/> types.
/// </summary>
public class CosmosInputBindingFunctions
{
private readonly ILogger<CosmosInputBindingFunctions> _logger;
Expand All @@ -18,6 +21,11 @@ public CosmosInputBindingFunctions(ILogger<CosmosInputBindingFunctions> logger)
_logger = logger;
}

/// <summary>
/// This sample demonstrates how to retrieve a collection of documents.
/// The code uses a <see cref="CosmosClient"/> instance to read a list of documents.
/// The <see cref="CosmosClient"/> instance could also be used for write operations.
/// </summary>
[Function(nameof(DocsByUsingCosmosClient))]
public async Task<HttpResponseData> DocsByUsingCosmosClient(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
Expand All @@ -40,6 +48,11 @@ public async Task<HttpResponseData> DocsByUsingCosmosClient(
return req.CreateResponse(HttpStatusCode.OK);
}

/// <summary>
/// This sample demonstrates how to retrieve a collection of documents.
/// The function is triggered by an HTTP request and binds to the specified database.
/// as a <see cref="Database"/> type. The function then queries for all collections in the database.
/// </summary>
[Function(nameof(DocsByUsingDatabaseClient))]
public async Task<HttpResponseData> DocsByUsingDatabaseClient(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
Expand All @@ -61,6 +74,11 @@ public async Task<HttpResponseData> DocsByUsingDatabaseClient(
return req.CreateResponse(HttpStatusCode.OK);
}

/// <summary>
/// This sample demonstrates how to retrieve a collection of documents.
/// The function is triggered by an HTTP request and binds to the specified database and collection
/// as a <see cref="Container"/> type. The function then queries for all documents in the collection.
/// </summary>
[Function(nameof(DocsByUsingContainerClient))]
public async Task<HttpResponseData> DocsByUsingContainerClient(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
Expand All @@ -82,6 +100,11 @@ public async Task<HttpResponseData> DocsByUsingContainerClient(
return req.CreateResponse(HttpStatusCode.OK);
}

/// <summary>
/// This sample demonstrates how to retrieve a single document.
/// The function is triggered by an HTTP request that uses a query string to specify the ID and partition key value to look up.
/// That ID and partition key value are used to retrieve a ToDoItem document from the specified database and collection.
/// </summary>
[Function(nameof(DocByIdFromQueryString))]
public HttpResponseData DocByIdFromQueryString(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
Expand All @@ -106,6 +129,11 @@ public HttpResponseData DocByIdFromQueryString(
return req.CreateResponse(HttpStatusCode.OK);
}

/// <summary>
/// This sample demonstrates how to retrieve a single document.
/// The function is triggered by an HTTP request that uses route data to specify the ID and partition key value to look up.
/// That ID and partition key value are used to retrieve a ToDoItem document from the specified database and collection.
/// </summary>
[Function(nameof(DocByIdFromRouteData))]
public HttpResponseData DocByIdFromRouteData(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "todoitems/{partitionKey}/{id}")] HttpRequestData req,
Expand All @@ -130,6 +158,12 @@ public HttpResponseData DocByIdFromRouteData(
return req.CreateResponse(HttpStatusCode.OK);
}

/// <summary>
/// This sample demonstrates how to retrieve a collection of documents.
/// The function is triggered by an HTTP request that uses route data to specify the ID to look up.
/// That ID is used to retrieve a list of ToDoItem documents from the specified database and collection.
/// The example shows how to use a binding expression in the <see cref="CosmosDBInputAttribute.SqlQuery"/> parameter.
/// </summary>
[Function(nameof(DocByIdFromRouteDataUsingSqlQuery))]
public HttpResponseData DocByIdFromRouteDataUsingSqlQuery(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "todoitems2/{id}")] HttpRequestData req,
Expand All @@ -150,6 +184,12 @@ public HttpResponseData DocByIdFromRouteDataUsingSqlQuery(
return req.CreateResponse(HttpStatusCode.OK);
}

/// <summary>
/// This sample demonstrates how to retrieve a collection of documents.
/// The function is triggered by an HTTP request that uses a query string to specify the ID to look up.
/// That ID is used to retrieve a list of ToDoItem documents from the specified database and collection.
/// The example shows how to use a binding expression in the <see cref="CosmosDBInputAttribute.SqlQuery"/> parameter.
/// </summary>
[Function(nameof(DocByIdFromQueryStringUsingSqlQuery))]
public HttpResponseData DocByIdFromQueryStringUsingSqlQuery(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
Expand All @@ -170,6 +210,10 @@ public HttpResponseData DocByIdFromQueryStringUsingSqlQuery(
return req.CreateResponse(HttpStatusCode.OK);
}

/// <summary>
/// This sample demonstrates how to retrieve a collection of documents.
/// The function is triggered by an HTTP request. The query is specified in the <see cref="CosmosDBInputAttribute.SqlQuery"/> attribute property.
/// </summary>
[Function(nameof(DocsBySqlQuery))]
public HttpResponseData DocsBySqlQuery(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
Expand All @@ -189,6 +233,12 @@ public HttpResponseData DocsBySqlQuery(
return req.CreateResponse(HttpStatusCode.OK);
}

/// <summary>
/// This sample demonstrates how to retrieve a single document.
/// The function is triggered by a queue message that contains a JSON object. The queue trigger parses the JSON into
/// an object of type ToDoItemLookup, which contains the ID and partition key value to look up. That ID and partition
/// key value are used to retrieve a ToDoItem document from the specified database and collection.
/// </summary>
[Function(nameof(DocByIdFromJSON))]
public void DocByIdFromJSON(
[QueueTrigger("todoqueueforlookup")] ToDoItemLookup toDoItemLookup,
Expand Down
25 changes: 17 additions & 8 deletions samples/WorkerBindingSamples/Cosmos/CosmosTriggerFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,35 @@

namespace SampleApp
{
// We cannot use SDK-type bindings with the Cosmos trigger binding. There is no way for
// the CosmosDB SDK to let us know the ID of the document that triggered the function;
// therefore we cannot create a client that is able to pull the triggering document.
public static class CosmosTriggerFunction
/// <summary>
/// Samples demonstrating binding to the <see cref="IReadOnlyList{T}"/> type.
/// </summary>
public class CosmosTriggerFunction
{
private readonly ILogger<CosmosTriggerFunction> _logger;

public CosmosTriggerFunction(ILogger<CosmosTriggerFunction> logger)
{
_logger = logger;
}

/// <summary>
/// This function demonstrates binding to a collection of <see cref="ToDoItem"/>.
/// </summary>
[Function(nameof(CosmosTriggerFunction))]
public static void Run([CosmosDBTrigger(
public void Run([CosmosDBTrigger(
databaseName: "ToDoItems",
containerName:"TriggerItems",
Connection = "CosmosDBConnection",
LeaseContainerName = "leases",
CreateLeaseContainerIfNotExists = true)] IReadOnlyList<ToDoItem> todoItems,
FunctionContext context)
{
var logger = context.GetLogger(nameof(CosmosTriggerFunction));

if (todoItems is not null && todoItems.Any())
{
foreach (var doc in todoItems)
{
logger.LogInformation("ToDoItem: {desc}", doc.Description);
_logger.LogInformation("ToDoItem: {desc}", doc.Description);
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions samples/WorkerBindingSamples/EventGrid/CloudEventSamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

// Default URL for triggering event grid function in the local environment.
// http://localhost:7071/runtime/webhooks/EventGrid?functionName={functionname}

using Azure.Messaging;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace SampleApp
{
/// <summary>
/// Samples demonstrating binding to the <see cref="CloudEvent"/> type.
/// </summary>
public class CloudEventSamples
{
private readonly ILogger<CloudEventSamples> _logger;

public CloudEventSamples(ILogger<CloudEventSamples> logger)
{
_logger = logger;
}

/// <summary>
/// This function demonstrates binding to a single <see cref="CloudEvent"/>.
/// </summary>
[Function(nameof(CloudEventFunction))]
public void CloudEventFunction([EventGridTrigger] CloudEvent cloudEvent)
{
_logger.LogInformation("Event type: {type}, Event subject: {subject}", cloudEvent.Type, cloudEvent.Subject);
}

/// <summary>
/// This function demonstrates binding to an array of <see cref="CloudEvent"/>.
/// Note that when doing so, you must also set the <see cref="EventGridTriggerAttribute.IsBatched"/> property
/// to <value>true</value>.
/// </summary>
[Function(nameof(CloudEventBatchFunction))]
public void CloudEventBatchFunction([EventGridTrigger(IsBatched = true)] CloudEvent[] cloudEvents)
{
foreach (var cloudEvent in cloudEvents)
{
_logger.LogInformation("Event type: {type}, Event subject: {subject}", cloudEvent.Type, cloudEvent.Subject);
}
}
}
}
48 changes: 48 additions & 0 deletions samples/WorkerBindingSamples/EventGrid/EventGridEventSamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

// Default URL for triggering event grid function in the local environment.
// http://localhost:7071/runtime/webhooks/EventGrid?functionName={functionname}

using Azure.Messaging.EventGrid;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;

namespace SampleApp
{
/// <summary>
/// Samples demonstrating binding to the <see cref="EventGridEvent"/> type.
/// </summary>
public class EventGridEventSamples
{
private readonly ILogger<EventGridEventSamples> _logger;

public EventGridEventSamples(ILogger<EventGridEventSamples> logger)
{
_logger = logger;
}

/// <summary>
/// This function demonstrates binding to a single <see cref="EventGridEvent"/>.
/// </summary>
[Function(nameof(EventGridEventFunction))]
public void EventGridEventFunction([EventGridTrigger] EventGridEvent eventGridEvent)
{
_logger.LogInformation("Event received: {event}", eventGridEvent.Data.ToString());
}

/// <summary>
/// This function demonstrates binding to an array of <see cref="EventGridEvent"/>.
/// Note that when doing so, you must also set the <see cref="EventGridTriggerAttribute.IsBatched"/> property
/// to <value>true</value>.
/// </summary>
[Function(nameof(EventGridEventBatchFunction))]
public void EventGridEventBatchFunction([EventGridTrigger(IsBatched = true)] EventGridEvent[] eventGridEvents)
{
foreach (var eventGridEvent in eventGridEvents)
{
_logger.LogInformation("Event received: {event}", eventGridEvent.Data.ToString());
}
}
}
}

This file was deleted.

Loading