Indigo Untyped Channel
有两种通道来使用最新的网络通讯组件Indigo:typeed 和untyped,typed channel是通过Server端的WSDL,即通过自动自动生成的interface,很明显,我们要遵守一个契约。另一个方面,我们可以使用untyped channel,在这个通道里,我们只是从一端到另一端传递最基本的Message(Indigo中的最基本单元),没有协议需要遵守,通常基于一个action URI.
看一个sample:
using System;
using System.MessageBus;
The client:
class Client
{
[STAThread]
static void
{
Uri client = new Uri(String.Format("soap.tcp://{0}:6000/Client", System.Environment.MachineName));
Uri server = new Uri(String.Format("soap.tcp://{0}:6001/Server", System.Environment.MachineName));
Console.WriteLine("Client - {0}", client.ToString());
Uri action = new Uri("http://tempuri.org/action1");
Message message = new Message(action, "Hello");
message.Headers.Add(new PathHeader(server));
message.Headers.Add(new ToHeader(server));
message.Headers.Add(new ReplyInfoHeader(client));
Port port = new Port(client);
port.Open();
port.SendChannel.Send(message);
port.Close();
}
}
The Server:
class Server
{
[STAThread]
static void
{
Uri server = new Uri(String.Format("soap.tcp://{0}:6001/Server", System.Environment.MachineName));
Port port = new Port(server);
ServerMessageHandler handler = new ServerMessageHandler();
port.ReceiveChannel.Handler = handler;
port.CloseTimeout = new TimeSpan(1, 0, 0, 0);
port.Open();
Console.WriteLine("Server - {0}", server.ToString());
Console.ReadLine();
}
}
public class ServerMessageHandler : SyncMessageHandler
{
public override bool ProcessMessage(Message message)
{
string content = message.Content.GetObject(typeof(string)) as string;
Console.WriteLine(content);
Console.ReadLine();
return true;
}
}
看上去和remoting有点类似,其实编程方式更加简洁,更好理解。