public static void Mutate(this IBaseMessage pInMsg, Dictionary <string, Func <string, string> > xPathToMutatorMap) { var xPathCollection = new XPathCollection(); foreach (var xPath in xPathToMutatorMap.Keys) { xPathCollection.Add(xPath); } Stream inboundStream = pInMsg.BodyPart.GetOriginalDataStream(); var virtualStream = new VirtualStream(inboundStream); ValueMutator valueMutator = delegate(int matchIdx, XPathExpression matchExpr, string origVal, ref string finalVal) { var mutator = xPathToMutatorMap[matchExpr.XPath]; finalVal = mutator(origVal); }; var xPathMutatorStream = new XPathMutatorStream(virtualStream, xPathCollection, valueMutator); pInMsg.BodyPart.Data = xPathMutatorStream; }
/// <summary> /// Synchronously executes the task using the specified <see cref="RuntimeTaskExecutionContext"/> execution context object. /// </summary> /// <param name="context">The execution context.</param> public override void Run(RuntimeTaskExecutionContext context) { string propName, propNamespace; // Check if there are any properties with values to be promoted. if (this.propertyToValueMap.Count > 0) { foreach (var property in this.propertyToValueMap) { if (TryParsePropertyName(property.Key, out propName, out propNamespace)) { #if DEBUG TraceManager.CustomComponent.TraceDetails("DETAIL: Context property promoted: {0}{1}{2} = {3}", propNamespace, BizTalkUtility.ContextPropertyNameSeparator, propName, property.Value); #endif context.Message.Context.Promote(propName, propNamespace, property.Value); } } } // Check if there are any properties to be promoted by inspecting the incoming message and capturing property values using XPath. if (this.propertyToXPathMap.Count > 0) { Stream messageDataStream = BizTalkUtility.EnsureSeekableStream(context.Message, context.PipelineContext); if (messageDataStream != null) { byte[] buffer = new byte[BizTalkUtility.VirtualStreamBufferSize]; string[] propValues = new string[this.propertyToXPathMap.Count]; int arrIndex = 0, matchFound = 0; XPathCollection xc = new XPathCollection(); XPathQueryLibrary xPathLib = ApplicationConfiguration.Current.XPathQueries; foreach (var property in this.propertyToXPathMap) { propValues[arrIndex++] = property.Key; xc.Add(xPathLib.GetXPathQuery(property.Value)); } try { XPathMutatorStream mutator = new XPathMutatorStream(messageDataStream, xc, delegate(int matchIdx, XPathExpression expr, string origValue, ref string finalValue) { if (matchIdx >= 0 && matchIdx < propValues.Length) { if (TryParsePropertyName(propValues[matchIdx], out propName, out propNamespace)) { #if DEBUG TraceManager.CustomComponent.TraceDetails("DETAIL: Context property promoted: {0}{1}{2} = {3}", propNamespace, BizTalkUtility.ContextPropertyNameSeparator, propName, origValue); #endif context.Message.Context.Promote(propName, propNamespace, origValue); } matchFound++; } }); while (mutator.Read(buffer, 0, buffer.Length) > 0 && matchFound < propValues.Length) { ; } } finally { if (messageDataStream.CanSeek) { messageDataStream.Seek(0, SeekOrigin.Begin); } } } } }
/// <summary> /// Synchronously executes the task using the specified <see cref="RuntimeTaskExecutionContext"/> execution context object. /// </summary> /// <param name="context">The execution context.</param> public override void Run(RuntimeTaskExecutionContext context) { if (this.lateBoundActivityValueMap.Count > 0) { foreach (var dateTimeMilestone in this.lateBoundActivityValueMap.Where((item) => { return(item.Value == typeof(DateTime)); })) { SetActivityData(dateTimeMilestone.Key, DateTime.UtcNow); } } if (this.milestoneToXPathMap.Count > 0) { Stream messageDataStream = BizTalkUtility.EnsureSeekableStream(context.Message, context.PipelineContext); if (messageDataStream != null) { byte[] buffer = new byte[BizTalkUtility.VirtualStreamBufferSize]; string[] milestones = new string[this.milestoneToXPathMap.Count]; int arrIndex = 0, matchFound = 0; XPathCollection xc = new XPathCollection(); XPathQueryLibrary xPathLib = ApplicationConfiguration.Current.XPathQueries; foreach (var listItem in this.milestoneToXPathMap) { milestones[arrIndex++] = listItem.Key; xc.Add(xPathLib.GetXPathQuery(listItem.Value)); } try { XPathMutatorStream mutator = new XPathMutatorStream(messageDataStream, xc, delegate(int matchIdx, XPathExpression expr, string origValue, ref string finalValue) { if (matchIdx >= 0 && matchIdx < milestones.Length) { SetActivityData(milestones[matchIdx], origValue); matchFound++; } }); while (mutator.Read(buffer, 0, buffer.Length) > 0 && matchFound < milestones.Length) { ; } // Check we had performed continuation token value resolution via XPath. object continuationToken = null; if (CurrentActivity.ActivityData.TryGetValue(ContinuationTokenInternalDataItemName, out continuationToken)) { CurrentActivity.ContinuationToken = (string)continuationToken; CurrentActivity.ActivityData.Remove(ContinuationTokenInternalDataItemName); } } finally { if (messageDataStream.CanSeek) { messageDataStream.Seek(0, SeekOrigin.Begin); } } } } }