Search code examples
c#asp.net.netazure-functionsmicrosoft-graph-api

MSB4062 Error Code in Azure Function project in VS for running Microsoft Graph API with Azure Functions


I am trying to pull data using the Microsoft Graph API and I am following this tutorial in the article: Tutorial Link

I don't know very much of API and let alone Microsoft Graph API. Therefore, I find this solution helpful but due to I am beginner I am missing some steps not listed in the tutorial.

The steps that I have done is:

  1. Register both application and Azure Function application in Azure.
  2. Store AppID, Tenant ID and client secret of the registered app.
  3. Add API permissions and grant admin access.
  4. Create new Azure function project in Visual studio.
  5. Copy and paste code.
  6. Install packages.
  7. Add class to the project.

These are the steps that I have taken and these are my codes:

This is the code in the Function file:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Azure.Identity;
using Microsoft.Graph;
using System.Security.Claims;
using Newtonsoft.Json;

namespace FunctionApp2
{
    public class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log, ClaimsPrincipal claimIdentity)
        {

            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };


            var clientSecretCredential = new ClientSecretCredential(
                Constants.TenantId, Constants.AppId, Constants.ClientSecret, options);

            var graphClient = new GraphServiceClient(clientSecretCredential, Constants.scopes);
            var user = await graphClient.Users[claimIdentity.Identity.Name].GetAsync();
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
            return new OkObjectResult(json);

        }
    }
}

If I have create a new class file names Constants and pass the details of my Azure Application:

internal class Constants
    {
        public static string AppId = "<YOUR APP ID>";
        public static string TenantId = "<YOUR TENANT ID>";
        public static string ClientSecret = "<YOUR CLIENT SECRET>";
        public static string TenantName = "<YOUR TENANT NAME>";
        public static string [] scopes = new[] { "https://graph.microsoft.com/.default" };

    }

Further, these are the packages that I have installed:

  • Azure.Identity
  • Microsoft.Graph
  • Microsoft.NET.Sdk.Functions
  • Microsoft.Build.Framework

However, when I run the project locally in my Visual Studio I get the following error:

Error MSB4062 The "GenerateFunctionMetadata" task could not be loaded from the assembly C:\Users.... (hide rest of the path). Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. FunctionApp2 C:\Users.... (hide rest of the path).

When I run this from the Azure Function App I received this screen: browser screen

Thus the blue screen with the "Your Functions 4.0 app is up and running Azure Functions is an event-based serverless compute experience to accelerate your development." message.

Can someone please help me with this requesy?

Any help will b emuch appreciated.

Tried recreating the project many times and also in different .NET versions 6 and 8.


Solution

  • When I run this from the Azure Function App I received this screen browser screen Thus the blue screen with the "Your Functions 4.0 app is up and running Azure Functions is an event-based serverless compute experience to accelerate your development." message.

    Yes, it is saying Function app is up and running, it is for whole function app. If you want to run the function that you deployed you have to check here :

    enter image description here

    Then click on the Function and then click on test and run as below:

    enter image description here

    Now you will get the output of your function.

    Error MSB4062 The "GenerateFunctionMetadata" task

    This is generic error which comes when function is not updated and loaded correctly, try to use below latest csproj and rebuild:

    <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="Azure.Identity" Version="1.12.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.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.1" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
        <PackageReference Include="Microsoft.Graph" Version="5.56.0" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
      </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>