Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

818

Part IX

BEYOND THE LABS

 

 

 

 

 

9. Type the following code:

 

 

using System;

 

 

using Math;

 

 

namespace MathClient

 

 

{

///

<summary>

 

 

Y

{

 

 

 

/// Summary description for Class1.

/// </summary>

 

L

class Class1

 

F

 

M

 

 

/// <summary>

 

 

 

A

 

 

 

 

/// The main entry point for the application.

 

E

 

/// </summary>

 

 

 

[STAThread]

 

 

 

 

T

 

 

static void Main(string[] args)

 

{

MathComp obj=new MathComp(); long lRes=obj.Add(10,20); obj.Extra=false; Console.Write(lRes);

return;

}

}

}

Compile the project and see the output. You can expand on this project further to create a better math application that performs more functions.These steps explain to you the mechanism to create a DLL and access it.

Messaging

The System.Messaging namespace provides classes that allow you to connect to, monitor, and administer message queues on the network and send or receive messages. Before I explain the System.Messaging namespace, it is critical to understand message queuing and certain terms related to messaging.

Team-Fly®

ADVANCED C# CONCEPTS

Chapter 34

819

 

 

 

 

Benefits of Message Queues

Messaging also provides a powerful and flexible mechanism for interprocess communication between components of a server-based application. The advantages provided by messaging are:

Robustness. Messages are less affected by component failures than direct calls between components, as messages are stored in queues until processed.

Message prioritization. Important messages are received before less important ones.Therefore, you can guarantee adequate response time for critical applications.

Offline capabilities. Messages can be sent to temporary queues and remain there until they are delivered successfully. Moreover, users can continue to perform operations when access to the queue is unavailable. Additional operations can proceed as if the message has already been processed, because the message delivery is guaranteed when the network connection is restored.

Transactional messaging. Several messages can be coupled into a single transaction, ensuring that the messages are delivered in sequence and are successfully retrieved from their destination queue. If any errors occur, the entire transaction is cancelled.

Security. The message queuing technology on which the MessageQueue component is based uses Windows security to provide secure access control, provide auditing, and encrypt and authenticate the messages your component sends and receives.

Limitations

In order to develop MessageQueue components your system must meet the following requirements:

To see queue information in Server Explorer or to access queues programmatically, you must install message queuing on your client computer.

Message queuing can be run in either a domain or a workgroup environment. In the context of message queuing, a domain environment

820

Part IX

BEYOND THE LABS

 

 

includes domain controllers that provide a directory service such as active

 

 

 

 

directory, and a workgroup environment is any environment that does not

 

 

provide such a directory service.

Key Messaging Terms

Before we proceed further, I would like to explain certain key terms.

A message is a unit of data sent from one computer to another. A message can be very simple, consisting of just a string of text, or more complex, possibly involving embedded objects or pictures.

Messages are transmitted to queues. A message queue is a temporary storage area that holds messages while they are in transit. The message queue manager acts as the intermediary in transmitting a message from its source to its destination.The main purpose of a queue is to provide routing and guarantee the delivery of the message. In case the recipient is not available when a message is sent, the queue holds the message until it can be successfully delivered.

Message queuing, Microsoft’s messaging technology, provides messaging and message queue facilities for any applications that have Microsoft Windows installed regardless of whether they are on the same network or whether they are online at the same time.

A message queuing network is a set of computers that are enabled to send messages back and forth to one another. Different computers in the network play different roles to ensure that messaging proceeds smoothly. Some computers provide information to determine how messages are sent, some hold information about the entire network, while others simply send and receive messages.

During message queuing setup, an administrator makes decisions about which servers can communicate with each other and sets up special roles for specific servers. The computers that make up this message queuing network are called sites, and they are connected to one another by site links. Each site link has an associated cost, determined by the administrator, that indicates how quickly messages can be passed across it.

The message queuing administrator also sets up one or more computers in the network that act as routing servers. A routing server makes decisions about how a message is delivered by looking at the cost of various site links and determining the quickest and most efficient way to deliver the message across multiple sites.

