Skip to content

Instantly share code, notes, and snippets.

@rod-dot-codes
Created August 21, 2014 11:59
Show Gist options
  • Save rod-dot-codes/36afc8325b2b8e6461a4 to your computer and use it in GitHub Desktop.
Save rod-dot-codes/36afc8325b2b8e6461a4 to your computer and use it in GitHub Desktop.
MSMQ Messaging Console Application that sends Payload to the server specified. Was intended to be used with Fundamental Fund Management System South Africa's MSMQ implementation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using NDesk.Options;
namespace Send_MSMQ
{
class Program
{
public static string ORG = "";
public static string ORIG = "";
static void SendMessage(string sXML, string sQueue, bool bEntire)
{
var PayloadMe = @"<FPMMessage Version=""1.0"">
<Header>
<Source>
<Organisation>"+ ORG + @"</Organisation>
<Originator>"+ ORIG +@"</Originator>
</Source>
<MessageID>" + Guid.NewGuid().ToString() + @"</MessageID>
<Timestamp>" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz") + @"</Timestamp>
</Header>
<Payload>
" + sXML + @"
</Payload>
</FPMMessage>";
Console.WriteLine("Sending message to: ");
Console.WriteLine("FormatName:Direct=OS:" + sQueue);
MessageQueue msgQ = new MessageQueue("FormatName:Direct=OS:" + sQueue);
if (bEntire == false)
{
Message msg = new Message(PayloadMe);
msgQ.Send(msg);
}
else
{
Message msg = new Message(sXML);
msgQ.Send(msg);
}
msgQ.Close();
}
static void Main(string[] args)
{
//This is a command line application. It takes two arguments.
//The first is the Queue on which the message must be placed.
//The second is the XML Message that must be placed on the Queue.
//The application simply places the message on the Queue.
bool show_help = false;
string eXML = "";
string sXML = "";
string sQueue = "";
List<string> extra;
var p = new OptionSet() {
{ "x|xml=", "The XML that must be placed on the queue.",
v => sXML = v },
{ "e|entire_xml=", "The XML that must be placed on the queue.",
v => eXML = v },
{ "q|queue=",
"The queue onto which the XML must be placed.",
v => sQueue = v },
{ "h|help", "This application simply takes the options provided, --queue= and --xml=, and places the xml message on the queue",
v => show_help = v != null },
};
try
{
extra = p.Parse(args);
Console.WriteLine("Queue:");
Console.WriteLine(sQueue);
Console.WriteLine("XML:");
Console.WriteLine(sXML);
Console.WriteLine("Entire XML:");
Console.WriteLine(eXML);
if (eXML.Length > 0)
{
SendMessage(eXML, sQueue, true);
}
else
{
SendMessage(sXML, sQueue, false);
}
}
catch (Exception e)
{
Console.WriteLine("Error");
Console.WriteLine("Please use --help or -h for more information.");
Console.WriteLine(e.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment