Azure function with HttpTrigger is not getting executed

Stefania Grillo 25 Reputation points
2024-02-28T16:53:24.4533333+00:00

Hello folks, after migrating my code to latest Azure Function with .NET 7 (isolated process), I am having an issue trying to call all of my azure functions with an http trigger. In the azure function console, I can see I am calling the right endpoint, as you can see from the picture below: immagine

Here is how the function is implemented:

[Function("uploading-process")]
 public async Task Run(
     [HttpTrigger(AuthorizationLevel.Anonymous, "post")]
     HttpRequest request,
     FunctionContext executionContext,
     CancellationToken cancellationToken,
     ILogger logger)
{
   		try
            {
                var globalConfig = GlobalConfig.Create(() =>
                    new AutofacConfig()
                        .ConfigTelemetryExecutionContextProvider(executionContext)
                        .ConfigDocumentClient(_cosmosClient)
                        .DefaultConfig()
                        .BuildContainer());
                await globalConfig.Container.ResolveNamed<IActivityMediator>("update-internalstatus-mediator")
                    .ExecuteAsync(batch, cancellationToken);

            } 
            catch(Exception ex)
            {
                _telemetryClient.TrackException(ex);
            }
        }
}


This is my host.json file:

{
  "version": "2.0",
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.3.0, 4.0.0]"
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }
}

Hope the info I provided are enough. Do you have any idea why this is happening?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,930 questions
Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Stefania Grillo 25 Reputation points
    2024-03-06T14:42:35.84+00:00

    I fixed the issue just creating a new Azure Function and updating the code as follows:

    Program.cs changed a bit the structure:

    using Microsoft.AspNetCore.Server.Kestrel.Core;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    
    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
        .ConfigureServices(services =>
        {
            services.AddApplicationInsightsTelemetryWorkerService();
            services.ConfigureFunctionsApplicationInsights();
            services.Configure<KestrelServerOptions>(options =>
            {
                options.AllowSynchronousIO = true;
            });
        })
        .Build();
    
    host.Run();
    
    
    

    host.json removed useless code:

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          },
          "enableLiveMetricsFilters": true
        }
      }
    }
    
    
    

    .csproj changed few versions:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
        <ImplicitUsings>enabled</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
      <ItemGroup>
        <Compile Remove="Activities\DownloadDocumentsByDatesMediator.cs" />
        <Compile Remove="DownloadDocumentsByDates.cs" />
      </ItemGroup>
      <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Autofac" Version="8.0.0" />
        <!--<PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />-->
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.1.0" />
        <PackageReference Include="Microsoft.Azure.Core.NewtonsoftJson" Version="2.0.0" />
        <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
        <PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
        <PackageReference Include="Reviso.Authentication" Version="1.0.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.DurableTask" Version="1.1.1" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.4"  OutputItemType="Analyzer"/>
        <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.Extensions.Timer" Version="4.3.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.6.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.2.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="6.2.0" />
        <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Tables" Version="1.2.1" />
      </ItemGroup>
      <ItemGroup>
        <ProjectReference Include="..\RevisoAgyoDataLayer\RevisoAgyoDataLayer.csproj" />
        <ProjectReference Include="..\RevisoAgyoModels\RevisoAgyoModels.csproj" />
        <ProjectReference Include="..\RevisoAgyoServices\RevisoAgyoServices.csproj" />
      </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.example.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
      <ItemGroup>
        <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
      </ItemGroup>
    </Project>
    
    
    

    Function with endpoint, small changes in the structure:

    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Autofac;
    using Microsoft.Azure.Cosmos;
    using Microsoft.Azure.Functions.Worker;
    using Microsoft.Azure.Functions.Worker.Http;
    using Microsoft.DurableTask.Client;
    using Microsoft.Extensions.Logging;
    using Microsoft.AspNetCore.Http;
    using Microsoft.WindowsAzure.Storage.Queue;
    using Microsoft.WindowsAzure.Storage;
    using RevisoAgyoModels;
    using RevisoAgyoServices;
    using RevisoAgyoWorkflow.DownloadDocumentsByDates.Implementations;
    namespace RevisoAgyoWorkflow.DownloadDocumentsByDates.EntryPoints
    {
        public class DownloadDocumentsByDates
        {
            [Function("DownloadDocumentsByDates_HttpStart")]
            public static async Task<HttpResponseData> HttpStart(
                [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req, 
                [DurableClient] DurableTaskClient starter,
                FunctionContext _executionContext)
            {
                ILogger _logger = _executionContext.GetLogger(nameof(DownloadDocumentsByDates));
                var cosmosDbConnectionString = Environment.GetEnvironmentVariable("CosmosDb_connectionString"); 
                var downloadDocumentQueueConnectionString = Environment.GetEnvironmentVariable("DownloadDocument_connectionString"); 
                var downloadDocumentQueueName = Environment.GetEnvironmentVariable("DownloadDocument_queueName");
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(downloadDocumentQueueConnectionString);
                CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
                CloudQueue queue = queueClient.GetQueueReference(downloadDocumentQueueName);
                var globalConfig = GlobalConfig.Instance(() =>
                        new AutofacConfig()
                            .DefaultConfig()
                            .ConfigCosmosClient(new CosmosClient(cosmosDbConnectionString))
                            .ConfigDownloadDocumentQueueStorage(queue)
                            .ConfigTelemetryExecutionContextProvider(_executionContext)
                            .BuildContainer());
                var _telemetry = globalConfig.Container.Resolve<ITelemetry>();
                var input = req.ReadFromJsonAsync<DownloadDocumentsByDatesParams>().Result;
                var executionContext = new TelemetryExecutionContext(_executionContext);
                string instanceId = null;
                IPurchaseInvoicesDownloadingService _purchaseInvoicesDownloadingService = null;
                ...
            }
        }
    }
    

    I do not know what exactly fixed the issue, but I made these changes based on what I had the new Azure Function and it worked.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.