0
votes

I am new azure functions. I have created an azure http trigger function in azure portal. I want to output message to a servicebus topic.

Here is my httptrigger function in the portal:

#load "..\sharedcode\DeleteCommandRequest.csx"

#r "Newtonsoft.Json"
#r "Microsoft.ServiceBus"

using System.Net;
using Newtonsoft.Json;
using Microsoft.ServiceBus
using Microsoft.ServiceBus.Messaging;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, 
    TraceWriter log,
    ServiceBus outputSbMsg)
{
    log.Info("C# HTTP trigger function processed a request.");

    DeleteCommandRequest request = await req.Content.ReadAsAsync<DeleteCommandRequest>();

    log.Info($"Delete command recevied: {request.SagaId} {request.Action} {request.RequestId}");

    var message = new BrokeredMessage(JsonConvert.SerializeObject(request));
    outputSbMsg.send(message);

    return request == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass the correct request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + JsonConvert.SerializeObject(request));
}

I am getting the following error:

error CS0246: The type or namespace name 'ServiceBus' could not be found (are you missing a using directive or an assembly reference?)

Can anyone please help how to output brokeredmessage to a servicebus topic from httptrigger?

I also tried from VS2017 precompiled azure function:

[FunctionName("DeleteCase")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]
    HttpRequestMessage req,
    [ServiceBus("cqrs-commands", EntityType = EntityType.Topic)]
    IAsyncCollector<BrokeredMessage> messageTopic,
    TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    DeleteCommandRequest delRequest = await req.Content.ReadAsAsync<DeleteCommandRequest>();

    log.Info($"Delete request received: {delRequest.SagaId} {delRequest.Action} {delRequest.RequestId}");

    var brokeredMessage = new BrokeredMessage(delRequest);

    await messageTopic.AddAsync(brokeredMessage);

    return req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(delRequest));
}

But using the above code, the http trigger function not sending brokered message to the topic. Can anyone let me know what am i doing wrong here please?

Thanks

1
Can you share your function.json file ? I suspect your connectionstring is not set correctly ?Thomas
Could you elaborate on " the http trigger function not sending brokered message to the topic"? Is it an error you're facing or messages are not visible inside ServiceBus?kamil-mrzyglod

1 Answers

0
votes

the following is the extended httptrigger template for ServiceBus output bindings:

#r "Newtonsoft.Json"
#r "Microsoft.ServiceBus"
#r "System.Runtime.Serialization"

using System.Text;
using System.Runtime.Serialization;
using System.Net;
using Newtonsoft.Json;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;


public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, IAsyncCollector<BrokeredMessage> collector, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string name = req.GetQueryNameValuePairs()
    .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
    .Value;

    if (name == null)
    {
        // Get request body
        dynamic data = await req.Content.ReadAsAsync<object>();
        name = data?.name;
    }

    // brokered message
    var payload = JsonConvert.SerializeObject(new { id = 1234, name = "abcd" });
    var bm = new BrokeredMessage(new MemoryStream(Encoding.UTF8.GetBytes(payload)), true) { ContentType = "application/json", Label = "Hello", MessageId = "1234567890" };
    await collector.AddAsync(bm);

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

and the function.json file:

    {
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "serviceBus",
      "connection": "{yourconnection}",
      "name": "collector",
      "topicName": "{yourtopic}",
      "accessRights": "Manage",
      "direction": "out"
    }
  ],
  "disabled": false
}