Skip to content

Commit

Permalink
[Samples] Updated the exception handling middleware sample to handle …
Browse files Browse the repository at this point in the history
…multiple output binding case (#1071)

* Update exception handling middleware sample to handle multiple output binding case

* Update SDK package to latest stable

* Add line break
  • Loading branch information
kshyju committed Sep 28, 2022
1 parent 04ccbd8 commit acffe91
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
5 changes: 3 additions & 2 deletions samples/CustomMiddleware/CustomMiddleware.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.8.0-preview1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.9.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.5.0-preview1" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="5.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
23 changes: 21 additions & 2 deletions samples/CustomMiddleware/ExceptionHandlingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
Expand Down Expand Up @@ -44,10 +45,28 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
// https://github.com/Azure/azure-functions-dotnet-worker/issues/776
await newHttpResponse.WriteAsJsonAsync(new { FooStatus = "Invocation failed!" }, newHttpResponse.StatusCode);

// Update invocation result.
context.GetInvocationResult().Value = newHttpResponse;
var invocationResult = context.GetInvocationResult();

var httpOutputBindingFromMultipleOutputBindings = GetHttpOutputBindingFromMultipleOutputBinding(context);
if (httpOutputBindingFromMultipleOutputBindings is not null)
{
httpOutputBindingFromMultipleOutputBindings.Value = newHttpResponse;
}
else
{
invocationResult.Value = newHttpResponse;
}
}
}
}

private OutputBindingData<HttpResponseData> GetHttpOutputBindingFromMultipleOutputBinding(FunctionContext context)
{
// The output binding entry name will be "$return" only when the function return type is HttpResponseData
var httpOutputBinding = context.GetOutputBindings<HttpResponseData>()
.FirstOrDefault(b => b.BindingType == "http" && b.Name != "$return");

return httpOutputBinding;
}
}
}
8 changes: 7 additions & 1 deletion samples/CustomMiddleware/HttpFunction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
Expand All @@ -10,10 +11,15 @@ namespace CustomMiddleware
{
public class HttpFunction
{
[Function("HttpFunction")]
[Function(nameof(HttpFunction))]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req,
FunctionContext context)
{
if (req.Url.Query.Contains("throw-exception"))
{
throw new Exception("App code failed");
}

var logger = context.GetLogger<HttpFunction>();

// Get the item set by the middleware
Expand Down
39 changes: 39 additions & 0 deletions samples/CustomMiddleware/HttpTriggerWithMultipleOutputBindings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Net;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace CustomMiddleware
{
public static class HttpTriggerWithMultipleOutputBindings
{
[Function(nameof(HttpTriggerWithMultipleOutputBindings))]
public static MyOutputType Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
{
if (req.Url.Query.Contains("throw-exception"))
{
throw new Exception("App code failed");
}

var response = req.CreateResponse(HttpStatusCode.OK);
response.WriteString("Success!");

return new MyOutputType()
{
Name = "Azure Functions",
HttpResponse = response
};
}
}

public class MyOutputType
{
[QueueOutput("functionstesting2", Connection = "AzureWebJobsStorage")]
public string Name { get; set; }

public HttpResponseData HttpResponse { get; set; }
}
}

0 comments on commit acffe91

Please sign in to comment.