Azure Functions (Function App) : Tips for developer

This article is focus on Azure Functions and dotnet5-isolated mode.

Mostly when working azure functions, Visual Studio is quite developer friendly. When Function App created and hit the "F5" , behind the scene It will use func tool to run function app.

image.png

Now there is a requirement to run function app from command-line and specially when working with microservices, there is a possibility to have multiple function app. In this scenario run from command line or say terminal will be a good option.

To run function app from terminal

Go to directory for your function app where host.json or local.settings.json is located. In my case following is how it looks like.

image.png

Go to that directory from terminal and execute following command.

func host start

It will look something like this once execute.

image.png

If it is required to run in verbose mode then execute following command. This will give details during run.

func host start --verbose

Change default routePrefix.

By default when function app created and run, it has default routePrefix "api".

image.png

To do this go to host.json and it will looks like following.

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

Replace that with following. Specially add focus to "extensions" section.

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensions": {
    "http": {
      "routePrefix": "newroute"
    }
  }
}

Now it looks like following.

image.png

Note: If it is required that no routePrefix required then leave it blank.

Above option is good for production where host.json is used or scenario where production and development environment required different configuration.

In that case needs to change in loca.settings.json so only apply local development.

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    }
}

Now add following configuration AzureFunctionsJobHost__extensions__http__routePrefix into it so it will looks like following.

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "AzureFunctionsJobHost__extensions__http__routePrefix":"localonlyroute"
    }
}

Change default port

To do this go to local.settings.json file and it will default looks like following.

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
    }
}

Add host section into that. It will look like this.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  },
  "Host": {
    "LocalHttpPort": 12001
  }
}

It will now look like this.

image.png

Next time will try to cover some advance configuration.