ADVANCED C# CONCEPTS

Chapter 34

821

 

 

 

 

Types of Message Queues

There are two main categories of queues, queues created by users and system queues.

User-Generated Queues

Queues created by users can be any of the following:

Public queues are those that are replicated throughout the message network and can potentially be accessed by all of the computers connected by the network.

Private queues are queues that are not published across the entire network. They are available only on the local computer that contains them. They can be accessed only by applications that know the full path name of the queue.

Administration queues are queues that contain messages acknowledging the delivery of messages sent within a given message queuing network. You specify the administration queue you want your MessageQueue components to use.

Response queues contain response messages that are returned to the sender application when the message is received by the destination application. You specify the response queue you want your MessageQueue components to use.

System Queues

System queues generally fall in one of the following categories:

Journal queues optionally store copies of messages that you send and copies of messages removed from a queue. A single journal queue on each message queuing client stores copies of messages sent from that computer. On the server, a separate journal queue is created for each individual queue. This journal tracks messages removed from that queue.

Dead-letter queues store copies of undeliverable or expired messages. If the message that expired or was undeliverable was a transactional message, it is stored in a special kind of dead-letter queue called a transaction dead-letter queue. Dead letters are stored on the computer on which the message expired.

Report queues contain messages that indicate the route a message took to its destination and can contain test messages. There can be only one report queue per computer.

822

Part IX

BEYOND THE LABS

 

 

 

Private system queues are a series of private queues that store administrative and notification messages that the system needs to process messaging actions.

Most of the work you do in your applications will involve accessing public queues and their messages. However, you will most likely use several different kinds of the system queues in your day-to-day operations, depending on your application’s need for journal recording, acknowledgement, and other special processing.

Synchronous and Asynchronous Communication

Messages are sent to and received from a queue as separate processes. Therefore, queue communication is inherently asynchronous. You can also receive messages asynchronously by invoking the BeginReceive method and then move on to perform other tasks without waiting for a reply. However, synchronous communication is different from this.

In synchronous communication, the sender of a request waits for a response from the receiver before performing other tasks. The amount of time that the sender must wait depends on the amount of time it takes for the receiver to process the request and send a response.

System.Messaging Namespace

As you know, message queuing is a technology that allows applications running at different times to communicate across heterogeneous networks and systems. Applications send, receive, or read messages from queues.The MessageQueue class is a wrapper around message queuing. There are different versions for different operating environments. System.Messaging namespace provides classes that allow you to connect to, monitor, and administer message queues on the network and send, receive, or read messages.

Message queuing enables developers to build applications that communicate with other programs quickly in a simple and reliable manner. Messaging ensures guaranteed messages delivery and a fail-proof way to carry out your business processes. For example, suppose you have a retail point-of-sale application that must run 24 hours a day, seven days a week. If the database system behind the application goes down, your sales staff might need to start taking orders manually. Using message queuing, you can set up the system so that the orders that cannot be processed during the downtime are automatically put into a queue and processed as soon as the database comes back up.

ADVANCED C# CONCEPTS

Chapter 34

 

823

 

 

 

 

 

 

You can use an instance of the MessageQueue component to establish connection with existing message queues, examine their contents, and send and receive messages. You can also use Server Explorer of Visual Studio .NET to view message queues on any server to which you have access. You can also add a queue from Server Explorer to your component ’s designer to automatically create a component that is configured to interact with the queue.

MessageQueue Class

MessageQueue class provides members for reading and writing message to the queue.The Send method enables your application to write messages to the queue. This method is overloaded and enables you to specify whether to send your message using a message or any other managed object, including application-specific classes.

The Receive, ReceiveById, and ReceiveByCorrelationId methods provide func-

tionality for reading messages from a queue.These methods support transactional queue processing. These methods also provide overloads with timeout parameters that enable processing to continue if the queue is empty. Because these methods are examples of synchronous processing, they interrupt the current thread until a message is available, unless you specify a timeout.

