Adding basic middleware sample

This commit is contained in:
Fabio Cavalcante 2021-01-20 11:57:30 -08:00
Родитель 3716246e3f
Коммит 77db60de69
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 0DB1E924635A0D75
4 изменённых файлов: 48 добавлений и 2 удалений

Просмотреть файл

@ -6,7 +6,7 @@
<OutputType>Exe</OutputType>
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>
<Target Name="CopyRuntimes" AfterTargets="AfterBuild" Condition=" '$(OS)' == 'UNIX' ">
<Target Name="CopyRuntimes" AfterTargets="AfterBuild" Condition=" '$(OS)' == 'UNIX' ">
<!-- To workaround a bug where the files aren't copied correctly for non-Windows platforms -->
<Exec Command="rm -rf $(OutDir)bin/runtimes/* &amp;&amp; mkdir -p $(OutDir)bin/runtimes &amp;&amp; cp -R $(OutDir)runtimes/* $(OutDir)bin/runtimes/" />
</Target>
@ -17,7 +17,7 @@
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.3" />
<PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.0.0-preview1" OutputItemType="Analyzer"/>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.0.0-preview1" OutputItemType="Analyzer" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.0.0-preview1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />

Просмотреть файл

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace FunctionApp.Middleware
{
public static class ApplicationBuilderExtensions
{
public static IFunctionsWorkerApplicationBuilder UseSampleMiddleware(this IFunctionsWorkerApplicationBuilder builder)
{
builder.Services.AddSingleton<SampleMiddleware>();
builder.Use(next =>
{
return context =>
{
var middleware = context.InstanceServices.GetRequiredService<SampleMiddleware>();
return middleware.Invoke(context, next);
};
});
return builder;
}
}
}

Просмотреть файл

@ -0,0 +1,14 @@
using Microsoft.Azure.Functions.Worker.Pipeline;
using System.Threading.Tasks;
namespace FunctionApp
{
public class SampleMiddleware
{
public Task Invoke(FunctionExecutionContext context, FunctionExecutionDelegate next)
{
context.Items.Add("Greeting", "Hello from our middleware");
return next(context);
}
}
}

Просмотреть файл

@ -4,6 +4,7 @@ using System.Diagnostics;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Azure.Functions.Worker.Configuration;
using FunctionApp.Middleware;
namespace FunctionApp
{
@ -21,6 +22,7 @@ namespace FunctionApp
})
.ConfigureFunctionsWorker((c, b) =>
{
b.UseSampleMiddleware();
b.UseFunctionExecutionMiddleware();
})
.ConfigureServices(s =>