I spent 30 minutes or so on it and was able to do it. IFindSagas lets you look into message headers.
Issues I found:
- I couldn’t get the ReadOnlyContextBag to return header properties, so I had to cast it to an IMessageHandlerContext. Then I was able to get access to the message headers.
- Since JSON_VALUE does not exist in SQL 2014, I had to use SUBSTRING and CHARINDEX to find the value in the Data column. This has poor performance, but, in my case, at most 1 or 2 of these saga’s will be running at a time, so it’s acceptable.
- IF I tried to do custom mapping for an IAmStartedBy message, then lots of the properties do not get filled in. These include the property I was mapping on as well as the CorrelationId property for the SagaData table.
I have made the Substring search into a SQL Function.
"@propertyValue = SUBSTRING(Data, CHARINDEX(’“propertyName”: "’, Data) + LEN(’“propertyName”: “’), CHARINDEX(’”’, Data, CHARINDEX(’“propertyName”: "’, Data) + LEN(’“propertyName”: "’)) - CHARINDEX(’“propertyName”: "’, Data) - LEN(’“propertyName”: “’))”,
So it’s all do-able EXCEPT for StartedByMessages; as I do not know of any way to set the CorrelationId, it’s not exposed in the SagaData class.
System.Exception: Failed to ExecuteNonQuery. CommandText:
insert into [NServiceBus].[mySaga]
) —> System.Data.SqlClient.SqlException: Must declare the scalar variable “@CorrelationId”.