0

I have a simple Azure function with HttpTrigger like so:

[FunctionName("Heartbeat")]
public async Task<IActionResult> Heartbeat(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "heartbeat")] HttpRequest req,
    ILogger log)
{
    log.Log(LogLevel.Information, "Received heartbeat request");
    var status = await _healthCheck.CheckHealthAsync();

    return new OkObjectResult(Enum.GetName(typeof(HealthStatus), status.Status));
}

local.settings.json:

{
  "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=false",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "APPINSIGHTS_INSTRUMENTATIONKEY": "*****"
    },

    "ConnectionStrings": {
        "ServiceDb": "Server=.;Initial Catalog=Acquire;Integrated Security=True;"
    },

  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },

When i run the function locally I'm able to hit a breakpoint inside Heartbeat() and get 200OK. However, when it's deployed to Azure, It does not work and I get 404 Not Found.

Local:

enter image description here

Azure:

enter image description here

What is the problem? why can't I execute an HttpTrigger?

Also, is there a way to view local.settings.json configururation in Azure? I though these settings would somehow populate from local.settings.json in Azure (like connection strings)-yet they're blank. Is there a way to show these settings in Azure?

enter image description here

ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • 1
    Is AZURE windows or linux? Instead of Localhost try the machine name (or IP). On linux the IP address is not the same as in windows. – jdweng May 12 '21 at 21:10
  • 1
    I would verify the URL is correct it will look like this https://YourAppName.azurewebsites.net/api/Heartbeat notice it starts with https:// – Sql Surfer May 12 '21 at 21:43
  • Url is correct. i verified and re-verified and when i go to the url (without the '...azurewebsites.net/api/heartbeat' i get the 404. Without /api/heartbeat I get "your function is up and running" – ShaneKm May 12 '21 at 22:09
  • 1
    In order to configure this you need to add the settings 1 by 1 to the Configuration in azure. Some details are here (https://stackoverflow.com/questions/62960764/how-to-modify-iconfiguration-natively-injected-in-azure-functions/63124002#63124002) – lopezbertoni May 12 '21 at 22:24

3 Answers3

2

is there a way to view local.settings.json configururation in Azure?

There is no local.settings.json in Azure, hence the name "local".

You should add settings with the same names to your App Settings.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • 1
    Stephen, do you mean add them manually in Azure? What about using IOptions settings that are in local.settings.json? how would that be constructred? BTW> I really enjoyed your Concurrency in C# Cookbook :) – ShaneKm May 12 '21 at 22:06
  • https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources – ShaneKm May 12 '21 at 22:16
  • Does reading my configuration in Azure before I setup my configuration in Azure count as a Concurrency Problem? – Sql Surfer May 13 '21 at 00:13
  • @ShaneKm: In Azure Functions, `IOptions` [binds to the application settings by default](https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal#settings). If you create the function in the portal, you can add app settings in the portal. If you create the function via Bicep/ARM, then you can add app settings in your Bicep/ARM file. – Stephen Cleary May 13 '21 at 02:02
1

There should be an option for azure to give you the function url. Should look something like this enter image description here

freeAll
  • 82
  • 1
  • 4
  • 1
    You can even test that trigger on then `Code + Test` page without leaving the azure portal. It Should work, if not then surround the entry method with a try catch block and log the error – freeAll May 13 '21 at 06:59
0

I found the solution. My issue was that Runtime version was set to: ~1 instead of ~3. Another change I had to make for IOptions to bind is edit project, and modify these options:

<Content Include="local.settings.json">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>

enter image description here

To make logging work: https://nuggets.hammond-turner.org.uk/2019/10/tuesday-quickie-ilogger-in-azure.html

I added this and now I see all logs fine:

{
  "version": "2.0",
  "logging": {
    "logLevel": {
      "ServiceLogger": "Information"
    }
  }
}

but all the answers from other users got me on the right path.

ShaneKm
  • 20,823
  • 43
  • 167
  • 296