Azure
cloud
devops
dev
Functions
local.settings.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"AzureWebJobs.FileScan.Disabled": false,
"AzureWebJobs.FileAnalysis.Disabled": false,
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"Azure:FileAnalysisMq": "asset-analysis"
}
}
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
}
}
Program.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
var host = new HostBuilder()
.ConfigureAppConfiguration(c =>
{
c.AddJsonFile("./appsettings.local.json", true, false);
})
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices((context, sc) =>
{
sc.TryAddTransient<ITService, TService>();
})
.Build();
host.Run();
Csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>V4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<RootNamespace>Weavc</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.8.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="5.0.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.Development.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>
Function Examples
public class HttpFunctions
{
private readonly TService _service;
public HttpFunctions(TService service)
{
_service = service;
}
[Function("<TriggerName>")]
public async Task<HttpResponseData> RunAnalysis(
[HttpTrigger(AuthorizationLevel.Function, "get", "post")]
HttpRequestData req,
FunctionContext executionContext)
{
await Service.Run(new MyInfo(), executionContext);
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
await response.WriteStringAsync($"Successfully ran analysis");
return response;
}
[Function("<TriggerName>")]
public async Task Run([TimerTrigger("0 */1 * * * *")] MyInfo myTimer, FunctionContext context)
{
if (_fileScan is null)
return;
_logger?.LogInformation($"FileAnalysis function executed at: {DateTime.Now}");
_logger?.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus?.Next}");
var cancellationToken = new CancellationTokenSource();
for (var i = 0; i < _processCount; i++)
{
var result = await _service.Run(context, cancellationToken.Token);
if (result.IsFail())
{
if (result.Error.GetType() == typeof(NotFoundError))
return;
_logger?.LogError(result.Message);
}
}
}
}