How to debug ServiceBus-triggered Azure Function locally?
Asked Answered
T

3

14

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?

Talya answered 29/8, 2017 at 9:43 Comment(5)
Press F5 and send a message to your service bus topicDriven
:-) Yes. I am aware of that possibility. But is there an easy way doing that in the context of the function? That is, reusing settings and maybe do it every time I start debugging?Howe
If you want it to add messages on startup you could consider adding an additional timer triggered function to your app that will automatically put messages onto the service bus on a short interval to facilitate debugging.Lucullus
It's a triggered functions, so when you debug, it's expected that the function will be triggered by an event. In addition to the suggested options, another option is to drop a message on a queue using a tool (SBExplorer, ServiceBus360, Cloud Explorer, etc).Salvatore
I had to add "-- port 7033" (or whatever port you want) to the Debug Command Line Arguments. The console does not display which port the Service Bus Trigger endpoint is listening on. So you might not find it. After that the admin endpoint worked fine via localhos:<port>.Sena
D
2

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);
    }
}
Draughtboard answered 30/8, 2017 at 6:19 Comment(0)
V
29

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

Vexatious answered 12/10, 2017 at 17:2 Comment(5)
Teomat, have you successfully done this for a function triggered by a Service Bus message? I'm trying to do exactly this, and keep getting SerializationException's when my function calls BrokeredMessage#GetBody<string>() to get the body of the message.Vestiary
Yeah, it worked. Don't serialize it yourself, the runtime will do that for you. Just include your string parameter in the Run function: Run([ServiceBusTrigger("queuename",......)]string Info, TraceWriter log)Vexatious
Thanks! I needed to do some additional actions to make it work: 1) to change trigger signature accepting string msg instead of Message msg; 2) to do JSON escape for my test SB message like { "input": "{ \"testobj\": { \"testprop\": \"testval\"} }"Vookles
This should be the accepted answer. Saved me a lot of trouble. ThanksStrang
@Ivan, you don't need to change signature, you can use Encoding.UTF8.GetString(message.Body) to get the data you posted.Aga
A
6

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)
Aga answered 6/10, 2021 at 5:16 Comment(0)
D
2

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);
    }
}
Draughtboard answered 30/8, 2017 at 6:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.