Adding a Functions Filter sample to SampleHost
This commit is contained in:
Родитель
44838547da
Коммит
21e27cd8ed
|
@ -0,0 +1,24 @@
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Azure.WebJobs.Host;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace SampleHost.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// Sample exception filter that shows how declarative error handling logic
|
||||
/// can be integrated into the execution pipeline.
|
||||
/// </summary>
|
||||
public class ErrorHandlerAttribute : FunctionExceptionFilterAttribute
|
||||
{
|
||||
public override Task OnExceptionAsync(FunctionExceptionContext exceptionContext, CancellationToken cancellationToken)
|
||||
{
|
||||
// custom error handling logic could be written here
|
||||
// (e.g. write a queue message, send a notification, etc.)
|
||||
|
||||
exceptionContext.Logger.LogError($"ErrorHandler called. Function '{exceptionContext.FunctionName}:{exceptionContext.FunctionInstanceId} failed.");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Azure.WebJobs.Host;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SampleHost.Models;
|
||||
|
||||
namespace SampleHost.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// Sample invocation filter that demonstrates how declarative validation logic
|
||||
/// can be integrated into the execution pipeline.
|
||||
/// </summary>
|
||||
public class WorkItemValidatorAttribute : FunctionInvocationFilterAttribute
|
||||
{
|
||||
public override Task OnExecutingAsync(FunctionExecutingContext executingContext, CancellationToken cancellationToken)
|
||||
{
|
||||
executingContext.Logger.LogInformation("WorkItemValidator executing...");
|
||||
|
||||
var workItem = executingContext.Arguments.First().Value as WorkItem;
|
||||
string errorMessage = null;
|
||||
if (!TryValidateWorkItem(workItem, out errorMessage))
|
||||
{
|
||||
executingContext.Logger.LogError(errorMessage);
|
||||
throw new ValidationException(errorMessage);
|
||||
}
|
||||
|
||||
return base.OnExecutingAsync(executingContext, cancellationToken);
|
||||
}
|
||||
|
||||
private static bool TryValidateWorkItem(WorkItem workItem, out string errorMessage)
|
||||
{
|
||||
errorMessage = null;
|
||||
|
||||
if (string.IsNullOrEmpty(workItem.ID))
|
||||
{
|
||||
errorMessage = "ID cannot be null or empty.";
|
||||
return false;
|
||||
}
|
||||
if (workItem.Priority > 100 || workItem.Priority < 0)
|
||||
{
|
||||
errorMessage = "Priority must be between 0 and 100";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,9 +4,12 @@
|
|||
using System;
|
||||
using Microsoft.Azure.WebJobs;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SampleHost.Filters;
|
||||
using SampleHost.Models;
|
||||
|
||||
namespace SampleHost
|
||||
{
|
||||
[ErrorHandler]
|
||||
public static class Functions
|
||||
{
|
||||
public static void BlobTrigger(
|
||||
|
@ -24,10 +27,11 @@ namespace SampleHost
|
|||
Console.WriteLine($"Poison blob: {container}/{blobName}");
|
||||
}
|
||||
|
||||
public static void QueueTrigger(
|
||||
[QueueTrigger("test")] string message)
|
||||
[WorkItemValidator]
|
||||
public static void ProcessWorkItem(
|
||||
[QueueTrigger("test")] WorkItem workItem)
|
||||
{
|
||||
Console.WriteLine("Processed message: " + message);
|
||||
Console.WriteLine($"Processed work item {workItem.ID}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
namespace SampleHost.Models
|
||||
{
|
||||
public class WorkItem
|
||||
{
|
||||
public string ID { get; set; }
|
||||
public int Priority { get; set; }
|
||||
public string Region { get; set; }
|
||||
public int Category { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@ namespace SampleHost
|
|||
var config = new JobHostConfiguration();
|
||||
config.Queues.VisibilityTimeout = TimeSpan.FromSeconds(15);
|
||||
config.Queues.MaxDequeueCount = 3;
|
||||
config.LoggerFactory = new LoggerFactory().AddConsole();
|
||||
|
||||
if (config.IsDevelopment)
|
||||
{
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
{
|
||||
"ConnectionStrings": {
|
||||
"AzureWebJobsDashboard": "Your connection string here",
|
||||
"AzureWebJobsStorage": "Your connection string here"
|
||||
}
|
||||
}
|
|
@ -26,11 +26,6 @@ namespace Microsoft.Azure.WebJobs.Host
|
|||
throw new ArgumentNullException(nameof(functionName));
|
||||
}
|
||||
|
||||
if (logger == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
if (properties == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(properties));
|
||||
|
|
Загрузка…
Ссылка в новой задаче