Last active
September 4, 2019 05:16
-
-
Save JerryNixon/ea3b50f36e37429babb6dfee18d13242 to your computer and use it in GitHub Desktop.
How to use custom dependency injection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.ApplicationInsights; | |
using Microsoft.ApplicationInsights.Extensibility; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.Functions.Extensions.DependencyInjection; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
using Newtonsoft.Json.Linq; | |
using System.IO; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
// https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection | |
// https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring#configure-sampling | |
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2#named-clients | |
[assembly: FunctionsStartup(typeof(FunctionApp1.Startup))] | |
namespace FunctionApp1 | |
{ | |
public class Startup : FunctionsStartup | |
{ | |
public override void Configure(IFunctionsHostBuilder builder) | |
{ | |
builder.Services.AddScoped<IUtility, Utility>(); | |
} | |
} | |
public interface IUtility | |
{ | |
bool TryRead(HttpRequest request, string key, out string value); | |
IActionResult OkayResult(string value = null); | |
IActionResult BadResult(string value = null); | |
} | |
public class Utility : IUtility | |
{ | |
public bool TryRead(HttpRequest request, string key, out string value) | |
{ | |
return request.TryReadQueryString(key, out value) || request.TryReadBody(key, out value); | |
} | |
public IActionResult OkayResult(string value = null) | |
{ | |
return value == null | |
? new OkResult() | |
: (IActionResult)new OkObjectResult(value); | |
} | |
public IActionResult BadResult(string value = null) | |
{ | |
return value == null | |
? new BadRequestResult() | |
: (IActionResult)new BadRequestObjectResult(value); | |
} | |
} | |
public class MyFunction | |
{ | |
private readonly IUtility _utility; | |
public MyFunction(IUtility common) | |
{ | |
_utility = common; | |
} | |
[FunctionName("MyFunction")] | |
public async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest request, | |
ExecutionContext context, | |
ILogger log) | |
{ | |
log.LogInformation($"C# HTTP trigger function processed a request: {context.InvocationId}"); | |
if (_utility.TryRead(request, "name", out var query_value)) | |
{ | |
return _utility.OkayResult($"Hello, {query_value}."); | |
} | |
else | |
{ | |
return _utility.BadResult("Please pass a name on the query string or in the request body"); | |
} | |
} | |
} | |
public static class HttpRequestExtensions | |
{ | |
public static bool TryReadQueryString(this HttpRequest request, string key, out string value) | |
{ | |
value = request.Query.ContainsKey(key) ? request.Query[key].ToString() : null; | |
return value != null; | |
} | |
public static bool TryReadBody(this HttpRequest request, string key, out string value) | |
{ | |
var json = new StreamReader(request.Body).ReadToEndAsync().Result; | |
var jobj = JObject.Parse(json); | |
value = jobj.ContainsKey(key) ? jobj.Value<string>(key) : null; | |
return value != null; | |
} | |
} | |
} |
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.10.0" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Before you can use dependency injection, you must install the following NuGet packages: