public static string ToXml(this IBaseMessageContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // cache xmlns while constructing xml infoset... var nsCache = new XmlDictionary(); var xmlDocument = new XElement( "context", Enumerable.Range(0, (int)context.CountProperties).Select( i => { var value = context.ReadAt(i, out var name, out var ns); // give each property element a name of 'p' and store its actual name inside the 'n' attribute, which avoids // the cost of the name.IsValidQName() check for each of them as the name could be an xpath expression in the // case of a distinguished property return(name.IndexOf("password", StringComparison.OrdinalIgnoreCase) > -1 ? null : new XElement( (XNamespace)nsCache.Add(ns).Value + "p", new XAttribute("n", name), context.IsPromoted(name, ns) ? new XAttribute("promoted", true) : null, value)); })); // ... and declare/alias all of them at the root element level to minimize xml string size for (var i = 0; nsCache.TryLookup(i, out var xds); i++) { xmlDocument.Add(new XAttribute(XNamespace.Xmlns + "s" + xds.Key.ToString(CultureInfo.InvariantCulture), xds.Value)); } return(xmlDocument.ToString(SaveOptions.DisableFormatting)); }
/// <summary> /// /// </summary> /// <param name="config"></param> /// <param name="resolver"></param> /// <param name="resolution"></param> /// <returns></returns> private static Dictionary <string, string> ResolveStatic(IBaseMessageContext messageContext) { Dictionary <string, string> resolverDictionary = new Dictionary <string, string>(); string result = null; StringWriter stringWriter = null; XmlTextWriter xmlTextWriter = null; try { stringWriter = new StringWriter(); xmlTextWriter = new XmlTextWriter(stringWriter); xmlTextWriter.Formatting = Formatting.Indented; xmlTextWriter.WriteStartDocument(); xmlTextWriter.WriteStartElement("ContextProperties"); int num = 0; while ((long)num < (long)((ulong)messageContext.CountProperties)) { string propName; string propNamespace; string propValue = messageContext.ReadAt(num, out propName, out propNamespace).ToString(); xmlTextWriter.WriteStartElement("Property"); xmlTextWriter.WriteAttributeString("name", propName); xmlTextWriter.WriteAttributeString("namespace", propNamespace); xmlTextWriter.WriteString(propValue); xmlTextWriter.WriteEndElement(); num++; } xmlTextWriter.WriteFullEndElement(); xmlTextWriter.WriteEndDocument(); result = stringWriter.ToString(); } catch { throw; } finally { if (xmlTextWriter != null) { xmlTextWriter.Close(); } if (stringWriter != null) { stringWriter.Close(); stringWriter.Dispose(); } } resolverDictionary.Add("MessageContext", result); return(resolverDictionary); }
private string GetContextTransform(IBaseMessageContext context) { for (int i = 0; i < context.CountProperties; i++) { string name; string ns; object obj = context.ReadAt(i, out name, out ns); if (name == "XSLTransform") { context.Write(name, ns, null);//Remove context as we do not want it to interfere return((string)obj); } } return(null); }
/// <summary> /// Returns the Message Properties based on DbPropList /// </summary> /// <param name="context"></param> /// <returns></returns> protected string GetMessageProperties(IBaseMessageContext context) { //Get Msg Properties StringBuilder sb = new StringBuilder(); sb.AppendLine("<MessageProperties>"); string name; string nspace; for (int loop = 0; loop < context.CountProperties; loop++) { context.ReadAt(loop, out name, out nspace); string value = context.Read(name, nspace).ToString(); sb.AppendLine(string.Format("<{0}>{1}</{0}>", name, value)); } sb.AppendLine("</MessageProperties>"); return(sb.ToString()); }
}//Business Layer public List <ContextProperty> ReadContextProperty(IBaseMessage msg, string PropertyName, string Namespace) { IBaseMessageContext ctx = msg.Context; string name; string nspace; List <ContextProperty> contextProperties = new List <AtomicScope.ContextProperty>(); for (int i = 0; i < ctx.CountProperties; i++) { ctx.ReadAt(i, out name, out nspace); string PropertyValue = ctx.Read(name, nspace).ToString(); contextProperties.Add(new ContextProperty() { Name = name, NameSpace = nspace, Value = PropertyValue }); } return(contextProperties); }
public string ReadProperties(IBaseMessage msg) { IBaseMessageContext ctx = msg.Context; string name; string nspace; List <ContextProperty> contextProperties = new List <AtomicScope.ContextProperty>(); for (int i = 0; i < ctx.CountProperties; i++) { ctx.ReadAt(i, out name, out nspace); string value = ctx.Read(name, nspace).ToString(); contextProperties.Add(new ContextProperty() { Name = name, NameSpace = nspace, Value = value }); } return(JsonConvert.SerializeObject(contextProperties)); }//Business Layer
private static IBaseMessageContext CloneMessageContext(IBaseMessageContext context) { IBaseMessageContext clonedContext = MessageFactory.CreateMessageContext(); for (int i = 0; i < context.CountProperties; i++) { string propertyNamespace = String.Empty; string propertyName = String.Empty; object value = context.ReadAt(i, out propertyName, out propertyNamespace); if (context.IsPromoted(propertyName, propertyNamespace)) { clonedContext.Promote(propertyName, propertyNamespace, value); } else { clonedContext.Write(propertyName, propertyNamespace, value); } } return(clonedContext); }
/// <summary> /// Resolve implementation for use within a Pipeline component. This method is typically used with one of the ESB Pipeline components such as the Itinerary Selector, or ESB Dispatcher to resolve entities, such as itinerary, maps and endpoint addresses. This method invokes the BRE Policies that were configured through the Config and Resolver connection string values. /// </summary> /// <param name="config">string containing name, value property values</param> /// <param name="resolver">Resolver connection string</param> /// <param name="message">BizTalk IBaseMessage class which is used to pass to the BRE policies if configured properly</param> /// <param name="pipelineContext">BizTalk Pipeline configuration</param> /// <returns>Resolver Dictionary Collection containing resolved entries, such as itinerary name, map name, and endpoint address resolution values</returns> Dictionary <string, string> IResolveProvider.Resolve(string config, string resolver, IBaseMessage message, IPipelineContext pipelineContext) { Dictionary <string, string> dictionary; if (string.IsNullOrEmpty(config)) { throw new ArgumentNullException("config"); } if (string.IsNullOrEmpty(resolver)) { throw new ArgumentNullException("resolver"); } if (message == null) { throw new ArgumentNullException("message"); } if (pipelineContext == null) { throw new ArgumentNullException("pipelineContext"); } Resolution resolution = new Resolution(); XmlDocument document = new XmlDocument(); try { ResolverMgr.SetContext(resolution, message, pipelineContext); BRE bre = this.CreateResolverDescriptor(config, resolver); Stream originalDataStream = message.BodyPart.GetOriginalDataStream(); if (!originalDataStream.CanSeek) { ReadOnlySeekableStream stream2 = new ReadOnlySeekableStream(originalDataStream); message.BodyPart.Data = stream2; pipelineContext.ResourceTracker.AddResource(stream2); originalDataStream = stream2; } if (originalDataStream.Position != 0L) { originalDataStream.Position = 0L; } document.Load(originalDataStream); originalDataStream.Position = 0L; if (bre.useMsg && (message.BodyPart != null)) { if (bre.useMsgCtxt) { // Check for Message Context values string strMsgCtxtValues = bre.msgCtxtValues; if (string.IsNullOrEmpty(strMsgCtxtValues)) { // get all context values object objValue = null; ctxtValues.Clear(); IBaseMessageContext bmCtxt = message.Context; for (int i = 0; i < bmCtxt.CountProperties; i++) { string strName = string.Empty; string strNamespace = string.Empty; string key = string.Empty; try { objValue = bmCtxt.ReadAt(i, out strName, out strNamespace); key = string.Format("{0}#{1}", strNamespace, strName); if (objValue != null) { // check to see if already in collection if (!ctxtValues.ContainsKey(key)) { ctxtValues.Add(key, objValue.ToString()); } else { ctxtValues[key] = objValue.ToString(); } } else { if (!ctxtValues.ContainsKey(key)) { ctxtValues.Add(key, string.Empty); } else { ctxtValues[key] = string.Empty; } } } catch (Exception ex) { EventLogger.LogMessage(string.Format("Namespace: {0}\nName: {1}\n caused an exception:\n{2}\nThis item was not added to the collection.", strNamespace, strName, ex.Message), EventLogEntryType.Error, 1000); } } // Now ready body part properties for (int p = 0; p < message.PartCount; p++) { string partName = string.Empty; var part = message.GetPartByIndex(p, out partName); for (int pp = 0; pp < part.PartProperties.CountProperties; pp++) { string strName = string.Empty; string strNamespace = string.Empty; string key = string.Empty; try { objValue = part.PartProperties.ReadAt(pp, out strName, out strNamespace); key = string.Format("{0}#{1}", strNamespace, strName); if (objValue != null) { // check to see if already in collection if (!ctxtValues.ContainsKey(key)) { ctxtValues.Add(key, objValue.ToString()); } else { ctxtValues[key] = objValue.ToString(); } } else { if (!ctxtValues.ContainsKey(key)) { ctxtValues.Add(key, string.Empty); } else { ctxtValues[key] = string.Empty; } } } catch (Exception ex) { EventLogger.LogMessage( string.Format( "Namespace: {0}\nName: {1}\n caused an exception:\n{2}\nThis item was not added to the collection.", strNamespace, strName, ex.Message), EventLogEntryType.Error, 1000); } } } } else { // otherwise get specific context values by checking for | bool hasPipeDelimiter = strMsgCtxtValues.Contains("|"); string[] msgCtxtValues = null; string msgCtxtValue = string.Empty; if (hasPipeDelimiter) { msgCtxtValues = strMsgCtxtValues.Split('|'); object objValue = null; ctxtValues.Clear(); string key; foreach (string str in msgCtxtValues) { string[] ns_values = str.Split('#'); try { objValue = message.Context.Read(ns_values[1], ns_values[0]); key = str; if (objValue != null) { if (!ctxtValues.ContainsKey(key)) { ctxtValues.Add(key, objValue.ToString()); } else { ctxtValues[key] = objValue.ToString(); } } else { if (!ctxtValues.ContainsKey(key)) { ctxtValues.Add(key, string.Empty); } else { ctxtValues[key] = string.Empty; } } } catch (Exception ex) { EventLogger.LogMessage(string.Format("Namespace: {0}\nName: {1}\n caused an exception:\n{2}\nThis item was not added to the collection.", ns_values[0], ns_values[1], ex.Message), EventLogEntryType.Error, 1000); } } } else { object objValue; string key; string[] ns_Values = new string[] { string.Empty, string.Empty }; try { ns_Values = strMsgCtxtValues.Split('#'); objValue = message.Context.Read(ns_Values[1], ns_Values[0]); key = strMsgCtxtValues; if (objValue != null) { if (!ctxtValues.ContainsKey(key)) { ctxtValues.Add(key, objValue.ToString()); } } else { if (!ctxtValues.ContainsKey(key)) { ctxtValues.Add(key, string.Empty); } } } catch (Exception ex) { EventLogger.LogMessage(string.Format("Namespace: {0}\nName: {1}\n caused an exception:\n{2}\nThis item was not added to the collection.", ns_Values[0], ns_Values[1], ex.Message), EventLogEntryType.Error, 1000); } } } } } dictionary = ResolveRules(config, resolver, document, resolution, bre, ctxtValues, pipelineContext, ref message); } catch (Exception exception) { EventLogger.Write(MethodBase.GetCurrentMethod(), exception); throw; } finally { if (resolution != null) { resolution = null; } } return(dictionary); }
public static void ArchivetoStorage(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg, string FullFilePath, bool ReadFullContext) { StringBuilder SBContext = new StringBuilder(); ReadOnlySeekableStream stream = new ReadOnlySeekableStream(inmsg.BodyPart.GetOriginalDataStream()); Stream sourceStream = inmsg.BodyPart.GetOriginalDataStream(); List <JsonContextProperty> jsonContextpropertylist = new List <JsonContextProperty>(); if (!sourceStream.CanSeek) { ReadOnlySeekableStream seekableStream = new ReadOnlySeekableStream(sourceStream); inmsg.BodyPart.Data = seekableStream; sourceStream = inmsg.BodyPart.Data; } if (inmsg.BodyPart != null) { string json = "NA"; VirtualStream virtualStream = new VirtualStream(sourceStream); //ArchiveToFileLocation(virtualStream, FullFilePath); //virtualStream.Seek(0, SeekOrigin.Begin); inmsg.BodyPart.Data = virtualStream; if (ReadFullContext == true) { IBaseMessageContext ctx = inmsg.Context; string name; string nspace; for (int loop = 0; loop < ctx.CountProperties; loop++) { ctx.ReadAt(loop, out name, out nspace); string value = ctx.Read(name, nspace).ToString(); jsonContextpropertylist.Add(new JsonContextProperty() { Name = name, NameSpace = nspace, Value = value }); } json = JsonConvert.SerializeObject(jsonContextpropertylist); } string InterchangeID = inmsg.Context.Read("InterchangeID", "http://schemas.microsoft.com/BizTalk/2003/system-properties").ToString(); string ServiceName = "NA"; if (inmsg.Context.Read("ReceivePortID", "http://schemas.microsoft.com/BizTalk/2003/system-properties") != null) { ServiceName = inmsg.Context.Read("ReceiveLocationName", "http://schemas.microsoft.com/BizTalk/2003/system-properties").ToString(); } else if (inmsg.Context.Read("SPID", "http://schemas.microsoft.com/BizTalk/2003/system-properties") != null) { ServiceName = inmsg.Context.Read("SPName", "http://schemas.microsoft.com/BizTalk/2003/system-properties").ToString(); } string msgId = inmsg.MessageID.ToString(); SqlConnection Conn = new SqlConnection("Server=BT360DEV34\\MSSQLSERVER1;Database=B360_BAM;Integrated Security=True;"); SqlCommand command = new SqlCommand( "INSERT INTO [dbo].[CustomTrackTbl] " + "([ActivityID] " + ",[CorrelationID] " + ",[StartTime] " + ",[EndTime] " + ",[ServiceName] " + ",[MsgContext] " + ",[StreamMsgPayload] " + ",[RawMsgPayload] " + ",[Status] " + ",[MessageArchiveLocation])" + "VALUES(@ActivityID,@CorrelationID,@StartTime,@EndTime,@ServiceName,@MsgContext,@StreamMsgPayload,@RawMsgPayload,@Status,@MessageArchiveLocation)", Conn); command.Parameters.Add("@ActivityID", SqlDbType.VarChar).Value = msgId; command.Parameters.Add("@CorrelationID", SqlDbType.VarChar).Value = InterchangeID; command.Parameters.Add("@StartTime", SqlDbType.VarChar).Value = DateTime.Now.ToString(); command.Parameters.Add("@EndTime", SqlDbType.VarChar).Value = DateTime.Now.ToString(); command.Parameters.Add("@ServiceName", SqlDbType.VarChar).Value = ServiceName; command.Parameters.Add("@MsgContext", SqlDbType.VarChar).Value = json; sourceStream.Position = 0; command.Parameters.Add("@StreamMsgPayload", SqlDbType.Binary).Value = sourceStream; command.Parameters.Add("@RawMsgPayload", SqlDbType.VarChar).Value = "NA"; command.Parameters.Add("@Status", SqlDbType.VarChar, 255).Value = "Success"; command.Parameters.Add("@MessageArchiveLocation", SqlDbType.VarChar).Value = FullFilePath; Conn.Open(); command.ExecuteNonQuery(); Conn.Close(); } }
private IBaseMessage BuildResponseMessage(IBaseMessage message, IBaseMessageContext context, LoopBackTransmitProperties props) { Guid callToken = TraceManager.CustomComponent.TraceIn(); long startScope = TraceManager.CustomComponent.TraceStartScope("BuildResponseMessage", callToken); IBaseMessageFactory messageFactory = _transportProxy.GetMessageFactory(); IBaseMessage btsResponse = messageFactory.CreateMessage(); TraceManager.CustomComponent.TraceInfo("PropertyCopy: {0}", props.PropertyCopy); if (props.PropertyCopy) { btsResponse.Context = PipelineUtil.CloneMessageContext(context); } TraceManager.CustomComponent.TraceInfo("CustomPropertyCopy: {0}", props.CustomPropertyCopy); if (props.CustomPropertyCopy) { btsResponse.Context = messageFactory.CreateMessageContext(); for (int i = 0; i < context.CountProperties; i++) { string strName; string strNamespace; object oValue = context.ReadAt(i, out strName, out strNamespace); if (!strNamespace.StartsWith("http://schemas.microsoft.com/BizTalk")) { if (context.IsPromoted(strName, strNamespace)) { TraceManager.CustomComponent.TraceInfo("Promoted into context: {1}#{0}={2}", strName, strNamespace, oValue); btsResponse.Context.Promote(strName, strNamespace, oValue); } else { TraceManager.CustomComponent.TraceInfo("Copied into context: {1}#{0}={2}", strName, strNamespace, oValue); btsResponse.Context.Write(strName, strNamespace, oValue); } } } } TraceManager.CustomComponent.TraceInfo("PartCount: {0}", message.PartCount); for (int i = 0; i < message.PartCount; i++) { string str; VirtualStream stream = new VirtualStream(); StreamReader rdr = new StreamReader(message.GetPartByIndex(i, out str).GetOriginalDataStream(), true); StreamWriter wrtr = new StreamWriter(stream, rdr.CurrentEncoding); wrtr.Write(rdr.ReadToEnd()); rdr.Close(); wrtr.Flush(); stream.Seek(0, SeekOrigin.Begin); IBaseMessagePart part = messageFactory.CreateMessagePart(); if (props.PropertyCopy) { part.Charset = message.GetPart(str).Charset; part.ContentType = message.GetPart(str).ContentType; part.PartProperties = PipelineUtil.CopyPropertyBag(message.GetPart(str).PartProperties, messageFactory); } btsResponse.AddPart(str, part, message.GetPart(str).PartID.Equals(message.BodyPart.PartID)); btsResponse.GetPart(str).Data = stream; } TraceManager.CustomComponent.TraceEndScope("BuildResponseMessage", startScope, callToken); TraceManager.CustomComponent.TraceOut(callToken); return(btsResponse); }