2009年2月18日 星期三

C# Remoting


Remoting可建立的Channel有(1)TCP Channel. (2)Http Channel. (3)IPC Channel

.Net 1.1 : TCP Channel、Http Channel.
.Net 2.0 : 之後多了IPC Channel 使用Windows程式間通信系統(IPC:Inter process Communication)

Remoting 的使用流程
1.建立Remoting 物件
2.在Server建立使用Remoting物件的Channel
3.在Client使用Remoting


//------------------ Example --------------------//
1.建立Remoting物件









1.1 建立Interface1.2.實作Remoting物件
using System;
using System.Collections;
namespace G5.RemotingEvent.ICommon
{
public delegate void PAEventHandler(Hashtable fax);
public interface IG5Business
{
void SendPA(Hashtable fax);
}
}

using System;
using System.Collections;
using G5.RemotingEvent.ICommon;

namespace G5.RemotingEvent.RemoteObj
{
public class G5Business:MarshalByRefObject,IG5Business
{
// 建立Event
public static event PAEventHandler PASendedEvent;
#region
public void SendPA(Hashtable fax)
{
if (PASendedEvent != null)
{
PASendedEvent(fax);
}
}
#endregion
public override object InitializeLifetimeService()
{
return null;
}
}
}

2.在Server建立使用Remoting物件的Channel





2.1 using
//using System.Runtime.Remoting.Channels.Http;
//using System.Runtime.Remoting.Channels.Tcp;

using System.Runtime.Remoting.Channels.Ipc;

2.2 建立Channel
// IPC Channel
IpcChannel channel = new IpcChannel("G5Channel");
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof (G5Business), "G5Loader", WellKnownObjectMode.Singleton);

//TCP Channel
//TcpServerChannel channel = new TcpServerChannel(3210);
//ChannelServices.RegisterChannel(channel, false);
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(G5Business), "G5Loader", WellKnownObjectMode.Singleton);

// Http Channel
// HttpChannel channel = new HttpChannel(8080);
// ChannelServices.RegisterChannel(channel);
// RemotingConfiguration.RegisterWellKnownServiceType(typeof(G5Business),"G5Business.soap",WellKnownObjectMode.Singleton);

G5Business.PASendedEvent += new PAEventHandler(OnPASended);



3.在Client使用Remoting




1.using

//using System.Runtime.Remoting.Channels.Http;
//using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;

2.使用Channel
private IG5Business faxBus = null;

// IPC Channel
string stringIpc = "ipc://G5Channel/G5Loader";
faxBus = (IG5Business)Activator.GetObject(typeof(IG5Business), stringIpc);

//TCP Channel
//string stringTcp = "tcp://localhost:3210/TestLoader";
//faxBus = (IG5Business)Activator.GetObject(typeof(IG5Business), stringTcp);
//Http Channel
//HttpChannel channel = new HttpChannel(0);
//ChannelServices.RegisterChannel(channel);
//faxBus = (IG5Business)Activator.GetObject(typeof(IG5Business),
// http://localhost:8080/G5Business.soap);



參考資料:
http://www.cnblogs.com/idior/archive/2007/01/04/611265.html (原理)
http://www.codeguru.com/csharp/csharp/cs_syntax/remoting/article.php/c9251



沒有留言:

張貼留言