使用CLI创建Azure Function项目

问题描述 投票:0回答:1

[使用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自变量吗?

azure-functions azure-functions-core-tools
1个回答
0
投票

是的,通常,您会得到一个像这样的项目:

© www.soinside.com 2019 - 2024. All rights reserved.