How to update a digital twin object property using Azure Service Bus queue data - alternative to Microsoft.Azure.ServiceBus 5.2.0

GuidoL 310 Reputation points
2024-01-24T13:45:17.5466667+00:00

Hi, in the question about "How to update a digital twin object property using Azure Service Bus queue data from an Iot Central device" at https://learn.microsoft.com/en-us/answers/questions/1290036/how-to-update-a-digital-twin-object-property-using i accepted answer of @LeelaRajeshSayana-MSFT . I would like to point out, however, that I use Microsoft.Azure.ServiceBus 5.2.0 in IotCentralTrigger.cs file but the nuget packaged is deprecated and Visual Studio suggests that Azure.Messaging.ServiceBus should be used instead. Now if i run the code i obtain the ExpiresAtUtc = 'message.ExpiresAtUtc' ha generato un'eccezione di tipo 'System.InvalidOperationException' . Look at the following snaphosts. Debug: immagine Messages properties in Azure Service Bus queue: immagine

What would be an alternative code example to IotCentralTrigger.cs that now needs Microsoft.Azure.ServiceBus 5.2.0 to manage the following "Message message" istruction to avoid the error indicated?

 [FunctionName("IoTCentralTrigger")]
 public void Run([ServiceBusTrigger(queueName,
                                     Connection ="ServiceBusConnection")]
                                     Message message,
                                     ILogger log)
 {
    ...
   manager.UpdateDigitalTwinProperty(twinId, "LibraryItemDataProperty", LibraryItemDataObject);
}

Thanks in advance. Guido

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
211 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 12,121 Reputation points
    2024-01-25T21:07:56.8+00:00

    Hi @GuidoL Greetings! Thank you for posting the question here. There seems to be some changes in processing events between Microsoft.Azure.ServiceBus and Azure.Messaging.ServiceBus

    The deprecated package Microsoft.Azure.ServiceBus uses Encoding.UTF8.GetString(message.Body) to receive the message from the queue. To achieve the same in the Azure.Messaging.ServiceBus you can do the following.

    // get the message body as a string
    string body = receivedMessage.Body.ToString();
    
    

    Please also note that the nee package uses the message of type ServiceBusReceivedMessage and not Message. Here is a sample Azure function implementation of the new package.

    [Function(nameof(ServiceBusReceivedMessageFunction))]
    [ServiceBusOutput("outputQueue", Connection = "ServiceBusConnection")]
    public string ServiceBusReceivedMessageFunction(
        [ServiceBusTrigger("queue", Connection = "ServiceBusConnection")] ServiceBusReceivedMessage message)
    {
        _logger.LogInformation("Message ID: {id}", message.MessageId);
        _logger.LogInformation("Message Body: {body}", message.Body.ToString());
        _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
    
        var outputMessage = $"Output message created at {DateTime.Now}";
        return outputMessage;
    }
    

    There are no changes in the package that pushes the data to ADT. Once you get the data you can use the same rest of the code to push data. You can find more details on the changes between the packages from the Migration Guide.

    Hope this helps! Please let us know if you have any additional questions or need further assistance on this.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    1 person found this answer helpful.

  2. Sander van de Velde 27,046 Reputation points MVP
    2024-01-24T21:34:59.0566667+00:00

    Hello @GuidoL,

    welcome to this moderated Azure community forum.

    If I look at the screenshots you provide, that expiration date looks very strange:

    User's image

    I have the impression that the first plus sign is not valid in a regular UTC notation of a DateTime. Are you able to fix this and try again?


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.