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

Migrating Azure Functions sample to isolated worker model #420

Merged
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
5 changes: 3 additions & 2 deletions samples/azure/azure-function/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Find the Dockerfile at this URL
# https://github.com/Azure/azure-functions-docker/blob/dev/host/4/bullseye/amd64/dotnet/dotnet-inproc/dotnet.Dockerfile

FROM mcr.microsoft.com/azure-functions/dotnet:4.0 AS base
FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4-dotnet-isolated8.0 AS base
WORKDIR /home/site/wwwroot

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
COPY ["src/", "data/src/"]
COPY ["scripts/", "data/scripts/"]
COPY ["samples/", "data/samples/"]
Expand All @@ -21,3 +21,4 @@ RUN dotnet publish "samples/azure/azure-function/function.csproj" -c Release -o
FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .
ENV ASPNETCORE_CONTENTROOT=/home/site/wwwroot
15 changes: 8 additions & 7 deletions samples/azure/azure-function/GetCarbonIntensity.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GSF.CarbonAware.Handlers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
Expand All @@ -14,17 +14,18 @@ namespace function
public class GetCarbonIntensity
{
private readonly IEmissionsHandler _handler;
private readonly ILogger<GetCarbonIntensity> _log;

public GetCarbonIntensity(IEmissionsHandler handler)
public GetCarbonIntensity(IEmissionsHandler handler, ILogger<GetCarbonIntensity> log)
{
this._handler = handler;
this._log = log;
}


[FunctionName("GetAverageCarbonIntensity")]
[Function("GetAverageCarbonIntensity")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
{
//Get the startDate, endDate, and location from the request query if the values are present in the query
string startDate = req.Query["startdate"];
Expand All @@ -47,7 +48,7 @@ public async Task<IActionResult> Run(
try
{
var result = await _handler.GetAverageCarbonIntensityAsync(location, DateTimeOffset.Parse(startDate), DateTimeOffset.Parse(endDate));
log.LogInformation($"For location {location} Starting at: {startDate} Ending at: {endDate} the Average Emissions Rating is: {result}.");
_log.LogInformation($"For location {location} Starting at: {startDate} Ending at: {endDate} the Average Emissions Rating is: {result}.");

return new OkObjectResult(result);
}
Expand Down
13 changes: 7 additions & 6 deletions samples/azure/azure-function/GetForecast.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GSF.CarbonAware.Handlers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
Expand All @@ -15,16 +15,17 @@ namespace CarbonAwareFunctions
public class GetForecast
{
private readonly IForecastHandler _handler;
private readonly ILogger<GetForecast> _log;

public GetForecast(IForecastHandler handler)
public GetForecast(IForecastHandler handler, ILogger<GetForecast> log)
{
this._handler = handler;
this._log = log;
}

[FunctionName("GetCurrentForecast")]
[Function("GetCurrentForecast")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
{
//Get the startDate, endDate, location, and duration from the request query if the values are present in the query
string startDate = req.Query["startdate"];
Expand Down
24 changes: 24 additions & 0 deletions samples/azure/azure-function/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using GSF.CarbonAware.Configuration;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using System.IO;

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureAppConfiguration((context, builder) => {
var env = context.HostingEnvironment;
builder.AddJsonFile(Path.Combine(env.ContentRootPath, "appsettings.json"), optional: true, reloadOnChange: false)
.AddJsonFile(Path.Combine(env.ContentRootPath, $"appsettings.{env.EnvironmentName}.json"), optional: true, reloadOnChange: false)
.AddEnvironmentVariables();
})
.ConfigureServices((context,services) => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddEmissionsServices(context.Configuration);
services.AddForecastServices(context.Configuration);
})
.Build();

host.Run();
27 changes: 9 additions & 18 deletions samples/azure/azure-function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,20 @@ will use.

The Carbon Aware SDK is included in the function .csproj file by
[creating and adding the SDK as a package](../../docs/packaging.md#included-scripts).
The [Startup.cs](./Startup.cs) file uses dependency injection to access the
The [Program.cs](./Program.cs) file uses dependency injection to access the
handlers in the library. The following code initializes the C# Library:

```C#
public override void Configure(IFunctionsHostBuilder builder)
{
var configuration = builder.GetContext().Configuration;
builder.Services
.AddEmissionsServices(configuration)
.AddForecastServices(configuration);
}
// omitted
.ConfigureServices((context,services) => {
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.AddEmissionsServices(context.Configuration);
services.AddForecastServices(context.Configuration);
})
// omitted
```

> Note as the in-process
> [Azure Function uses dependency injection](https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection)
> though via
> [Microsoft.Azure.Functions.Extensions](https://www.nuget.org/packages/Microsoft.Azure.Functions.Extensions/)
> there is a version conflict of
> [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration).
> It is fixed adding a version specific project dependency (in .csproj) to the
> same version as the Carbon Aware SDK. Microsoft.Extensions.Configuration is
> backwards compatible.

## Run Function Locally

Both Azure Function apps can be
Expand Down
31 changes: 0 additions & 31 deletions samples/azure/azure-function/Startup.cs

This file was deleted.

14 changes: 11 additions & 3 deletions samples/azure/azure-function/function.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.19.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.0.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
<PackageReference Include="System.ComponentModel.Composition" Version="6.0.0" />
<PackageReference Include="WireMock.Net" Version="1.5.6" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand All @@ -26,4 +31,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext"/>
</ItemGroup>
</Project>
Loading