Alf Kåre Lefdal
Alf Kåre Lefdal

Reputation: 723

How to debug ServiceBus-triggered Azure Function locally?

My function will be triggered from an existing ServiceBus topic. I have created the function using the new tooling in VS2017 (15.3) preview, as a compiled function.

How can I test this function locally?

Upvotes: 16

Views: 26977

Answers (4)

Haithem
Haithem

Reputation: 1

For .Net8 debugging in isolated mode, you would to need to:

  1. Follow the instruction posted by @Amor.

  2. Copy local.setting.json file to your console app.

  3. Copy Program.cs file into your console app.

  4. Change your HostBuilder declaration as follow:

    var host = Host.CreateDefaultBuilder()
    .ConfigureAppConfiguration(app => {
        app.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();
    })
    .ConfigureServices((context, services) =>
    {
        services.AddScoped<FunctionName>();
    })
    .Build();
    
  5. To get access to any value in the json file you would need to append Values: to the variable name as follow:

    var clientID = context.Configuration.GetValue<string>("Values:clientId");
    

Upvotes: 0

Ali
Ali

Reputation: 1245

You run the project in debug mode and create a JSON payload for the message then you can post a complete json object in this format.

{
    "input": "<trigger_input>"
}

for example

http://localhost:7071/admin/functions/OrderFunction

enter image description here

You can then read message in your function body and get the Json you posted.

Encoding.UTF8.GetString(message.Body)

Upvotes: 7

Amor
Amor

Reputation: 8509

If you want to check whether your function will be triggered by Azure Service Bus messages, you need to own a Azure Subscription and create a Service Bus namespace because Microsoft haven't provided Azure Service Bus emulator like Azure Storage emulator.

If you want to debug your function, you could create a new console application and invoke the function you defined. Steps below are for your reference.

Step 1, Create a Console Application.

Step 2, Add Project reference to the function project.

Step 3, Install Microsoft.Azure.WebJobs -Version 2.1.0-beta1 package from NuGet to your console application.

Install-Package Microsoft.Azure.WebJobs -Version 2.1.0-beta1 

Step 4, Use following code to invoke your function.

class Program
{
    static void Main(string[] args)
    {
        Function1.Run("msg1", new MyTraceWriter(TraceLevel.Info));
    }
}

public class MyTraceWriter : TraceWriter
{
    public MyTraceWriter(TraceLevel level) : base(level)
    {

    }

    public override void Trace(TraceEvent traceEvent)
    {
        Console.WriteLine(traceEvent.Message);
    }
}

Upvotes: 2

TomTichy
TomTichy

Reputation: 627

For a non-http triggered function, you can send a POST request to the local administrator endpoint. More info here

like this (I am using Postman) enter image description here

Upvotes: 32

Related Questions