Remoting 技术
Remoting 是基于TCP連接的,要實現(xiàn)她要先創(chuàng)建一個類庫,然后將類庫的dll文件引用到服務(wù)端和客戶端
類庫的主要代碼:
創(chuàng)建一個類庫叫Compute ,在類庫中創(chuàng)建一個接口ICompute
ICompte的代碼:
public interface ICompute
{
int Mutil(int a, int b);
}
定義一個Mathes的類,必須繼承一個支持遠程訪問的對象
public class Mathes : MarshalByRefObject , ICompute
{
public Mathes()
{
Console.WriteLine("創(chuàng)建一個對象實例");
}
#region ICompute 成員
public int Mutil(int a, int b)
{
return a * b;
}
#endregion
}
?
創(chuàng)建一個控制臺程序
服務(wù)器的主要代碼:
static void Main(string[] args)
{
Console.WriteLine("服務(wù)器");
//注冊一個通道
ChannelServices.RegisterChannel(new TCPServerChannel(50000));
//注冊一個類型
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Compute.Mathes),"abc",WellKnownObjectMode.Singleton);
Console.Read();
}
?
客戶端主要代碼:
static void Main(string[] args)
{
Console.WriteLine("客戶端");
//注冊一個通道,不需要指定端口號
ChannelServices.RegisterChannel(new TcpChannel());
//獲取類型的實例
Compute.Mathes ma = Activator.GetObject(typeof(Compute.Mathes),"tcp://127.0.0.1:50000/abc") as Compute.Mathes;
int result = ma.Mutil(2,3);
Console.WriteLine(result);
?
Console.Read();?
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/jasonjiang/archive/2010/06/24/1764491.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Remoting 技术的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SharePoint 2010开发实例精
- 下一篇: 图解Silverlight 3的7个新功