简体   繁体   English

筛选从 Dynamics 365 发布到 Azure 服务总线的消息

[英]Filtering of messages posted from Dynamics 365 to Azure Service Bus

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.我需要将来自 Dynamics 365 的消息发布到 Azure 服务总线主题,并通过从订阅中读取的 Azure function 处理该主题。 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.来自 Dynamics 的消息负载(通过插件注册工具服务端点注册)具有指示负载是来自创建还是更新操作的属性。 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?消息 header 中是否有任何属性表明该消息来自创建请求或更新请求? Or is there a way to look at the message coming from dynamics to the service bus along with its headers?或者有没有办法查看从动态到服务总线的消息及其标头?

Is there any attribute in the message header which indicates that the message is from a Create request or Update request?消息 header 中是否有任何属性表明该消息来自创建请求或更新请求?

Question relates to Azure service bus and I think there is no such way (as per my understanding).问题与 Azure 服务总线有关,我认为没有这种方法(根据我的理解)。

Now coming to your requirement, you need create message from crm to go on Create Topic (service bus) and same way with Update message.现在满足您的要求,您需要在创建主题(服务总线)上创建从 crm 到 go 的消息,并且与更新消息相同。 This can be achieved, you will have to register plugin on create and update trigger for particular entity in crm.这可以实现,您必须在 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.在此插件中,您必须使用 SASKey 连接您想要的 Azure 服务总线正确主题(有很多示例)并将 crm 消息推送到您的服务总线主题。

below pseudo code for understanding to send object to service bus.下面的伪代码用于理解将 object 发送到服务总线。 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;
        }

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.在消息到达 Azure 服务总线后(配置在问题帖子中共享),预览功能允许我们查看已到达的消息。 We also have the option to look at the message properties, refer screenshot Message Propereties .我们还可以选择查看消息属性,请参阅屏幕截图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.从那里,我可以发现有一个自定义属性指示消息是创建还是更新,并使用它设置了 SQL 过滤器,并且消息被成功过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM