0

主要问题

有什么方法可以让 Azure Function C# 脚本 (.cxs) 在 .NET 6 上运行?

背景

我们有一个配置为使用运行时版本(即FUNCTIONS_EXTENSION_VERSION.~4

在该应用程序中,我们有一个具有以下function.proj内容的函数:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>      
    <ItemGroup>     
        <PackageReference Include="CsvHelper" Version="27.2.1" />  
    </ItemGroup>   
</Project>    

run.csx文件中,我有以下引用和using指令:

#r "Newtonsoft.Json"

using System;
using System.IO;     
using System.Globalization;
using System.Text;  
using CsvHelper;   
using Newtonsoft.Json;

这工作正常:

[Information] Restoring packages.
[Information] Starting packages restore
[Information] Determining projects to restore...
[Information] Restored C:\local\Temp\{guid}\function.proj (in 3.73 sec).
[Information] Packages restored.
[Information] Package references have changed.
[Information] Assembly reference changes detected. Restarting host...
[Information] Script for function '...' changed. Reloading.
[Information] Compilation succeeded.

但我想从netstandard2.0(我理解它.NET Framework 4.8 运行时相关联)升级到net6.0(.NET 6)。

将文件中的 TargetFramework 更改为net6.0(甚至netstandard2.1function.proj会导致出现警告:

[Information] Restored C:\local\Temp\{guid}\function.proj (in 470 ms).
[Warning] You may be referencing NuGet packages incorrectly. Learn more: https://go.microsoft.com/fwlink/?linkid=2091419
[Warning] You may be referencing NuGet packages incorrectly. Learn more: https://go.microsoft.com/fwlink/?linkid=2091419
[Warning] You may be referencing NuGet packages incorrectly. Learn more: https://go.microsoft.com/fwlink/?linkid=2091419

然后尝试保存/编译 csx 文件会导致错误:

[Information] Script for function '...' changed. Reloading.
[Error] run.csx(10,7): error CS0246: The type or namespace name 'CsvHelper' could not be found (are you missing a using directive or an assembly reference?)

我在链接文档中注意到他们netstandard2.0在示例function.proj文件中使用。我还发现了这个 github 问题,在 2020 年 7 月,他们说:

对于 C# 脚本函数,我们建议您在 Function.proj 中以 .Net Standard 2.0 为目标。
...
CSX 的依赖关系解析将针对 .NET Standard 2.0。对于 netcoreapp3.1,建议使用预编译项目。

现在,我们到了 2022年。自去年 11 月以来, .NET 6.0 已在 Azure Functions 中普遍可用。Azure 的Functions Versions 文档将 .NET 6.0 列为运行时版本 4.x 中 C# 支持的框架。我认为Azure Functions Scripts 将支持 .NET 6.0,但我在这个方向上所做的每一次尝试都导致了神秘的警告和错误消息。

我只是错过了什么,还是 Azure Functions Scripts 仍然停留在netstandard2.0

我尝试过的事情

我尝试添加<AzureFunctionsVersion>v4</AzureFunctionsVersion>到我这里function.proj提到的。那没有任何区别。

我尝试使用#r

#r "CsvHelper"

但这只是给了我另一个关于找不到元数据文件的错误。

run.csx(2,1): error CS0006: Metadata file 'CsvHelper' could not be found

我尝试使用这个:

#r "nuget: CsvHelper"

...但这似乎使编译器挂起:

[Information] Script for function '...' changed. Reloading.
No new trace in the past 1 min(s).
No new trace in the past 2 min(s).
...
4

0 回答 0