From 468b291cc2ff900f605072a30d6722c61513a446 Mon Sep 17 00:00:00 2001 From: Schwirg László Date: Thu, 24 Feb 2022 17:47:38 +0100 Subject: [PATCH] v1.11.2.0 - MSMQ Manager-ben apró javítások --- Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------- Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs | 4 ++-- 2 files changed, 73 insertions(+), 47 deletions(-) diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs index 5abc46a..893c724 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs @@ -15,7 +15,7 @@ using Vrh.XmlProcessing; using VRH.Common; using System.Xml.Linq; using System.Text.RegularExpressions; - +using System.ComponentModel; namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS { @@ -72,7 +72,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS MSMQ st = p.Parameters as MSMQ; try { - var fullpath = MSMQManagerCore.CreateFullQueuePath(st.Xml_AddressPrefix, st.Xml_Server, st.Xml_Name); + var fullpath = st.Xml_FullPath; MSMQManagerCore.CreateQueue(fullpath, st.Xml_Label); ColorConsole.WriteLine($"MSMQ:{st.Xml_Name} created.", ConsoleColor.Green); foreach (var accessright in st.Xml_AccessRights) @@ -106,7 +106,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS MSMQ st = p.Parameters as MSMQ; try { - var fullpath = MSMQManagerCore.CreateFullQueuePath(st.Xml_AddressPrefix, st.Xml_Server, st.Xml_Name); + var fullpath = st.Xml_FullPath; MSMQManagerCore.DeleteQueue(fullpath); ColorConsole.WriteLine($"MSMQ:{st.Xml_Name} removed.", ConsoleColor.Green); } @@ -138,7 +138,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS { foreach (var accessright in st.Xml_AccessRights) { - var fullpath = MSMQManagerCore.CreateFullQueuePath(st.Xml_AddressPrefix, st.Xml_Server, st.Xml_Name); + var fullpath = st.Xml_FullPath; MSMQManagerCore.SetPermissions(fullpath, accessright.un, accessright.ar, accessright.ac); string ctrlstring = accessright.AccessControlEntryTypeText(); ColorConsole.WriteLine($"Access right {accessright.ar} {ctrlstring} for user:{accessright.un}.", ConsoleColor.Green); @@ -171,12 +171,12 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS while (true) { - string username = ColorConsole.ReadLine("Enter username:", ConsoleColor.Yellow,defaultvalue:defaultusername); + string username = ColorConsole.ReadLine("Enter username:", ConsoleColor.Yellow, defaultvalue: defaultusername); if (username.ToUpper() == "EX") { break; } - string accessrightsstring = ColorConsole.ReadLine("Enter access right:", ConsoleColor.Yellow, validitylist: Enum.GetNames(typeof(MessageQueueAccessRights)).Select(e=>e.ToString()).ToList(), defaultvalue: defaultaccessrightsstring); + string accessrightsstring = ColorConsole.ReadLine("Enter access right:", ConsoleColor.Yellow, validitylist: Enum.GetNames(typeof(MessageQueueAccessRights)).Select(e => e.ToString()).ToList(), defaultvalue: defaultaccessrightsstring); if (accessrightsstring.ToUpper() == "EX") { break; } - var accessrights = (MessageQueueAccessRights)Enum.Parse(typeof(MessageQueueAccessRights),accessrightsstring); + var accessrights = (MessageQueueAccessRights)Enum.Parse(typeof(MessageQueueAccessRights), accessrightsstring); string controltypestring = ColorConsole.ReadLine("Enter access right:", ConsoleColor.Yellow, validitylist: Enum.GetNames(typeof(AccessControlEntryType)).Select(e => e.ToString()).ToList(), defaultvalue: defaultcontroltypestring); if (controltypestring.ToUpper() == "EX") { break; } @@ -187,7 +187,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS MSMQ st = p.Parameters as MSMQ; try { - var fullpath = MSMQManagerCore.CreateFullQueuePath(st.Xml_AddressPrefix, st.Xml_Server, st.Xml_Name); + var fullpath = st.Xml_FullPath; MSMQManagerCore.SetPermissions(fullpath, username, accessrights, controltype); ColorConsole.WriteLine($"Permissions set for MSMQ:{st.Xml_Name}. Username:{username}, access right: {accessrights}, control type: {controltype}", ConsoleColor.Green); } @@ -211,14 +211,14 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS string sn; while (true) { - sn = ColorConsole.ReadLine("Enter server ip/name to get queue info from (enter . for localhost)",defaultvalue:sndefault); + sn = ColorConsole.ReadLine("Enter server ip/name to get queue info from (enter . for localhost)", defaultvalue: sndefault); if (sn.ToLower() == "ex") { return o; } sndefault = sn; List queuearray = new List(); queuearray = System.Messaging.MessageQueue.GetPrivateQueuesByMachine(sn).ToList(); - var menumsmqs = DisplayMSMQMenu(queuearray.Select((q, ixx) => new MSMQ(q, "Q"+((ixx + 1).ToString()))).ToList(), $"Select the MSMQ(s) on server '{sn}'!", silent: false,midf: DisplayMSMQShortInfo) + var menumsmqs = DisplayMSMQMenu(queuearray.Select((q, ixx) => new MSMQ(q, "Q" + ((ixx + 1).ToString()))).ToList(), $"Select the MSMQ(s) on server '{sn}'!", silent: false, midf: DisplayMSMQShortInfo) .SetSelectionMode(Menu.SelectionMode.Multi); Menu.Selection sr = menumsmqs.Select(); @@ -235,12 +235,12 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS var sp = SendParameters.Get(msmq.Xml_Encoding, msmq.Xml_Formatter); if (sp != null) { - var fullpath = MSMQManagerCore.CreateFullQueuePath(msmq.Xml_AddressPrefix, msmq.Xml_Server, msmq.Xml_Name); + var fullpath = msmq.Xml_FullPath; MSMQManagerCore.Send(fullpath, sp.messagebody, sp.messagelabel, sp.messageformatter, sp.encoding); ColorConsole.WriteLine($"Sending to '{fullpath}' ....", ConsoleColor.Yellow); Message msg = MSMQManagerCore.PeekLast(fullpath, sp.messageformatter, sp.encoding, out string body, out string label); TestSendCheckMessage(fullpath, msg, sp, body, label, "message sending/peek back"); - var ans = ColorConsole.ReadLine($"Do You want to remove this test message (lookup id='{msg.LookupId}') from quue?", ConsoleColor.Yellow,validitylist:new List { "yes","no"},defaultvalue:"yes"); + var ans = ColorConsole.ReadLine($"Do You want to remove this test message (lookup id='{msg.LookupId}') from quue?", ConsoleColor.Yellow, validitylist: new List { "yes", "no" }, defaultvalue: "yes"); if (ans.ToLower() == "yes") { msg = MSMQManagerCore.ReadById(fullpath, msg.LookupId, sp.messageformatter, sp.encoding, out body, out label); @@ -253,7 +253,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS return o; } - private static void TestSendCheckMessage(string fullpath,Message msg, SendParameters sp, string body, string label,string actiontext) + private static void TestSendCheckMessage(string fullpath, Message msg, SendParameters sp, string body, string label, string actiontext) { if (sp.messagebody == body && sp.messagelabel == label) { ColorConsole.WriteLine($"Success! Action:{actiontext}. Queue:'{fullpath}'.", ConsoleColor.Green); } else @@ -310,7 +310,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS { var messageformatter = MSMQManagerCore.SetFormatter(st.Xml_Formatter); //new ActiveXMessageFormatter(); var encoding = MSMQManagerCore.SetEncoding(st.Xml_Encoding); // Encoding.UTF8; - var fullpath = MSMQManagerCore.CreateFullQueuePath(st.Xml_AddressPrefix, st.Xml_Server, st.Xml_Name); + var fullpath = st.Xml_FullPath; MSMQManagerCore.Peek(fullpath, messageformatter, encoding, out string msgbody, out string msglabel); ColorConsole.WriteLine($"Message peeked from MSMQ:{st.Xml_Name}", ConsoleColor.Green); ColorConsole.WriteLine($" Body: {msgbody}", ConsoleColor.Yellow); @@ -346,7 +346,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS { var messageformatter = MSMQManagerCore.SetFormatter(st.Xml_Formatter); //new ActiveXMessageFormatter(); var encoding = MSMQManagerCore.SetEncoding(st.Xml_Encoding); // Encoding.UTF8; - var fullpath = MSMQManagerCore.CreateFullQueuePath(st.Xml_AddressPrefix, st.Xml_Server, st.Xml_Name); + var fullpath = st.Xml_FullPath; MSMQManagerCore.Read(fullpath, messageformatter, encoding, out string msgbody, out string msglabel); ColorConsole.WriteLine($"Message read from MSMQ:{st.Xml_Name}", ConsoleColor.Green); ColorConsole.WriteLine($" Body: {msgbody}", ConsoleColor.Yellow); @@ -368,7 +368,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS var menufolders = DisplayMSMQMenuFromXml(config, $"Select the MSMQ(s) to manage with function '{nameof(Send)}'!", silent: true) .SetSelectionMode(Menu.SelectionMode.Single); - + Menu.Selection sr = menufolders.Select(selectedtaskindexes); if (sr.Result == Menu.SelectionResult.Exit) { return o; } else if (sr.Result == Menu.SelectionResult.None) { return o; } @@ -385,8 +385,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS var sp = SendParameters.Get(st.Xml_Encoding, st.Xml_Formatter); if (sp == null) { break; } - var fullpath = MSMQManagerCore.CreateFullQueuePath(st.Xml_AddressPrefix, st.Xml_Server, st.Xml_Name); - MSMQManagerCore.Send(fullpath,sp.messagebody, sp.messagelabel, sp.messageformatter, sp.encoding); + var fullpath = st.Xml_FullPath; + MSMQManagerCore.Send(fullpath, sp.messagebody, sp.messagelabel, sp.messageformatter, sp.encoding); ColorConsole.WriteLine($"Message sent to MSMQ:{st.Xml_Name}.", ConsoleColor.Green); } catch (ApplicationException ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); } @@ -424,9 +424,9 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS public IMessageFormatter messageformatter; public Encoding encoding; public SendParameters() { } - public static SendParameters Get(string defaultencoding, string defaultformatter) + public static SendParameters Get(string defaultencoding, string defaultformatter) { - ColorConsole.WriteLine("Enter test message data!",ConsoleColor.Yellow); + ColorConsole.WriteLine("Enter test message data!", ConsoleColor.Yellow); var sp = new SendParameters() { }; var now = DateTime.Now.ToString(); sp.messagebody = ColorConsole.ReadLine("Enter message body:", ConsoleColor.Yellow, defaultvalue: "TEST MESSAGE " + now); @@ -468,7 +468,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS MSMQ st = p.Parameters as MSMQ; try { - var fullpath = MSMQManagerCore.CreateFullQueuePath(st.Xml_AddressPrefix, st.Xml_Server, st.Xml_Name); + var fullpath = st.Xml_FullPath; MSMQManagerCore.Purge(fullpath); ColorConsole.WriteLine($"MSMQ messages purged. Name:{st.Xml_Name}", ConsoleColor.Green); } @@ -485,9 +485,9 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS { if (config == null) { config = new MSMQManagerXmlProcessor(XMLCONNECTIONSTRING, "", "hu-HU"); } List msmqdefList = config.GetDefinitionList(); - return DisplayMSMQMenu(msmqdefList, prompt,silent,DisplayMSMQInfo); + return DisplayMSMQMenu(msmqdefList, prompt, silent, DisplayMSMQInfo); } - private static Menu DisplayMSMQMenu(List msmqlist = null, string prompt = null, bool silent = false,Menu.MenuItemDisplayerFunc midf=null) + private static Menu DisplayMSMQMenu(List msmqlist = null, string prompt = null, bool silent = false, Menu.MenuItemDisplayerFunc midf = null) { List msmqdefList = msmqlist; var menufct = new Menu("MSMQ list", prompt) @@ -559,18 +559,18 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS successpattern = p; bool isip = IPAddress.TryParse(servername, out IPAddress ip); if (servername == "" || servername == "." || servername.ToLower() == "localhost") { servername = System.Environment.MachineName; } - if (formatname == "") - { + if (formatname == "") + { if (isip) { formatname = @"FormatName:DIRECT=TCP"; addresstype = @"TCP"; } else { formatname = @"FormatName:DIRECT=OS"; addresstype = @"OS"; } } - if ((isip && addresstype=="OS") || (!isip && addresstype == "TCP")) { return false; } + if ((isip && addresstype == "OS") || (!isip && addresstype == "TCP")) { return false; } return true; } } return false; } - public static bool CheckIfQueueExists(string servername,string queuefullname) + public static bool CheckIfQueueExists(string servername, string queuefullname) { if (string.IsNullOrWhiteSpace(servername) || servername == "." || servername.ToLower() == "localhost") { servername = System.Environment.MachineName; } if (servername == "*") @@ -586,15 +586,15 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS //var queuenamelist = queuearray.Select(q => q.Path == queuefullname || q.FormatName== queuefullname || q.QueueName== queuefullname).ToList(); //return queuenamelist.Any(); } - public static void CreateQueue(string msmqFullname, string label=null) + public static void CreateQueue(string msmqFullname, string label = null) { - if (!MessageQueue.Exists(msmqFullname)) { MessageQueue.Create(msmqFullname); } + if (!MSMQ.IsExists(msmqFullname, out string returnmessage)) { MessageQueue.Create(msmqFullname); } using (var msmq = new MessageQueue(msmqFullname)) { if (!string.IsNullOrWhiteSpace(label)) { msmq.Label = label; } } } - public static void SetPermissions(string msmqFullname,string username, MessageQueueAccessRights accessrights, AccessControlEntryType controltype) + public static void SetPermissions(string msmqFullname, string username, MessageQueueAccessRights accessrights, AccessControlEntryType controltype) { using (var msmq = new MessageQueue(msmqFullname)) { @@ -621,7 +621,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS public static void DeleteQueue(string msmqFullname) { - if (MessageQueue.Exists(msmqFullname)) { MessageQueue.Delete(msmqFullname); } + if (MSMQ.IsExists(msmqFullname,out string returnmessage)) { MessageQueue.Delete(msmqFullname); } } public static void Purge(string msmqFullname) { @@ -747,8 +747,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS label = ""; using (var msmq = new MessageQueue(msmqFullname)) { - if (Count(msmq) == 0) {return null; } - + if (Count(msmq) == 0) { return null; } + msmq.Formatter = messageformatter; var m = msmq.ReceiveByLookupId(msgid); label = m.Label; @@ -886,7 +886,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS public List Xml_AccessRights = new List(); public MSMQDefaults(XElement rootxml) { - var generalxml = GetXElement(rootxml,nameof(MSMQ.XmlStructure.MSMQManager.General)); + var generalxml = GetXElement(rootxml, nameof(MSMQ.XmlStructure.MSMQManager.General)); var messagefiles = GetXElement(generalxml, nameof(MSMQ.XmlStructure.MSMQManager.General.MessageFiles)); Xml_MessageFilesDirectory = GetValue(nameof(MSMQ.XmlStructure.MSMQManager.General.MessageFiles.Attributes.Directory), messagefiles, MSMQ.XmlStructure.MSMQManager.General.MessageFiles.Attributes.Directory.Values.DEFAULT); @@ -945,6 +945,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS public EncryptionRequired EncryptionRequired; #endregion fields + public string Xml_FullPath {get{return MSMQManagerCore.CreateFullQueuePath(this.Xml_AddressPrefix, this.Xml_Server, this.Xml_Name);} } + #region basic constructor public MSMQ() { } #endregion basic constructor @@ -1007,22 +1009,38 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS } #endregion Xml cloner constructor - #region CollectMSMQInfo - public MSMQ CollectMSMQInfo() + #region IsExists + public static bool IsExists(string queueName, out string returnmessage) + { + returnmessage = ""; + //return MessageQueue.Exists(queueName); // nem működik remote messagequeue esetében + var queue = new MessageQueue(queueName); + try + { + queue.Peek(new TimeSpan(0, 0, 5)); + return true; + } + catch (MessageQueueException ex) + { + returnmessage= ex.ErrorCode + ":" + ex.Message + (ex.InnerException != null ? ("\n" + ex.InnerException.Message) : ""); + return ex.Message.StartsWith("Timeout"); + } + } + #endregion IsExists + + #region CollectMSMQInfo + public MSMQ CollectMSMQInfo() { MSMQ msmqdef = this; - string msmqaddressprefix = msmqdef.Xml_AddressPrefix; - string msmqservername = msmqdef.Xml_Server; - string msmqFullname = MSMQManagerCore.CreateFullQueuePath(msmqaddressprefix, msmqservername, msmqdef.Xml_Name); msmqdef.QueueName = msmqdef.Xml_Name; - msmqdef.MachineName = msmqservername; - msmqdef.Path = msmqFullname; + msmqdef.MachineName = msmqdef.Xml_Server; + msmqdef.Path = msmqdef.Xml_FullPath; try { - msmqdef.Status = MessageQueue.Exists(msmqFullname) ? "Installed" : "Uninstalled"; + msmqdef.Status = MSMQ.IsExists(msmqdef.Path,out string returnmessage) ? "Installed" : "Uninstalled/Error"; if (msmqdef.Status == "Installed") { - using (var msmq = new MessageQueue(msmqFullname)) + using (var msmq = new MessageQueue(msmqdef.Path)) { msmqdef.QueueName = msmq.QueueName; msmqdef.MachineName = msmq.MachineName; @@ -1049,8 +1067,16 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS msmqdef.Status = "Ready"; } } + else { msmqdef.StatusDescription = returnmessage; } + } + catch (MessageQueueException ex) + { + msmqdef.Status = "Error"; msmqdef.StatusDescription = ex.ErrorCode +":"+ ex.Message + (ex.InnerException != null ? ("\n" + ex.InnerException.Message) : ""); + } + catch (Exception ex) + { + msmqdef.Status = "Error"; msmqdef.StatusDescription = ex.Message + (ex.InnerException != null ? ("\n" + ex.InnerException.Message) : ""); } - catch (Exception ex) { msmqdef.Status = "Error"; msmqdef.StatusDescription = ex.Message + (ex.InnerException != null ? ("\n" + ex.InnerException.Message) : ""); } return msmqdef; } #endregion CollectMSMQInfo @@ -1075,7 +1101,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS { var privatepublictext = msmq.Private ? "private" : "public"; ColorConsole.Write($"{msmq.Xml_AddressPrefix}", ConsoleColor.Black, ConsoleColor.White, prefix: "Format:", suffix: $" Queue is { privatepublictext }."); - ColorConsole.Write($"{msmq.Xml_Server}", ConsoleColor.Black, ConsoleColor.White, prefix: "Server:", suffix: $" Queue is { privatepublictext }."); + ColorConsole.Write($"{msmq.Xml_Server}", ConsoleColor.Black, ConsoleColor.White, prefix: "Server:"); ColorConsole.WriteLine(); return " "; } diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index cd37eeb..5897bad 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.11.1.0")] -[assembly: AssemblyFileVersion("1.11.1.0")] +[assembly: AssemblyVersion("1.11.2.0")] +[assembly: AssemblyFileVersion("1.11.2.0")] -- libgit2 0.21.2