当前位置: 首页>>代码示例>>C#>>正文


C# Message.Body属性代码示例

本文整理汇总了C#中System.Messaging.Message.Body属性的典型用法代码示例。如果您正苦于以下问题:C# Message.Body属性的具体用法?C# Message.Body怎么用?C# Message.Body使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Messaging.Message的用法示例。


在下文中一共展示了Message.Body属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Messaging;

namespace MyProject
{

    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //		 
        // This example sends and receives a message from
        // a queue.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Send messages to a queue.
            myNewQueue.SendMessage(MessagePriority.Normal, "First Message Body.");
            myNewQueue.SendMessage(MessagePriority.Highest, "Second Message Body.");

            // Receive messages from a queue.
            myNewQueue.ReceiveMessage(); 
            myNewQueue.ReceiveMessage();

            return;
        }

        //**************************************************
        // Sends a string message to a queue.
        //**************************************************
        
        public void SendMessage(MessagePriority priority, string messageBody)
        {

            // Connect to a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            // Create a new message.
            Message myMessage = new Message();

            if(priority > MessagePriority.Normal)
            {
                myMessage.Body = "High Priority: " + messageBody;
            }
            else
            {
                myMessage.Body = messageBody;
            }

            // Set the priority of the message.
            myMessage.Priority = priority;

            // Send the Order to the queue.
            myQueue.Send(myMessage);

            return;
        }

        //**************************************************
        // Receives a message.
        //**************************************************
        
        public  void ReceiveMessage()
        {
            // Connect to the a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            // Set the queue to read the priority. By default, it
            // is not read.
            myQueue.MessageReadPropertyFilter.Priority = true;

            // Set the formatter to indicate body contains a string.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(string)});
            
            try
            {
                // Receive and format the message. 
                Message myMessage =	myQueue.Receive(); 

                // Display message information.
                Console.WriteLine("Priority: " + 
                    myMessage.Priority.ToString());
                Console.WriteLine("Body: " + 
                    myMessage.Body.ToString());
            }
            
            catch (MessageQueueException)
            {
                // Handle Message Queuing exceptions.
            }

            // Handle invalid serialization format.
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.Message);
            }
            
            // Catch other exceptions as necessary.

            return;
        }
    }
}
开发者ID:.NET开发者,项目名称:System.Messaging,代码行数:113,代码来源:Message.Body

示例2: Main

//引入命名空间
using System;
using System.Messaging;

class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    MessageQueue txq2 = new MessageQueue( @".\Private$\txq2" );

    while ( true )
    {
      Message msgIn = txq2.Receive( MessageQueueTransactionType.Single );
      msgIn.Formatter = new XmlMessageFormatter( new String[] { "System.String, mscorlib", } );
      System.Console.WriteLine( "Received: {0}", (string)msgIn.Body );
    }
  }
}
开发者ID:C#程序员,项目名称:System.Messaging,代码行数:19,代码来源:Message.Body


注:本文中的System.Messaging.Message.Body属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。