Adding basic middleware sample
This commit is contained in:
Родитель
3716246e3f
Коммит
77db60de69
|
@ -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/* && mkdir -p $(OutDir)bin/runtimes && 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 =>
|
||||
|
|
Загрузка…
Ссылка в новой задаче