afriedman111
afriedman111

Reputation: 2341

Azure Functions Core Tools version in VSCode when debugging incorrect and failing (Microsoft.Azure.WebJobs.Script.Grp)

I am having problems debugging my azure functions app in VSCode on a macbook. I have a functions v4 project that was created on my Windows machine and moved over to my mac. When I tried to run the function app with func start --csharp and it wouldn't run so I ran func --version and it said it was running on function v3. I did an npm global install for version 4 and it worked using the func start command. However, when I try to debug the project. I get the following info in the console:

Executing task: func host start 

Azure Functions Core Tools
Core Tools Version:       3.0.5682 Commit hash: N/A  (64-bit)
Function Runtime Version: 3.22.0.0

It then gives me the following error:

[2024-12-09T13:46:18.629Z] A host error has occurred during startup operation '208b4c31-f120-4954-8bf8-d4b306fc3f88'.
[2024-12-09T13:46:18.629Z] Microsoft.Azure.WebJobs.Script: Failed to start Language Worker Channel for language :dotnet-isolated. Microsoft.Azure.WebJobs.Script.Grpc: WorkerCofig for runtime: dotnet-isolated not found.
Value cannot be null. (Parameter 'provider')
[2024-12-09T13:46:18.762Z] A host error has occurred during startup operation '6c079412-f787-4e0a-a448-6ec6d5baaf81'.
[2024-12-09T13:46:18.763Z] Microsoft.Extensions.DependencyInjection: Cannot access a disposed object.
[2024-12-09T13:46:18.763Z] Object name: 'IServiceProvider'.

It seems as though the debugger is using Azure Functions Core Tools 3. How do I change this to version 4?

--- edit ---

My app is an isolated function. Here is the local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  },
  "Host": {
    "CORS": "*",
    "CORS_ALLOWED_ORIGINS": [
      "https://localhost:4200"
    ]
  }
}

Here is my .csproj file:

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UserSecretsId>4ae23a55-0673-43dd-b460-c9025ce15b4f</UserSecretsId>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Abstractions" Version="1.3.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
    <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.2.1" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.1" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\CriticalPass.Managers\CriticalPass.Managers.csproj" />
    <ProjectReference Include="..\CriticalPass.Models\CriticalPass.Models.csproj" />
    <ProjectReference Include="..\CriticalPass.MsGraph\CriticalPass.MsGraph.csproj" />
    <ProjectReference Include="..\CriticalPass.Utils\CriticalPass.Utils.csproj" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

Upvotes: 0

Views: 201

Answers (2)

afriedman111
afriedman111

Reputation: 2341

I found the problem. I installed an earlier version of the Azure Functions Core Tools a little while ago to get an older version of the app running which ran on functions v3. I was not able to do so, so I upgraded the app. When I tried to run it, it said version 3 was installed. So I ran npm install to install the latest version globally. When I shut down and started VSCode again I got an error message that said two versions of func cli were installed and gave me the option to delete the one from npm or the one from brew. I tried uninstalling and running each one separately but no luck. However I noticed when I uninstalled npm and ran brew, it said v3 was running, even though I had uninstalled it and reinstalled the new version. I also tried running brew upgrade azure-functions-core-tools and it still said v3 was running. When I uninstalled it it still said v3 was running.

When I ran brew unlink azure-functions-core-tools@3 and ran func -version it showed there was no func cli installed. I then ran brew install azure-functions-core-tools@4 and then it shows that v4 was running. When I hit F5 the debugger ran properly.

Upvotes: 0

Pravallika KV
Pravallika KV

Reputation: 8694

To use V4 version of Azure function core tools, Uninstall the existing(V3) version and install the latest version(V4) of core tools, refer MSDOC.

Delete %LocalAppData%\AzureFunctionsTools folder=> Restart the visual studio=>Run the function. This generates a new AzureFunctionsTools folder.

Cannot access a disposed object. Object name: 'IServiceProvider'.

Looks like the dependency injection in your function is misconfigured, the mentioned service might have not registered correctly in Program.cs and disposed in the correct scope. Avoid resolving services from IServiceProvider after it has been disposed.

I have created a .NET 8.0 Isolated Azure function.

Update .csproj as below:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
      <FrameworkReference Include="Microsoft.AspNetCore.App" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="2.0.0" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Abstractions" Version="1.3.0" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="2.0.0" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="2.0.0" />
      <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
      <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
      <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
      <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.2.1" />
      <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
  </ItemGroup>
</Project>

local.settings.json:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
      "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
     },
     "Host": {
       "CORS": "*",
       "CORS_ALLOWED_ORIGINS": [
         "https://localhost:4200"
       ]
     }
}

Program.cs:

using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = FunctionsApplication.CreateBuilder(args);

builder.ConfigureFunctionsWebApplication();
builder.Services.AddHttpClient();

builder.Services.AddSingleton<IMyService>((s) => {
    return new MyService();
});

builder.Build().Run();

Function.cs:

 public class Function1
 {
     private readonly ILogger<Function1> _logger;
     private readonly IMyService _myService;
     
     public Function1(IMyService myService, ILogger<Function1> logger)
     {
         _myService = myService;
         _logger = logger;
     }
     

     [Function("Function1")]
     public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
     {
         _logger.LogInformation("C# HTTP trigger function processed a request.");
         _logger.LogInformation(_myService.GetMessage());
         return new OkObjectResult("Welcome to Azure Functions! The message is: "+ _myService.GetMessage());
     }
 }

Response:

Azure Functions Core Tools
Core Tools Version:       4.0.6610 Commit hash: N/A +0d55b5d7efe83d85d2b5c6e0b0a9c1b213e96256 (64-bit)
Function Runtime Version: 4.1036.1.23224

[2024-12-10T09:24:10.026Z] Found C:\Users\uname\Source\Repos\FunctionApp1\FunctionApp1.csproj. Using for user secrets file configuration.
[2024-12-10T09:24:19.565Z] Azure Functions .NET Worker (PID: 24660) initialized in debug mode. Waiting for debugger to attach...
[2024-12-10T09:24:19.719Z] Worker process started and initialized.

Functions:

        Function1: [GET,POST] http://localhost:7087/api/Function1

For detailed output, run func with --verbose flag.
[2024-12-10T09:24:24.920Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-12-10T09:24:46.198Z] Executing 'Functions.Function1' (Reason='This function was programmatically called via the host APIs.', Id=b1817e02-8408-4633-a28b-e486afbd2e62)
[2024-12-10T09:24:47.090Z] C# HTTP trigger function processed a request.
[2024-12-10T09:24:47.093Z] Hello from DI in Azure Function!
[2024-12-10T09:24:47.113Z] Executing OkObjectResult, writing value of type 'System.String'.
[2024-12-10T09:24:47.302Z] Executed 'Functions.Function1' (Succeeded, Id=b1817e02-8408-4633-a28b-e486afbd2e62, Duration=1170ms)

Upvotes: 0

Related Questions