65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Indigo MSMQ 服务的“Hello world”程序

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.30/5 (7投票s)

2005年6月14日

2分钟阅读

viewsIcon

34988

downloadIcon

435

本文演示了一个基于 MSMQ 的简单 Indigo 服务器 - 客户端通信。

引言

本文演示了 Indigo 中一个基于 MSMQ 通道的简单通信服务。

背景

可以在这里找到 Indigo 的完整介绍。

Indigo 使用 MSMQ 作为其排队服务的基础。本文演示了为了使用 MSMQ 机制建立通信服务,我对 Indigo 统一通信代码所做的更改。

要求

为了使用该代码,需要满足以下先决条件

  • 必须在机器上安装 May Indigo CTP。
  • 适用于“Indigo”的 MSMQ 3.5 Beta 版(可在此处 [24 Kb]找到)。

该代码使用 VS 2005 Beta II 编译。

使用代码

创建队列

队列必须是事务性的,否则消息将会丢失。

代码

初始化服务

与所有类型的 Indigo 服务基础设施(COM+、Web 服务等)一样,需要以下步骤来初始化和启动服务

  1. 定义 URI

    声明 URI 以满足名为“HelloWorldQueue”的私有事务本地队列(其他 URI 实例可以指向 TCP 端口、asmx 文件等,具体取决于协议类型)。

  2. 定义绑定类型

    绑定类型对象包含通信的属性。例如,安全性、压缩、协议、寻址模式、最大重试次数、生存时间等。我使用“NetProfileMsmqBinding”,它是一个针对 MSMQ 传输的二进制优化跨机器绑定。

    其他绑定类型可能是:“NetProfileTcpBinding”、“WsProfileBinding”等。

  3. 声明服务

    声明服务阶段包含以下操作

    • 基于 MyHelloWorldService 类声明 ServiceHost
    • 将 URI 和绑定类型(端点)附加到服务。
    • 打开服务以开始监听传入的调用。
    Uri theQueueUri = 
          new Uri("net.msmq:///private$/HelloWorldQueue");
    
    NetProfileMsmqBinding theBinding = new NetProfileMsmqBinding();
    
    ServiceHost<MyHelloWorldService> theMsmqService = 
              new ServiceHost<MyHelloWorldService>();
    
    theMsmqService.AddEndpoint(typeof(MyHelloWorldService), 
                                  theBinding, theQueueUri);
    
    theMsmqService.Open();

声明服务契约

通过将 [ServiceContract] 属性应用于 IHelloWorldService 接口,我确定接口 IHelloWorldService 是一个 Indigo 服务契约,用于外部客户端。

HelloWorld 方法声明为 IsOneWay = true,表示异步模式调用。将“IsOneWay”标志设置为 true 并尝试返回值将导致异常,这是由于客户端和服务器之间的解耦导致的。

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract(IsOneWay = true)]
    void HelloWorld(string pMessage);
}

契约实现类

public class MyHelloWorldService : IHelloWorldService
{
    public void HelloWorld(string pMessage)
    { 
        Console.WriteLine("This is the hello world message: " + pMessage); 
    }
}

客户端初始化

同样,这里对所有 Indigo 服务类型都使用统一的方法。开发人员的职责仅在于更改绑定类型、URI 以满足所需的通信协议,并设置契约类。

private void SetUpMsmqClient()
{
   //Set the mq end point (hard coded for the demo )
   EndpointAddress theEndPoint = new EndpointAddress(new 
   Uri("net.msmq:///private$/HelloWorldQueue"));

   //Create the binding object  
   NetProfileMsmqBinding theBinding = new NetProfileMsmqBinding();

   //Constract the channel 
   mMqChannel = new ChannelFactory<IHelloWorldService>(theEndPoint, theBinding);

   //Create the channel and open it 
   mHelloWorldClient = mMqChannel.CreateChannel();

   mMqChannel.Error += new 
     EventHandler<CommunicationErrorEventArgs>(MqChannel_Error);
}

调用服务

我使用代理对象 mHelloWorldClient 来调用服务器 HelloWorld 方法。

mHelloWorldClient.HelloWorld ("Hello From me !!");

关注点

可以在此处找到使用 Indigo 服务中的 MSMQ 的声明性示例。

您可能会注意到 WSE 3.0 消息传递机制与 Indigo 服务的相似之处。我使用一个简单的字符串参数来声明 HelloWorld 方法。您可以使用 DataContract 属性来声明复杂类型。

例如

[DataContract]
public class HelloWorldData
{
        private string mMessage;
        private DateTime mDate;
    
    [DataMember]
    public string Message
    {
        get {return mMessage; }
        set { mMessage = value; }
    }
    [DataMember]
    public DateTime Date
    {
        get {return mDate; }
        set { mDate = value; }
    }
 
}

请注意,与 .NET Remoting 或 Web 服务相比,契约实现类不必派生自基类,例如 MarshalByRef 等。这允许在契约实现类的实现方面有更大的自由度。

使用 Indigo MSMQ 服务的“Hello world”程序 - CodeProject - 代码之家
© . All rights reserved.