-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Conditional for a specific Message Value to control Message Routing in Azure Service Bus #6
Comments
@jkears That's awesome that you are getting value out of this and enjoying it... 🙌 So you are on the right track! The original design was that the message sender could/should control various aspects dynamically. And since the So if your payload has any Json properties (objects) in the root of the payload with the name You can see in the null coalescing fallback logic for these three dynamic Json object nodes in the //Populate HeadersLookup/User Properties if defined dynamically...
var headers = GetJsonValueSafely<JObject>(json, JsonMessageFields.Headers)
?? GetJsonValueSafely<JObject>(json, JsonMessageFields.AppProperties)
?? GetJsonValueSafely<JObject>(json, JsonMessageFields.UserProperties); At this time there is not a configurable way to intercept and mutate the Json payload to move/copy the So whatever code creates and stores your messages can simply preprocess it with something like this (not tested): //Parse the payload if it's a string...
var json = JObject.Parse(messagePayload);
//Dynamically extract and copy the value into a new `headers` node of the payload...
var nameCheck = json.SelectToken("$.data.AuthenticationDefinition.Name")?.ToString();
if (nameCheck != null)
{
headersJson = new JObject();
headersJson["NameCheck"] = nameCheck;
json["headers"] = headersJson;
}
//Now re-serialize to json string...
messagePayload = json.ToString(); I'm sure you already know this but here's the MS docs discussing how to filter based on |
@cajuncoding, thank you for your prompt support. As a quick test I added the following header and Correlation rule, and it worked perfectly.... It may not be so obvious but I am using CloudEvents which promotes a specific schema, and so ideally, it would be great if somehow we could push the headers section into the Specifically, this is what is defined...
It would be idea for this to be a configuration driven value such that I could place headers in the Data Payload, such as " Is that possible with the existing code base and without modifying any code? |
@jkears Ok, I sort of mis-spoke in my response above... so I edited to clarify. There is no configurable way to intercept the json and mutate it (e.g. re-locating a value into the headers node). But there is definitely an elegant solution 😁! However, as an alternative, and by design, the version intentionally allows you to create a customized version of the So rather than using the Here's an sample (most of the below are code comments you can remove): public class MyCustomAzureServiceBusOutboxPublisher : BaseAzureServiceBusPublisher<Guid>
{
public MyCustomAzureServiceBusOutboxPublisher(string azureServiceBusConnectionString, AzureServiceBusPublishingOptions options = null)
: base(azureServiceBusConnectionString, options)
{
//All logic is currently in the Base Constructor
}
protected override ServiceBusMessage CreateEventBusMessage(ISqlTransactionalOutboxItem<Guid> outboxItem)
{
//First parse our input because it's easier to access the Payload before
// it's converted to Binary within the Message...
var payloadJson = base.ParsePayloadAsJsonSafely(outboxItem);
//Now create let the Default Outbox handler create the message...
var eventBusMessage = base.CreateEventBusMessage(outboxItem);
//Now we can update it with custom Applicatoin properties without affecting
// the Payload structure (e.g. no Headers node/section)...
var nameCheck = payloadJson.SelectToken("$.data.AuthenticationDefinition.Name")?.ToString();
eventBusMessage.ApplicationProperties.Add(MessageHeaders.ToHeader("data_AuthenticationDefinition_Name"), nameCheck);
//Finally return the customized message...
return eventBusMessage;
}
} |
Thank you so very much, this works like a charm! |
Love this library, working very well so far.
How can I publish a custom Property that I can use to filter my message.
For example this is a sample test message.. and I'd like only messages with the field data.Authentication.Name set equal to "Jimmy" to be forwarded to the subscriber ....
I see in the BaseAzureServiceBusPublisher class it appears to be adding a number of ApplicationProperties for ProcessorType, ProcessSender etc...
It also seems to be pulling from a headers section and adding Application Properties per each.
Is there a way of extracting the value for data.Authentication.Name property from the message and adding it to "NameCheck" such that I can add a rule in Azure Service bus such as:
NameCheck = 'Jimmy'
.The text was updated successfully, but these errors were encountered: