none
Microsoft.BizTalk.Operations; RRS feed

  • Question

  • I am trying to automate to pull suspended messages from BizTalk and display on a custom screen, works support team have visibility. I had written following code for that.workswell in my local and test where I have single machine environment. But it gives the error when I use production where I have the seperate database server with two instances on SQL server (one instance run all DBs except MsgBox and other instance have just MsgBox). Other difference is host instances are running with a different user than admin user.But I try to run my job with both users but still it error out,e-ven I try to run from command line,b ut still same. Here are error and code. Please let me know if you know the solution , I am struggling with this from yesterday.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.BizTalk.Operations;
    using System.IO;
    using System.Configuration;
    using Mycompany.TradingPartners.EF;
    using System.Diagnostics;
    using Microsoft.BizTalk.Message.Interop;

    namespace Mycompany.LogSuspenededErrors
    {
        public static class EDISuspendedMessage
        {
            public static void GetMessageWithOperations()
            {
                string body = string.Empty;
                string errMsg = string.Empty;
                string tpName = string.Empty;
                string sourceFile = string.Empty;
                string failedMsg = string.Empty;
                string direction = string.Empty;
               

                string DBServerName = ConfigurationManager.AppSettings["DBServerName"];
                string DBName = ConfigurationManager.AppSettings["DBName"];
                string FileServer = ConfigurationManager.AppSettings["FileServer"];
                try
                {

                    TrackingDatabase dta = new TrackingDatabase(DBServerName, DBName);
                    BizTalkOperations operations = new BizTalkOperations();
                    List<EDITransError> ediErrs = new List<EDITransError>();

                  
                    foreach (BizTalkMessage message in operations.GetMessages())
                    {
                        
                        EDITransError ediTransErr = new EDITransError();
                        ediTransErr.ErrMsg = message.ErrorDescription;
                        ediTransErr.LoggingTime = message.CreationTime;
                        ediTransErr.ErrMsgId = message.Context.Read("InterchangeID","http://schemas.microsoft.com/BizTalk/2003/system-properties").ToString();
                        ediTransErr.ErrInsertTime = DateTime.Now;
                        ediTransErr.ProcessingStatus = "Error";

                        string msgType = message.Context.Read("MessageType", "http://schemas.microsoft.com/BizTalk/2003/system-properties").ToString();
                        if (msgType.CompareTo("http://schemas.microsoft.com/Edi/X12#12_TA1_Root") == -1)
                        {

                            //if (
                            //    msgType.CompareTo("http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_832") >= 0
                            //    || msgType.CompareTo("http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_855") >= 0
                            //    || msgType.CompareTo("http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_856") >= 0
                            //    || msgType.CompareTo("http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_810") >= 0
                            //    || msgType.CompareTo("http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_870") >= 0
                            //    || msgType.CompareTo("http://schemas.microsoft.com/BizTalk/EDI/X12/2006#X12_00401_846") >= 0
                            //    )
                            //{

                                Dictionary<string, string> msgProperties = GetAllContextProperties(message);
                                //inbound
                                if (msgProperties.ContainsKey("ST01"))
                                {

                                    string ST01Element = message.Context.Read("ST01", "http://schemas.microsoft.com/Edi/PropertySchema").ToString();
                                    //inbound only
                                    if (!string.IsNullOrEmpty(ST01Element))
                                    {
                                        string GS02Element = message.Context.Read("GS02", "http://schemas.microsoft.com/Edi/PropertySchema").ToString();
                                        // GS receiverid
                                        string GS03Element = message.Context.Read("GS03", "http://schemas.microsoft.com/Edi/PropertySchema").ToString();
                                        // ISA SenderId
                                        string ISA06Element = message.Context.Read("ISA06", "http://schemas.microsoft.com/Edi/PropertySchema").ToString();
                                        // ISA ReceiverId 
                                        string ISA08Element = message.Context.Read("ISA08", "http://schemas.microsoft.com/Edi/PropertySchema").ToString();

                                        string srcFileName = message.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties").ToString();

                                        ediTransErr.TradingPartner = ISA06Element + " " + GS02Element + " " + ISA08Element + " " + GS03Element;
                                        //ediTransErr.SourceFile = srcFileName;
                                        ediTransErr.EDITrans = ST01Element;
                                        ediTransErr.Direction = "Inbound";

                                        //ediTransErr.SourceFile = ReplaceFilePath(srcFileName, FileServer);
                                        ediTransErr.SourceFile = GetArchiveFilePath(srcFileName, ST01Element);
                                        ediTransErr.ProcessingPath = GetProcessingFilePath(ST01Element);

                                    }
                                }
                                //}
                                    //outbound
                                else
                                {
                                   
                                    string partyName = message.Context.Read("PartyName", "http://schemas.microsoft.com/BizTalk/2003/messagetracking-properties").ToString();
                                    ediTransErr.TradingPartner = partyName;
                                    ediTransErr.EDITrans = msgType.Substring(msgType.Length - 3, 3);
                                    using (StreamReader streamReader = new StreamReader(message.BodyPart.Data))
                                    {
                                        body = streamReader.ReadToEnd();

                                    }
                                    ediTransErr.FailedMsg = body;
                                    ediTransErr.Direction = "Outbound";
                                    EventLog.WriteEntry("EDIErrors", message.ErrorDescription);
                                }
                        }
                        //check if this message already inserted in to log table
                        string errMsgId = EDIX12XRefHelper.GetSuspendMsgId(ediTransErr.ErrMsgId);
                       
                        if (string.IsNullOrEmpty(errMsgId))
                        {
                            ediErrs.Add(ediTransErr);
                        }
                    }
                    
                    EDIX12XRefHelper.PersistEDIErrorCollection(ediErrs, 3);
                }
                catch (Exception exOp)
                {
                    EventLog.WriteEntry("EDIErrors", "Error :" + exOp.Message + "stack:" + exOp.StackTrace, EventLogEntryType.Error);
                }
            }

            private static Dictionary<string,string> GetAllContextProperties(BizTalkMessage message)
            {
                string propName;
                string propNamespace;
                Dictionary<string, string> ctxProperties = new Dictionary<string,string>();
                for (int i = 0; i < message.Context.CountProperties; i++)
                {
                    message.Context.ReadAt(i, out propName, out propNamespace);
                    //avoid duplicate
                    if (!propNamespace.Contains("https://Mycompany._870.EDIProperty"))
                    {

                        ctxProperties.Add(propName, propNamespace);
                    }

                }
                return ctxProperties;
            }

            private static string ReplaceFilePath(string rcvPath, string srvName, out string uncPath)
            {
                //var builder = new UriBuilder(rcvPath);
                //builder.Host = srvName;
                //return builder.Uri.ToString();


                string pathWithoutDrive = rcvPath.Substring(Path.GetPathRoot(rcvPath).Length, rcvPath.Length - Path.GetPathRoot(rcvPath).Length);
                //string s = rcvPath.Replace(Path.GetPathRoot(rcvPath),srvName);
                string pathWithServerName = @"\" + srvName + @"\" + pathWithoutDrive;

                uncPath = pathWithServerName.Substring(0, pathWithoutDrive.Length - Path.GetFileName(pathWithServerName).Length);
                string replacedPath = uncPath + "Archive" + @"\" + Path.GetFileName(pathWithServerName);
                return replacedPath;
                
            }

            private static string GetArchiveFilePath(string rcvPath, string ediTransCode)
            {

                string arcvPathForTran = ConfigurationManager.AppSettings[ediTransCode];

                string finalPath = arcvPathForTran + @"\" + Path.GetFileName(rcvPath);
                return finalPath;
            }

            private static string GetProcessingFilePath(string ediTransCode)
            {
                string arcvPathForTran = ConfigurationManager.AppSettings[ediTransCode];

                DirectoryInfo parentDir = Directory.GetParent(arcvPathForTran);

                return parentDir.FullName;

            }

           


        }
    }


    Error is 

    Error :Object reference not set to an instance of an object.stack:   at Microsoft.BizTalk.Operations.ItemEnumerator.LoadItems()
       at Microsoft.BizTalk.Operations.ItemEnumerator.MoveNext()
       at Zonses.LogSuspenededErrors.EDISuspendedMessage.GetMessageWithOperations()



    • Edited by dnyanbhar Friday, April 7, 2017 5:24 PM
    Friday, April 7, 2017 5:21 PM

