3
votes

When developing Azure Functions locally with a Service Bus binding, it is possible to send a request to test the function using an HTTP request to http://localhost:{port}/admin/functions/{function_name} (see Non HTTP triggered functions at https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local).

In the body of the HTTP request, the following is able to be sent to populate the body of the service bus message:

{
    "input": "<trigger_input>"
}

which gets bound to the message.Body property in the method: eg

public class TestServiceBusTrigger
    {
        [FunctionName("test-servicebus-trigger")]
        public static void ProcessQueueMessage([ServiceBusTrigger("inputqueue")] Message message, TextWriter logger)
        {
            logger.WriteLine(message.Body);
        }
    }

How do you populate the message.UserProperties in the sample request.

Currently using the following versions:

  • Microsoft.Azure.WebJobs 3.0.0-beta5
  • Microsoft.Azure.WebJobs.ServiceBus 3.0.0-beta5
  • Microsoft.NET.Sdk.Functions 1.0.13
  • Azure Functions Core Tools 2.0.1-beta.31
  • Function Runtime Version: 2.0.11888.0
3
If you found my explanation does make sense, you could accept it as an answer.Jerry Liu

3 Answers

0
votes

As the document you mentioned says,

The message body is required to have the following JSON format { "input":"<trigger_input>"}.

What you want to achieve is to simulate a Message Object, which contains UserProperties field. Apparently a Json format string is not qualified for this. In my test, the whole trigger_input is always recognized as message body.

Normally speaking(without using admin endpoint) , we can declare additional parameters in trigger to receive metadata. So I tried with admin endpoint, didn't work either. Looks like function host only accepts the string named after input, despite other parameters in request body.

So admin endpoint seems an dead end. You can use this code sample to test service bus trigger.

0
votes

If you are developing in Visual Studio then abstract you implementation behind a flyweight facade and then unit-test the implementation(s).

0
votes

The admin/functions endpoint should receive POST request whose body is created as following:

{
  "input": "<your_whole_service_bus_message_serialized>"
}

So, when sending your request, you have to serialize your Service Bus message first, then serialize again your input object wrapper.

If you're using CSharp, the content of your POST request should be:

var content = new StringContent(
   JsonConvert.SerializeObject(new { input = JsonConvert.SerializeObject(sbMessage)}), 
   Encoding.UTF8,
   "application/json"
);

Id you don't need the message wrapper object around, and if your function binding parameter is your complex type, you can send only the serialize type as the value of the input Json property.