16

I'm trying to migrate an Azure Function that works perfectly on .NET 3.1 to .NET 5. I followed Microsoft's GitHub guide and still can't get it to run or debug locally (didn't even try to publish to Azure).

I'm getting this error:

[2021-09-05T09:49:33.066Z] A host error has occurred during startup operation 'bb37a6db-b6f4-4396-bb9b-cb5ae0bba387'.
[2021-09-05T09:49:33.067Z] Microsoft.Azure.WebJobs.Script: Did not find functions with language [dotnet-isolated].

And this exception is being raised:

This is my Program.cs:

static async Task Main(string[] args)
        {
            var host = new HostBuilder()
               .ConfigureAppConfiguration(c =>
               {
                   c.AddCommandLine(args);
               })
               .ConfigureServices(s =>
               {
                   s.AddLogging();
               })
               .Build();

            await host.RunAsync();
        }

This is my project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <UserSecretsId>76d0a5ed-221b-4b35-8ff4-3ee33d393196</UserSecretsId>
    <OutputType>Exe</OutputType>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
  </PropertyGroup>
  <ItemGroup>
    <None Remove="global.json" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="global.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.5.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="4.0.4" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.0.4" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
    <PackageReference Include="System.Linq.Async" Version="5.0.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="config.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

My local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "HIDDEN",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  }
}

My host.json:

{
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      //"QueueWorkers.EmailQueueWorker": "Trace",
      //"QueueWorkers.EmailQueueWorker.EmailQueueWorker": "Trace",
      "default": "Information",
      "Function": "Trace",
      "Host.Results": "Error",
      "Host.Aggregator": "Trace"
    },
    "applicationInsights": {
      //"samplingExcludedTypes": "Request",
      "samplingSettings": {
        "isEnabled": true
      }
    },
    "console": {
      "isEnabled": "true"
    }
  },
  "Values": {
    "AzureWebJobsStorage": "HIDDEN",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  },
  "extensions": {
    "queues": {
      "maxDequeueCount": 5,
      "maxPollingInterval": "00:00:02"
    }
  },
  "version": "2.0"
}

Thanks

NOP-MOV
  • 792
  • 2
  • 8
  • 28
  • Does this answer your question? [How can I debug Azure Functions using .NET 5 (isolated process) in Visual Studio?](https://stackoverflow.com/questions/67526631/how-can-i-debug-azure-functions-using-net-5-isolated-process-in-visual-studio) – Ian Kemp Sep 09 '21 at 12:46
  • 1
    This helped me: "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" Thanks!!! – Richard Sep 23 '21 at 13:32
  • `net5.0` <-- dotnet "isolated" also isn't supported by .NET5. Needs to be .NET6 or later. – Pure.Krome Jan 23 '23 at 03:28

8 Answers8

34

In Azure navigate to your Function and then in the section Settings -> Configuration change the Application settings:

"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"

enter image description here

ping
  • 663
  • 6
  • 13
24

Had a similar problem in Visual Studio 2022, while trying to migrate a project from .net 5 to .net 6 functions...

kept telling me "Microsoft.Azure.WebJobs.Script: Did not find functions with language [dotnet-isolated]"

enter image description here

My Solution

1) I was missing: Program.cs

using Microsoft.Azure.Functions.Worker.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;

namespace Main.Function
{
    public class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .Build();

            host.Run();
        }
    }
}

2) Edit .csproj > Under ItemGroup...

Remove:

<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />

Replace with

<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.3.0" OutputItemType="Analyzer" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
      <PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="4.0.1" />

3) Ensure OutputType is set to exe

<OutputType>Exe</OutputType>

enter image description here

4) Unload project and reload (delete bin and obj folder before building)

Dwain Browne
  • 792
  • 7
  • 12
  • How to set the localhost ip? When running this I get this error `The gRPC channel URI 'http://:7262' could not be parsed.` on the line `host. Run();` in the Program.cs. – Amr Eladawy Sep 25 '22 at 12:02
  • I had to change [FunctionName()] to [Function()] and remove ILogger parameter from the function method to get things to work after doing these changes. – Gayan Dec 22 '22 at 07:08
1

If everything is ok with your code (FunctionName attribute is now Function, and some other details), you may still have to remove unnecessary references like:

Microsoft.Azure.Functions.Extensions
Microsoft.Azure.WebJobs.Extensions
Microsoft.Azure.WebJobs.Extensions.ServiceBus
Microsoft.Azure.WebJobs.Extensions.Storage
Microsoft.Azure.WebJobs.Extensions.Tables
Microsoft.NET.Sdk.Functions

Also, don't forget to change the output type, as already said here

<OutputType>Exe</OutputType>

Of course, you should have included a new Program.cs file (there is a good example in a comment above).

0

Well, still couldn't directly launch and debug from VS 2019 but function does work if launched from CLI and debug is working.

  1. Add Debugger.Launch(); to Program.cs (first line of Main method)
  2. Make sure you have <LangVersion>preview</LangVersion> in your csproj (right after TargetFramework). This missing line was actually the critical difference that stopped my function from working.
  3. Launch from CLI (from function folder): "func start --verbose"

Thanks

NOP-MOV
  • 792
  • 2
  • 8
  • 28
0
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
Romesh
  • 2,291
  • 3
  • 24
  • 47
0

Since it is not working locally, you need to comment/remove the setting:

"FUNCTIONS_WORKER_RUNTIME": "dotnet"

OR

"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"

from your local.settings.json file.

In Azure, you need to make sure you have this setting in.

man_luck
  • 1,605
  • 2
  • 20
  • 39
0

The same error can also occur when switching from MSDeploy to ZipDeploy (as MS suggests one should do) and the function app is not configured yet.

Check your target app service also has package enabled: WEBSITE_RUN_FROM_PACKAGE : 1

Read more: Run your functions from a package file in Azure.

Imre Pühvel
  • 4,468
  • 1
  • 34
  • 49
0

In order to debug startup errors, I used Microsoft's suggestion of catching any startup errors and logging them to Application Insights. Specifically,

try
{
    // do all your DI stuff
}
catch (Exception ex)
{
    var config = new TelemetryConfiguration
    {
        ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")
    };
    var client = new TelemetryClient(config);
    client.TrackException(ex);
    client.Flush();

    throw;
}
Scott
  • 4,458
  • 1
  • 19
  • 27