/// <summary> /// Transforms original stream using streaming scalable transformation from BizTalk API. /// </summary> /// <param name="inputStream"></param> /// <returns></returns> internal Stream TransformMessage(Stream inputStream, TransformMetaData map, IBaseMessage pInMsg) { XsltArgumentList args = null; Context ext = null; SchemaMetadata targetSchema = targetSchema = map.TargetSchemas[0]; string portname = String.Empty; //It is possible to add a param but then you you need to work arounds in the map // map.ArgumentList.AddParam //The statement bellow caused me some problems that was solved by creating the XsltArgumentList instead //args = map.ArgumentList; try { ext = new Context(pInMsg.Context, pInMsg.MessageID.ToString()); args = map.ArgumentList;//Include BizTalk extensions //args.AddExtensionObject("http://www.w3.org/1999/XSL/Transform", ext); strangely it seams i cannot use this namespace in vs 2012, but it worked in vs 2010 args.RemoveExtensionObject("urn:schemas-microsoft-com:xslt"); args.AddExtensionObject("urn:schemas-microsoft-com:xslt", ext); AddParameters(args); //2017-08-23 Added intermidiate stream as Transform kills the original stream, //this is a problem if the incomming message is a enveloped message. // XmlTranslatorStream stm = new XmlTranslatorStream(XmlReader.Create(inputStream)); //Opted to use non disposable stream instead NonDisposableStream stm = new NonDisposableStream(inputStream); VirtualStream outputStream = new VirtualStream(VirtualStream.MemoryFlag.AutoOverFlowToDisk); //TODO test to add declaration and see what happens with params!!!!!!!!!!!!!!!!!!!!!!!! BTSXslTransform btsXslTransform = null; if (Transforms.ContainsKey(map)) { btsXslTransform = Transforms[map]; } else { btsXslTransform = new BTSXslTransform(); XmlTextReader xmlTextReader = new XmlTextReader((TextReader) new StringReader(map.XmlContent)); btsXslTransform.Load((XmlReader)xmlTextReader, new MemoryResourceResolver(map.Assembly), (System.Security.Policy.Evidence)null); Transforms.TryAdd(map, btsXslTransform); } btsXslTransform.Transform(stm, args, outputStream, null); outputStream.Seek(0, SeekOrigin.Begin); if (_dynamicMap) { pInMsg.Context.Write("XSLTransform", "PipelineComponents.XSLTransform", map.AssemblyQualifiedName); } pInMsg.Context.Promote("MessageType", _systemPropertiesNamespace, targetSchema.SchemaName); //Changed to Write as SchemaStrongName could exceed 255 chars pInMsg.Context.Write("SchemaStrongName", _systemPropertiesNamespace, targetSchema.ReflectedType.AssemblyQualifiedName); //pInMsg.MessageID.ToString() return(outputStream); } catch (Exception ex) { throw new ApplicationException($"Error while trying to transform using MapType specification: {_mapName}\nMap Assembly {map.AssemblyQualifiedName} used\nError {ex.Message}", ex); } }
/// <summary> /// Transforms original stream using streaming scalable transformation from BizTalk API. /// </summary> /// <param name="inputStream"></param> /// <returns></returns> internal Stream TransformMessage(Stream inputStream, TransformMetaData map, IBaseMessage pInMsg) { XsltArgumentList args = null; Context ext = null; SchemaMetadata targetSchema = targetSchema = map.TargetSchemas[0]; string portname = String.Empty; //It is possible to add a param but then you you need to work arounds in the map // map.ArgumentList.AddParam //The statement bellow caused me some problems that was solved by creating the XsltArgumentList instead //args = map.ArgumentList; try { ext = new Context(); for (int i = 0; i < pInMsg.Context.CountProperties; i++) { string name; string ns; string value = pInMsg.Context.ReadAt(i, out name, out ns).ToString(); ext.Add(name, value, ns); if (m_portDirection == PortDirection.receive && name == "ReceivePortName") { portname = value; } else if (m_portDirection == PortDirection.send && name == "SPName") { portname = value; } } //It is possible to add any information that should be available from the map ext.Add("MessageID", pInMsg.MessageID.ToString()); args = new XsltArgumentList(); //args.AddExtensionObject("http://www.w3.org/1999/XSL/Transform", ext); strangely it seams i cannot use this namespace in vs 2012, but it worked in vs 2010 args.AddExtensionObject("urn:schemas-microsoft-com:xslt", ext); AddParameters(args); //2017-08-23 Added intermidiate stream as Transform kills the original stream, //this is a problem if the incomming message is a enveloped message. XmlTranslatorStream stm = new XmlTranslatorStream(XmlReader.Create(inputStream)); VirtualStream outputStream = new VirtualStream(VirtualStream.MemoryFlag.AutoOverFlowToDisk); XmlTextReader xmlTextReader = new XmlTextReader((TextReader) new StringReader(map.XmlContent)); BTSXslTransform btsXslTransform = new BTSXslTransform(); btsXslTransform.Load((XmlReader)xmlTextReader, new MemoryResourceResolver(portname, m_portDirection), (System.Security.Policy.Evidence)null); btsXslTransform.Transform(stm, args, outputStream, null); outputStream.Seek(0, SeekOrigin.Begin); pInMsg.Context.Promote("MessageType", _systemPropertiesNamespace, targetSchema.SchemaName); pInMsg.Context.Promote("SchemaStrongName", _systemPropertiesNamespace, targetSchema.ReflectedType.AssemblyQualifiedName); //pInMsg.MessageID.ToString() return(outputStream); } catch (Exception ex) { throw new ApplicationException(string.Format("Error while trying to transform using MapType specification: {0}", _mapName), ex); } }