最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net core - Azure function topic output binding - set custom properties - Stack Overflow

programmeradmin0浏览0评论

I am trying to use Azure function output bindings to send message to a service bus topic having one subscription

Following piece of code is doing the job but I am not able to set

  • Custom Properties
  • Message Properties ( contentType, messageId etc )
[Function(nameof(Function1))]
[ServiceBusOutput("test-topic", Connection = "CONN")]
public async Task<OutputData> Run(
    [ServiceBusTrigger("test-queue", Connection = "CONN")]
    ServiceBusReceivedMessage message,
    ServiceBusMessageActions messageActions,
    ICollector<BrokeredMessage> collector
    )
{
    
    //Following return 
    //Is there any way to set custom properties of this message?
    //Along with custom property, I would also like to set messageProperty contentType to application/json

    OutputData outputData = new OutputData
    {
        ID = 123,
        Name = "Test"
    };

    // Complete the message
    await messageActions.CompleteMessageAsync(message);

    return outputData;

    /*As per solution mentioned [here](), I tried to set custom properties but collector is always null.*/
   
    BrokeredMessage brokeredMessage = new();
    brokeredMessage.ContentType = "application/json";
    brokeredMessage.Properties.Add("ID", 123);
    brokeredMessage.Properties.Add("Name", "Test");
    //Injecting  ICollector<BrokeredMessage>  notworking as its always null.
    //collector.Add(brokeredMessage);
}

I can see outputData has reached to its destination but content type is text/plain and I can't add any custom properties.

I am using

  • .NET 9 ( Git repo)
  • Azure service bus ( standard )

Any pointers ?

UPDATE 1 As suggested by @Dasari Kamali I tried using ServiceBusMessage but still observing same behavior

 #region ServiceBusMessage not working
 ServiceBusMessage serviceBusMessage = new ServiceBusMessage();
 serviceBusMessage.ContentType = "application/json";
 serviceBusMessage.ApplicationProperties.Add("ID", 123);
 serviceBusMessage.ApplicationProperties.Add("Name", "Test");
 serviceBusMessage.Body = BinaryData.FromString("Test");
 #endregion
 // Complete the message
 await messageActions.CompleteMessageAsync(message);
 return serviceBusMessage;

I am trying to use Azure function output bindings to send message to a service bus topic having one subscription

Following piece of code is doing the job but I am not able to set

  • Custom Properties
  • Message Properties ( contentType, messageId etc )
[Function(nameof(Function1))]
[ServiceBusOutput("test-topic", Connection = "CONN")]
public async Task<OutputData> Run(
    [ServiceBusTrigger("test-queue", Connection = "CONN")]
    ServiceBusReceivedMessage message,
    ServiceBusMessageActions messageActions,
    ICollector<BrokeredMessage> collector
    )
{
    
    //Following return 
    //Is there any way to set custom properties of this message?
    //Along with custom property, I would also like to set messageProperty contentType to application/json

    OutputData outputData = new OutputData
    {
        ID = 123,
        Name = "Test"
    };

    // Complete the message
    await messageActions.CompleteMessageAsync(message);

    return outputData;

    /*As per solution mentioned [here](https://stackoverflow/questions/50457428/custom-message-properties-on-azure-queue-topic-message-from-azure-function), I tried to set custom properties but collector is always null.*/
   
    BrokeredMessage brokeredMessage = new();
    brokeredMessage.ContentType = "application/json";
    brokeredMessage.Properties.Add("ID", 123);
    brokeredMessage.Properties.Add("Name", "Test");
    //Injecting  ICollector<BrokeredMessage>  notworking as its always null.
    //collector.Add(brokeredMessage);
}

I can see outputData has reached to its destination but content type is text/plain and I can't add any custom properties.

I am using

  • .NET 9 ( Git repo)
  • Azure service bus ( standard )

Any pointers ?

UPDATE 1 As suggested by @Dasari Kamali I tried using ServiceBusMessage but still observing same behavior

 #region ServiceBusMessage not working
 ServiceBusMessage serviceBusMessage = new ServiceBusMessage();
 serviceBusMessage.ContentType = "application/json";
 serviceBusMessage.ApplicationProperties.Add("ID", 123);
 serviceBusMessage.ApplicationProperties.Add("Name", "Test");
 serviceBusMessage.Body = BinaryData.FromString("Test");
 #endregion
 // Complete the message
 await messageActions.CompleteMessageAsync(message);
 return serviceBusMessage;

