我正在尝试学习 MSMQ,我从做两个简单的控制台应用程序开始。一个发射器,一个接收器。
首先,这是发射器代码:
static void Main(string[] args)
{
try
{
if (MessageQueue.Exists(path))
{
mqueue = new MessageQueue(path);
}
else
{
mqueue = MessageQueue.Create(path);
}
Timer timer = new Timer(timerCallback, null, 3000, 3000);
}
catch(Exception e)
{
System.Console.WriteLine(e.Message);
}
finally
{
System.Console.ReadLine();
}
}
static void timerCallback(object state)
{
Student student = new Student(randomString(), randomString(), randomString());
mqueue.Send(student);
System.Console.WriteLine("sent: {0}", student.ToString());
}
和接收器:
static void Main(string[] args)
{
try
{
if (MessageQueue.Exists(path))
{
mqueue = new MessageQueue(path);
}
else
{
mqueue = MessageQueue.Create(path);
}
Type [] supportedTypes = {typeof(Student)};
mqueue.Formatter = new XmlMessageFormatter(supportedTypes);
mqueue.BeginReceive(new TimeSpan(0,0,5000), mqueue, new AsyncCallback(messageReceived));
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
finally
{
System.Console.ReadLine();
}
}
该应用程序运行良好,但我想将通信渠道更改为TcpChannel/ HttpChannel
. 知道我该怎么做吗?