如何将门户网站中的httptrigger函数输出到servicebus主题

时间:2018-05-03 11:55:22

标签: c# azure-functions

我是新的天蓝色功能。我在azure portal中创建了一个azure http触发器函数。我想将消息输出到servicebus主题。

这是门户网站中的httptrigger功能:

#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));
}

我收到以下错误:

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

任何人都可以帮助如何从httptrigger输出breakredmessage到servicebus主题?

我也试过VS2017预编译的azure函数:

[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));
}

但是使用上面的代码,http触发器函数不向主题发送代理消息。谁能让我知道我在这里做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

以下是ServiceBus输出绑定的扩展httptrigger模板:

#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);
}

和function.json文件:

    {
  "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
}