Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Middleware sample to Net9 #2921

Closed
wants to merge 2 commits into from
Closed
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
70 changes: 61 additions & 9 deletions .gitignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer gitignore changes in a separate PR.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

## custom additions in azure-functions-dotnet-worker repo:
Azure.Functions.Cli
azurite

Expand All @@ -12,6 +11,17 @@ azurite
# ignore protobuf
src/protos

.DS_Store

# Entity framework sample
Migrations/
local.settings.json
/tools/localpack.ps1
/.vscode
/.idea

## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore

# User-specific files
*.rsuser
*.suo
Expand All @@ -32,6 +42,7 @@ mono_crash.*
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
Expand Down Expand Up @@ -70,6 +81,9 @@ project.lock.json
project.fragment.lock.json
artifacts/

# ASP.NET Scaffolding
ScaffoldingReadMe.txt

# StyleCop
StyleCopReport.xml

Expand All @@ -87,6 +101,8 @@ StyleCopReport.xml
*.pgc
*.pgd
*.rsp
# but not Directory.Build.rsp, as it configures directory-level build defaults
!Directory.Build.rsp
*.sbr
*.tlb
*.tli
Expand All @@ -95,6 +111,7 @@ StyleCopReport.xml
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
Expand Down Expand Up @@ -146,6 +163,11 @@ _TeamCity*
.axoCover/*
!.axoCover/settings.json

# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info

# Visual Studio code coverage results
*.coverage
*.coveragexml
Expand Down Expand Up @@ -293,6 +315,17 @@ node_modules/
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw

# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp

# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp

# Visual Studio 6 technical files
*.ncb
*.aps

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
Expand Down Expand Up @@ -349,6 +382,9 @@ ASALocalRun/
# Local History for Visual Studio
.localhistory/

# Visual Studio History (VSHistory) files
.vshistory/

# BeatPulse healthcheck temp database
healthchecksdb

Expand All @@ -358,10 +394,26 @@ MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/

.DS_Store
# Entity framework sample
Migrations/
local.settings.json
/tools/localpack.ps1
/.vscode
/.idea
# Fody - auto-generated XML schema
FodyWeavers.xsd

# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# Local History for Visual Studio Code
.history/

# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp

# JetBrains Rider
*.sln.iml
20 changes: 11 additions & 9 deletions samples/CustomMiddleware/CustomMiddleware.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep the previous indentation style for this file (2 spaces, not 4)

<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="6.6.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.18.1" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.5.0" />
</ItemGroup>

</Project>
4 changes: 1 addition & 3 deletions samples/CustomMiddleware/ExceptionHandlingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
{
// Create an instance of HttpResponseData with 500 status code.
var newHttpResponse = httpReqData.CreateResponse(HttpStatusCode.InternalServerError);
// You need to explicitly pass the status code in WriteAsJsonAsync method.
// https://github.com/Azure/azure-functions-dotnet-worker/issues/776
await newHttpResponse.WriteAsJsonAsync(new { FooStatus = "Invocation failed!" }, newHttpResponse.StatusCode);
await newHttpResponse.WriteAsJsonAsync(new { FooStatus = "Invocation failed!" });

var invocationResult = context.GetInvocationResult();

Expand Down
4 changes: 2 additions & 2 deletions samples/CustomMiddleware/HttpFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace CustomMiddleware
public class HttpFunction
{
[Function(nameof(HttpFunction))]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req,
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req,
FunctionContext context)
{
if (req.Url.Query.Contains("throw-exception"))
Expand All @@ -34,7 +34,7 @@ public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "g
context.Items.Add("functionitem", "Hello from function!");

response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.WriteString("Welcome to .NET 6!!");
await response.WriteStringAsync("Welcome to .NET 6!!");

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

namespace CustomMiddleware
{
public static class HttpTriggerWithMultipleOutputBindings
public class HttpTriggerWithMultipleOutputBindings
{
[Function(nameof(HttpTriggerWithMultipleOutputBindings))]
public static MyOutputType Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
public async Task<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!");
await response.WriteStringAsync("Success!");

return new MyOutputType()
{
Expand Down
45 changes: 18 additions & 27 deletions samples/CustomMiddleware/Program.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Linq;
using CustomMiddleware;
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;

namespace CustomMiddleware
{
public class Program
{
public static void Main()
{
//<docsnippet_middleware_register>
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(workerApplication =>
{
// Register our custom middlewares with the worker
//<docsnippet_middleware_register>
var builder = FunctionsApplication.CreateBuilder(args);

builder.ConfigureFunctionsWebApplication();

workerApplication.UseMiddleware<ExceptionHandlingMiddleware>();
// Register our custom middlewares with the worker

builder.UseMiddleware<ExceptionHandlingMiddleware>();
builder.UseWhen<StampHttpHeaderMiddleware>((context) =>
{
// We want to use this middleware only for http trigger invocations.
return context.FunctionDefinition.InputBindings.Values
.First(a => a.Type.EndsWith("Trigger")).Type == "httpTrigger";
});

workerApplication.UseMiddleware<MyCustomMiddleware>();
var host = builder.Build();
//</docsnippet_middleware_register>
host.Run();

workerApplication.UseWhen<StampHttpHeaderMiddleware>((context) =>
{
// We want to use this middleware only for http trigger invocations.
return context.FunctionDefinition.InputBindings.Values
.First(a => a.Type.EndsWith("Trigger")).Type == "httpTrigger";
});
})
.Build();
//</docsnippet_middleware_register>
host.Run();
}
}
}
2 changes: 1 addition & 1 deletion samples/CustomMiddleware/local.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"AzureWebJobsStorage": ""
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
}
}