Search code examples
azure-functionsazure-functions-core-tools

Creating Azure Function Project with CLI


After installing Azure Functions Core Tools with npm i -g azure-functions-core-tools@3 --unsafe-perm true, I assumed the func init --ProjectName command would create the Azure Functions Project with AzureFunctionsVersion v3 but instead it created with v2 in the project file:

.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>

After setting the environment variable CLI_DEBUG=1 and rerunning the func init command I've realized, it triggers following commands:

> 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"

clearly it calls the --AzureFunctionsVersion v2 argument. Is this default behavior?

Because if I use the dotnet new func command in a folder, or use VS2019 AzureFunctions v3 Project, it sets AzureFunctionsVersion to v3

After digging into AcureFunctionsCoreTools source code, i found following code in DeployDotnetProject method of DotnetHelpers class:

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");
        }
    });
}

Do we still need this --AzureFunctionsVersion v2 argument?


Solution

  • Yes, on usual, you will get a project like this:

    <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>
    

    Have a look of this Blog, you can choose to create azure function v3 by Visual Studio 2019 16.4 or newer. It will create v3 successfully.

    And have a look of this: The Azure CLI may be still in the situation of create v2 by default.

    By the way, you can change the AzureFunctionsVersion of the Function App that you created using CLI to v3. It seems no matter what Version you set, the RunTime Verison is still 3.0.