The Peek method is similar to the Receive method, but it does not cause a message to be removed from the queue when it is read. Because the Peek method does not change the queue contents, there are no overloads to support transactional processing.

The BeginPeek, EndPeek, BeginReceive, and EndReceive methods provide ways to

asynchronously read messages from the queue. They do not interrupt the current thread while waiting for a message to arrive in the queue.

Other methods of the MessageQueue class provide functionality for retrieving lists of queues by specified criteria and determining if specific queues exist.

In addition, MessageQueue class provides methods for creating and deleting message queues, for setting ACL-based access rights, and for working with the connection cache.

The Message class provides detailed control over the information you send to a queue and is the object used when receiving or peeking messages from a queue.

824

Part IX

BEYOND THE LABS

 

 

 

Besides the message body, the properties of the Message class include acknowledgment settings, format selection, identification, authentication and encryption information, timestamps, and transaction data.

Table 34-1 lists the classes in the System.Messaging namespace hierarchy.

Table 34-1 System.Messaging Namespace Hierarchy

Class

Description

AccessControlEntry

Specifies access rights for a user or a computer

 

to perform application-specific implementa-

 

tions of common tasks.

AccessControlList

Contains a list of access control entries, speci-

 

fying access rights for one or more trustees.

ActiveXMessageFormatter

Serializes or deserializes primitive data types

 

and other objects to or from the body of a

 

message.

BinaryMessageFormatter

Serializes or deserializes an object, or a group

 

of connected objects, to or from the body of a

 

message, using a binary format.

DefaultPropertiesToSend

Specifies the default proper ty values that wil l

 

be used when sending objects other than

 

Message instances to a message queue.

Message

Provides access to the properties needed to

 

define a message queuing message.

MessageEnumerator

Provides a forward-only cursor to enumerate

 

through messages in a message queue.

MessagePropertyFilter

Controls and selects the properties that are

 

retrieved when peeking or receiving messages

 

from a message queue.

MessageQueue

Provides access to a queue on a message queu-

 

ing server.

MessageQueueAccessControlEntry

Specifies access rights for a user or a computer

 

to perform message queuing tasks.

MessageQueueCriteria

Filters message queues when performing a

 

query.

ADVANCED C# CONCEPTS

Chapter 34

825

Table 34-1 System.Messaging Namespace Hierarchy (continued)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Class

Description

 

 

 

 

 

 

 

 

 

MessageQueueEnumerator

Provides a forward-only cursor to enumerate

 

 

through messages in a message queue.

 

MessageQueueException

The exception that is thrown if a Microsof t

 

 

message queuing internal error occurs.

 

MessageQueueInstaller

Allows you to install and configure a queue

 

 

that your application needs in order to run.

 

MessageQueuePermission

Allows control of code access permissions for

 

 

messaging.

 

 

 

 

MessageQueuePermissionAttribute

Allows declarative MessageQueue permission

 

 

checks.

 

 

 

 

MessageQueuePermissionEntry

Defines the smallest unit of a code access secu-

 

 

rity permission set for messaging .

 

 

 

 

MessageQueuePermissionEntryCollection

Contains a strongly typed collection of

 

 

MessageQueuePermissionEntry objects.

 

MessageQueueTransaction

Provides a message queuing internal

 

 

transaction.

 

 

 

 

MessagingDescriptionAttribute

Specifies a description for a property or event.

 

PeekCompletedEventArgs

Provides data for the PeekCompleted event.

 

 

When your asynchronous peek operation calls

 

 

an event handler, an instance of this class is

 

 

passed to the handler.

 

 

 

 

ReceiveCompletedEventArgs

Provides data for ReceiveCompleted event.

 

Trustee

Specifies a user account, group account, or

 

 

logon session to which an access control entry

 

 

applies.

 

 

 

 

XmlMessageFormatter

Serializes and deserializes objects to or from

 

 

the body of a message , using the XML format.