skip to Main Content

I need to post messages from Dynamics 365 to a Azure service bus topic and process that via a Azure function reading from a topic subscription. I have successfully achieved that. The messages that are posted from dynamics come from two types of operations: Create and Update.

I need to set up filters on the topics so that each of the topics (one for create and another for update) have only the messages that satisfy the filter criteria. The message payload coming from Dynamics (via Plugin Registration Tool Service EndPoint Registration) have an attribute indicating if the payload is from a create or update operation. I understand that the filters on topics can be set only for message headers and not the payload.end point registration screenshot

Is there any attribute in the message header which indicates that the message is from a Create request or Update request? Or is there a way to look at the message coming from dynamics to the service bus along with its headers?

2

Answers


  1. Chosen as BEST ANSWER

    After a message gets to the Azure Service bus (configuration for which is shared in the question post), the preview feature allows us to peek at the messages that have arrived. We also have the option to look at the message properties, refer screenshot Message Propereties. From there, I could figure out that there was custom property which was indicating if the message was a Create or Update and by using that I setup a SQL filter and the messages were getting filtered successfully.


  2. Is there any attribute in the message header which indicates that the message is from a Create request or Update request?

    Question relates to Azure service bus and I think there is no such way (as per my understanding).

    Now coming to your requirement, you need create message from crm to go on Create Topic (service bus) and same way with Update message. This can be achieved, you will have to register plugin on create and update trigger for particular entity in crm. In this plugin you will have to connect with your Azure service bus correct topic which you want with SASKey (there are numerous examples) and push the crm message to your service bus topic.

    below pseudo code for understanding to send object to service bus. please don’t take as it is, just to show this can be achieved and we did this in one of our project.

    public bool SendObjectToBus<TBusObject>(TBusObject serializedObject, HttpMessageHandler messagHandler = null) 
                where TBusObject: IBusObject
            {
               
                if (string.IsNullOrEmpty(_sasToken) || validUntil >= DateTime.UtcNow - new DateTime(1970, 1, 1))
                {
                    _sasToken = GetSasToken(new TimeSpan(0, 0, 90));
                }
    
                string json = JsonSerializer.Serialize(serializedObject);
    
                HttpClient client = messagHandler == null ? new HttpClient() : new HttpClient(messagHandler);
                client.DefaultRequestHeaders.Add("Authorization", _sasToken);
                using (var Content = new StringContent(json, Encoding.UTF8, "application/json"))
                {
                    _headerContext.AddHeaders(Content.Headers, ApiSettings.EnvironmentType);
                    
                    //this._tracingService.Trace("Sending Message to Servicebus");
                    var response = client.PostAsync(ApiSettings.GetTopicURL(), Content);
                    response.Wait();
    
                    return response.Result.IsSuccessStatusCode;
                }
                //this._tracingService.Trace("Message sent");
                //this._tracingService.Trace(response.Result.StatusCode.ToString());
            }
    
            private string GetSasToken(TimeSpan validRange)
            {
                TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
                validUntil = sinceEpoch + validRange;
    
                string expiry = Convert.ToString(validUntil.TotalSeconds);
                string stringToSign = Uri.EscapeDataString(ApiSettings.ServiceBusNamespaceUrl).ToLowerInvariant() + "n"
                     + expiry;
                HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(ApiSettings.SASValue));
    
                var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
                var sasToken = string.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
                    Uri.EscapeDataString(ApiSettings.ServiceBusNamespaceUrl).ToLowerInvariant(),
                    Uri.EscapeDataString(signature),
                    expiry,
                    ApiSettings.SASKey);
    
                return sasToken;
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search