Share Improve this question edited Feb 4 at 11:59 user2243747 asked Feb 4 at 11:36 user2243747user2243747 2,9778 gold badges45 silver badges66 bronze badges 3
  • Use ServiceBusMessage instead of BrokeredMessage to set custom properties and ContentType in the output binding. – Dasari Kamali Commented Feb 4 at 11:43
  • @DasariKamali please have a look at UPDATE 1. ServiceBusMessage is also not helpful. Am I missing anything else? – user2243747 Commented Feb 4 at 12:00
  • I used a ServiceBusQueue trigger function that manually creates a ServiceBusSender from a ServiceBusClient and explicitly sends the message using SendMessageAsync(). This approach successfully worked for me to set the ContentType as application/json under the Message Properties in the Topic. – Dasari Kamali Commented Feb 6 at 10:11
Add a comment  | 

2 Answers 2

Reset to default 0

I tried your code and got the same issue, so I tried using a ServiceBusQueue trigger function instead.

It manually creates a ServiceBusSender from a ServiceBusClient and explicitly sends the message using SendMessageAsync(). It successfully worked for me to send message to a Service Bus Topic and setting the ContentType as application/json and Custom Properties.

using System.Text;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp24
{
    public class Function1
    {
        private readonly ILogger<Function1> _logger;
        private readonly ServiceBusClient _serviceBusClient;

        public Function1(ILogger<Function1> logger, ServiceBusClient serviceBusClient)
        {
            _logger = logger;
            _serviceBusClient = serviceBusClient;
        }

        [Function(nameof(Function1))]
        public async Task Run(
            [ServiceBusTrigger("kamqueue", Connection = "CONN")] ServiceBusReceivedMessage receivedMessage,
            ServiceBusMessageActions messageActions)
        {
            _logger.LogInformation("Received Message ID: {id}", receivedMessage.MessageId);
            _logger.LogInformation("Received Content-Type: {contentType}", receivedMessage.ContentType);
            _logger.LogInformation("Received Body: {body}", Encoding.UTF8.GetString(receivedMessage.Body.ToArray()));

            var outputData = new
            {
                ID = 123,
                Name = "Test"
            };

            string jsonString = JsonConvert.SerializeObject(outputData);
            byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonString);

            var serviceBusMessage = new ServiceBusMessage(jsonBytes)
            {
                ContentType = "application/json", 
                MessageId = Guid.NewGuid().ToString(),
                CorrelationId = receivedMessage.CorrelationId ?? Guid.NewGuid().ToString(),
                ReplyTo = receivedMessage.ReplyTo,
                SessionId = receivedMessage.SessionId,
                PartitionKey = receivedMessage.PartitionKey,
                TimeToLive = TimeSpan.FromMinutes(10)
            };
            serviceBusMessage.ApplicationProperties["ID"] = 123;
            serviceBusMessage.ApplicationProperties["Name"] = "Test";

            _logger.LogInformation("Final Sent Message Content-Type: {contentType}", serviceBusMessage.ContentType);
            var sender = _serviceBusClient.CreateSender("kamtopic");
            await sender.SendMessageAsync(serviceBusMessage);
            await messageActions.CompleteMessageAsync(receivedMessage);
        }
    }
}

Output :

Azure Service Bus Topic :

I successfully set the contentType as application/json and Custom Properties under Message Properties in the Topic.

It looks like using built-in output bindings we can not add ApplicationProperties. I end up using IAzureClientFactory<ServiceBusSender> to create instance of service bus topic client. And subsequently used ServiceBusMessage to send message to service bus topic by setting ApplicationProperties

builder.Services.AddAzureClients(builder =>
{
    builder.AddServiceBusClient(conn);
    builder.AddClient<ServiceBusSender, ServiceBusClientOptions>((_, _, sp) =>
    {
        var sbClient = sp.GetRequiredService<ServiceBusClient>();
        var sender = sbClient.CreateSender("test-topic");
        return sender;
    }).WithName("test-topic");
});
 public Function1(ILogger<Function1> logger, IAzureClientFactory<ServiceBusSender> sbSender)
 {    
     _clientFactory = sbSender;
 }

 [Function(nameof(Function1))]
 public async Task Run(
     [ServiceBusTrigger("test-queue", Connection = "CONN")]
     ServiceBusReceivedMessage message,
     ServiceBusMessageActions messageActions
     )
 {     
     OutputData outputData = new OutputData
     {
         ID = 123,
         Name = "Test"
     };
     #region Working - Use IAzureClientFactory to send message
     var topic = _clientFactory.CreateClient("test-topic");
     var serviceBusMessage = new ServiceBusMessage(JsonConvert.SerializeObject(outputData))
     {
         ContentType = "application/json"
     };
     serviceBusMessage.ApplicationProperties.Add("ID", 123);
     await topic.SendMessageAsync(serviceBusMessage);
     #endregion
     // Complete the message
     await messageActions.CompleteMessageAsync(message);
 }

Complete code is available here - azure-sb-topic-output-binding

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>