All replies

  • On what line is it erroring?

    The user this runs under has to be a member of the BizTalk Administrators or equivalent Group.

    Friday, April 7, 2017 5:29 PM
    Moderator
  • This is the statement for error operations.GetMessages()

    Yes, user I tried is the admin on the server . 


    Friday, April 7, 2017 5:39 PM
  • For clarity, it's not server admin it needs, it BizTalk Administrator for the Group.
    Friday, April 7, 2017 6:20 PM
    Moderator
  • What u r trying to pull , is it equivalent to what u see from admin console?

    Pi_xel_xar

    Blog: My Blog

    BizTalkApplicationDeploymentTool: BizTalk Application Deployment Tool/

    Friday, April 7, 2017 6:25 PM
    Answerer
  • I check it and user I am trying to configure this job is part of Biztalk admin group, but user which are running host instances are not part of it. 
    Friday, April 7, 2017 6:28 PM
  • yes for visibility,I am pulling that info and adding in custom table where we will have web UI for support team.
    Friday, April 7, 2017 6:59 PM
  • I think I understood the problem but I do not know a solution. <g class="gr_ gr_21 gr-alert gr_spell gr_disable_anim_appear gr_inline_cards ContextualSpelling ins-del multiReplace" data-gr-id="21" id="21">Getmessages</g>() methods return all messages from message box, so I should connect to a message box. But I am not able to find a way to connect message box. Operation constructor support to comment managementDB, but I did not find any method where I can configure info for the message box. It works in my test environment because I guess implicitly it looks <g class="gr_ gr_22 gr-alert gr_spell gr_disable_anim_appear gr_inline_cards ContextualSpelling ins-del multiReplace" data-gr-id="22" id="22">messageboxdb</g> on the same box and it found it. In production, they are different boxes and need some way to configure it, but I can not. Please let me know is this could be an issue and how I can resolve it?




    • Edited by dnyanbhar Friday, April 7, 2017 8:54 PM
    Friday, April 7, 2017 8:33 PM
  • Are you only interested in suspended of a specific type? Per host? Or do you want to list all suspended and show on a screen?
    Tuesday, April 11, 2017 9:20 AM