0

I am trying to upgrade a .NET Core 3.1 Azure Functions v3 function to use .NET 7 and Azure Functions v4.

I am following the guide from Microsoft here.

Old .csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.5" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.4.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.3.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
    <PackageReference Include="RestSharp" Version="106.10.1" />
    <PackageReference Include="Sendgrid" Version="9.12.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

New .csproj:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.5" />
        <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.4.0" />
        <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.3.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.10.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.7.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
        <PackageReference Include="RestSharp" Version="106.10.1" />
        <PackageReference Include="Sendgrid" Version="9.12.0" />
        <PackageReference Include="System.Data.SqlClient" Version="4.8.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>
</Project>

I have also created a Program.cs

using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.Build();

host.Run();

But whenever I try to Debug through Visual Studio I am greeted with the following errors. I am not using Visual Studio Code.

Can't determine project language from files. Please use one of [--csharp, --javascript]

No job functions found. Try making your job classes and methods public. If you're using extensions e.g Azure Storage, ServiceBus etc.) make sure you've called the registration method for the exteion in your startup code (e.g builder.AddAzureStorage() etc.)

The 'User' function is in error: Invalid script file name configuration. The 'scriptFile' property is set to a file that does not exist.

All the functions are HTTP triggers so I don't believe I need to add any start-up code, plus I don't have any so where would this go?

Well the script file error makes sense as I can see the output file is looking for a DLL but with the upgrade not producing an .exe how on earth do I update this, these files are all generated by the compiler?

Any advice would be greatly appreciated. To confirm I can create a new function from the template and run it without any error.

Ryan Thomas
  • 1,724
  • 2
  • 14
  • 27

1 Answers1

2

AFAIK,

I can see you have migrated the Versions of NuGet Packages used in your .NET Core 3.1 to 7 Isolated from the .csproj file along with Program.cs file adding.

Even they are same Http Triggers but the function code is different. There are lot of changes need to be made such as:

  1. In .NET Core 3.1, the namespace looks like Microsoft.Azure.WebJobs.* but in .NET 7 Isolated Function Code, the namespaces will be Microsoft.Azure.Functions.Worker.*.

  2. In Function Code - Function declaration section and also in the body - lot of changes need to be done when migrating from In-process to Out-of-process model such as Function Declaration attribute, Request object class and Response object class.

.NET 7 Isolated Function Code Declaration

[Function("HttpFunctionTrigger")]

public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req,

FunctionContext executionContext)

{

enter image description here

  • In .NET 3.1 Function Code Declaration, Function Attribute will be FunctionName, HttpRequestData will be HttpRequest, HttpResponseData will be OkObjectResult and sample code given by @StephenCleary.

  • Microsoft recommends upgrading from .NET Core 3.1 to 6 (in-process) for easy upgrade process and then to .NET 7 Isolated model as specified in this section of MS Doc.

  • .NET Core 3.1 uses C# 8.0 Version and .NET 7.x uses the C# 11 language version so code should be converted accordingly.

  • Also, not only the Project File, Packages Upgradation, Function Code needs to be changed accordingly and also FUNCTIONS_WORKER_RUNTIME should be correct value like dotnet-isolated if it is .NET Core out-of-process model.

  • Thank you for this information, I completely forgot about the whole in-process / isolated stuff. I think for now my pass of least resistance is to go to .NET 6 and then review from there, thank you very much. – Ryan Thomas Mar 24 '23 at 09:26