Skip to content

Commit

Permalink
Merge pull request #1079 from icsharpcode/feature/funcnet8
Browse files Browse the repository at this point in the history
Upgrade Azure Function to .NET 8
  • Loading branch information
christophwille authored Jan 30, 2024
2 parents d6bea17 + 1856b78 commit 0816e57
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 348 deletions.
264 changes: 0 additions & 264 deletions Func/.gitignore

This file was deleted.

30 changes: 14 additions & 16 deletions Func/Convert.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.CodeConverter.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

Expand All @@ -16,36 +12,38 @@ public class Convert
public const string DefaultRequest = @"{""code"":""Public Class VisualBasicClass\r\n\r\nEnd Class"",""requestedConversion"":""vbnet2cs""}";
public const string DefaultConversion = "\r\npublic partial class VisualBasicClass\r\n{\r\n\r\n}";

private readonly ILoggerFactory _loggerFactory;
private readonly ILogger<Convert> _logger;

public Convert(ILoggerFactory loggerFactory)
public Convert(ILogger<Convert> logger)
{
_loggerFactory = loggerFactory;
_logger = logger;
}

//
// Sample data: {"code":"Public Class VisualBasicClass\r\n\r\nEnd Class","requestedConversion":"vbnet2cs"}
//
[FunctionName("Convert")]
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods - Name must be "Run" for this to work AFAIK
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
CancellationToken hostCancellationToken)
[Function("Convert")]
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
CancellationToken hostCancellationToken)
{
var logger = _loggerFactory.CreateLogger<Convert>();
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

if (0 == string.CompareOrdinal(requestBody, DefaultRequest)) {
logger.LogInformation("Short-circuiting for default conversion request");
_logger.LogInformation("Short-circuiting for default conversion request");
return new OkObjectResult(new ConvertResponse(true, DefaultConversion, ""));
}

var data = JsonConvert.DeserializeObject<ConvertRequest>(requestBody);

if (null == data) {
return new BadRequestResult();
}

using var cancellationSource = CancellationTokenSource.CreateLinkedTokenSource(hostCancellationToken, req.HttpContext.RequestAborted);
var response = await WebConverter.ConvertAsync(data, cancellationSource.Token);

return new OkObjectResult(response);
}
}
}
34 changes: 24 additions & 10 deletions Func/Func.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<RootNamespace>ICSharpCode.CodeConverter.Func</RootNamespace>
<AssemblyName>ICSharpCode.CodeConverter.Func</AssemblyName>
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
<UserSecretsId>1d2f0b91-7e29-4920-8a93-e52863d06c2e</UserSecretsId>
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\CodeConverter\Common\DefaultReferences.cs" Link="DefaultReferences.cs" />
<Compile Include="..\Web\WebConverter.cs" Link="WebConverter.cs" />
<None Include="local.settings.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
<ProjectReference Include="..\CodeConverter\CodeConverter.csproj" />
</ItemGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
Expand All @@ -23,9 +37,6 @@
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CodeConverter\CodeConverter.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -35,4 +46,7 @@
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
<ItemGroup>
<Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
</ItemGroup>
</Project>
8 changes: 0 additions & 8 deletions Func/GlobalSuppressions.cs

This file was deleted.

Loading

0 comments on commit 0816e57

Please sign in to comment.