一
要實現進程間的通信
消息的主體包括ID
public interface IMessage:ICloneable
{
IMessageItemSequence GetMessageBody();
string GetMessageID();
string GetMessageName();
void SetMessageBody(IMessageItemSequence aMessageBody);
void SetMessageID(string aID);
void SetMessageName(string aName);
}
消息主體類Message實現了IMessage接口
public interface IMessageItemSequence:ICloneable
{
IMessageItem GetItem(string aName);
void SetItem(string aName
string GetValue(string aName);
void SetValue(string aName
}
Value為string類型
IMessageItem支持了消息體的嵌套
IMessage——Name
——ID
——Body(IMessageItemSequence)
——Value
——Item(IMessageItem)
——SubValue
——SubItem(IMessageItem)
——……
各個消息對象之間的關系如下
在實現服務進程通信之前
IMessageFactory factory = new MessageFactory();
IMessageItemSequence body = factory
body
body
IMessageItem item = factory
item
item
IMessageItem subItem
subItem
subItem
IMessageItem subItem
subItem
subItem
item
item
body
//Send Request Message
MyServiceClient service = new MyServiceClient(
IMessageItemSequence reply = service
在接收消息端就可以通過獲得body的消息體內容
二
在
public interface IService
{
IMessage Execute(IMessage aMessage);
}
Execute()方法接受一條Request Message
然而
業務服務均具有發出Request請求的能力
public interface IListenService
{
IMessage OnRequest(IMessage aMessage);
}
Request服務實現了IService接口
public class RequestListener:MarshalByRefObject
{
public RequestListener(IListenService listenService)
{
m_ListenService = listenService;
}
private IListenService m_ListenService;
#region IService Members
public IMessage Execute(IMessage aMessage)
{
return this
}
#endregion
public override object InitializeLifetimeService()
{
return null;
}
}
在RequestListener服務中
由於Request消息均由Request服務即RequestListener處理
public abstract class Service:IListenService
{
public Service(string serviceName)
{
m_ServiceName = serviceName;
m_RequestListener = new RequestListener(this);
}
#region IListenService Members
public IMessage OnRequest(IMessage aMessage)
{
//……
}
#endregion
private string m_ServiceName;
private RequestListener m_RequestListener;
}
Service類是一個抽象類
我們還需要在Service類中定義發送Request消息的行為
public IMessageItemSequence SendRequest(string aServiceName
{
IMessage message = m_Factory
message
message
message
IService service = FindService(aServiceName);
IMessageItemSequence replyBody = m_Factory
if (service != null)
{
IMessage replyMessage = service
replyBody = replyMessage
}
else
{
replyBody
}
return replyBody;
}
注意SendRequest()方法的定義
public void Initialize()
{
RemotingServices
}
我們Marshal的對象
m_RequestListener = new RequestListener(this);
注意
通過調用Initialize()方法
IService service = FindService(aServiceName);
下面我們來看看FindService()方法的實現
protected IService FindService(string aServiceName)
{
lock (this
{
IService service = (IService)m_Services[aServiceName];
if (service != null)
{
return service;
}
else
{
IService tmpService = GetService(aServiceName);
AddService(aServiceName
return tmpService;
}
}
}
可以看到
private IService GetService(string aServiceName)
{
IService service = (IService)Activator
return service;
}
在這裡
再分析SendRequest方法
我們可以定義一個具體的業務服務類
public class MyService:Service
{
public MyService(string aServiceName):base(aServiceName)
{}
}
假設把進程分為服務端和客戶端
在這些步驟中
三
前面實現的服務架構
雖然
public delegate void RequestHandler(string aMessageName
在RequestHandler委托中
這些委托均在服務初始化時被添加到服務類的SortedList對象中
protected abstract void AddRequestHandlers();
protected void AddRequestHandler(string aMessageName
{
lock (this
{
if (!this
{
this
}
}
}
protected RequestHandler FindRequestHandler(string aMessageName)
{
lock (this
{
RequestHandler handler = (RequestHandler)m_EventHandlers[aMessageName];
return handler;
}
}
AddRequestHandler()用於添加委托對象與aMessageName的鍵值對
public class MyService:Service
{
public MyService(string aServiceName):base(aServiceName)
{}
protected override void AddRequestHandlers()
{
this
this
}
private void Test
{
Console
Console
aReplyMessageBody
}
private void Test
{
Console
}
}
Test
需要注意的是
通過這樣的設計
public IMessage OnRequest(IMessage aMessage)
{
string messageName = aMessage
string messageID = aMessage
IMessage message = m_Factory
IMessageItemSequence replyMessage = m_Factory
RequestHandler handler = FindRequestHandler(messageName);
handler(messageName
message
message
message
return message;
}
利用這種方式
public class TestService:Service
{
public TestService(string aServiceName):base(aServiceName)
{}
protected override void AddRequestHandlers()
{
this
}
private void Test
{
aReplyMessageBody = SendRequest(
aReplyMessageBody
}
}
注意在TestService中的Test
對於客戶端而言
public class MyServiceClient:Service
{
public MyServiceClient(string aServiceName):base(aServiceName)
{}
protected override void AddRequestHandlers()
{}
}
MyServiceClient類即為客戶端定義的服務類
IMessageFactory factory = new MessageFactory();
IMessageItemSequence body = factory
//……
MyServiceClient service = new MyServiceClient(
IMessageItemSequence reply = service
對於service
我們還需要另外定義一個類
public class Server
{
public Server()
{
m_Services = new ArrayList();
}
private ArrayList m_Services;
public void AddService(IListenService service)
{
this
}
public void Initialize()
{
IDictionary tcpProp = new Hashtable();
tcpProp[
tcpProp[
TcpChannel channel = new TcpChannel(tcpProp
ChannelServices
foreach (Service service in m_Services)
{
service
}
}
}
同理
在服務端
static void Main(string[] args)
{
MyService service = new MyService(
TestService service
Server server = new Server();
server
server
server
Console
}
四
利用這個基於消息與
唯一遺憾的是
From:http://tw.wingwit.com/Article/program/net/201311/13559.html