[使用npm i -g azure-functions-core-tools@3 --unsafe-perm true
安装Azure Functions Core工具后,我假设func init --ProjectName
命令将使用AzureFunctionsVersion v3创建Azure Functions项目,但在项目文件中使用v2创建了Azure Functions项目:
。csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
设置环境变量CLI_DEBUG=1
并重新运行我认识到的func init命令后,它会触发以下命令:
> dotnet new --install "C:\Users\murat\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\templates\itemTemplates.3.0.10405.nupkg"
> dotnet new --install "C:\Users\murat\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\templates\projectTemplates.3.0.10405.nupkg"
> dotnet new func --AzureFunctionsVersion v2 --name ProjectName --StorageConnectionStringValue "UseDevelopmentStorage=true"
> dotnet new --uninstall "C:\Users\murat\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\templates\itemTemplates.3.0.10405.nupkg"
> dotnet new --uninstall "C:\Users\murat\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\templates\projectTemplates.3.0.10405.nupkg"
显然,它调用--AzureFunctionsVersion v2
参数。这是默认行为吗?
因为如果我在文件夹中使用dotnet new func命令,或者使用VS2019 AzureFunctions v3项目,它将AzureFunctionsVersion设置为v3
[深入研究AcureFunctionsCoreTools源代码后,我在DotnetHelpers类的DeployDotnetProject方法中找到以下代码:
public async static Task DeployDotnetProject(string Name, bool force)
{
await TemplateOperation(async () =>
{
var connectionString = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? $"--StorageConnectionStringValue \"{Constants.StorageEmulatorConnectionString}\""
: string.Empty;
var exe = new Executable("dotnet", $"new func --AzureFunctionsVersion v2 --name {Name} {connectionString} {(force ? "--force" : string.Empty)}");
var exitCode = await exe.RunAsync(o => { }, e => ColoredConsole.Error.WriteLine(ErrorColor(e)));
if (exitCode != 0)
{
throw new CliException("Error creating project template");
}
});
}
我们还需要这个--AzureFunctionsVersion v2
自变量吗?
是的,通常,您会得到一个像这样的项目: