最简单的c#Remoting编程
生活随笔
收集整理的這篇文章主要介紹了
最简单的c#Remoting编程
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.建立一個(gè)類庫(kù)項(xiàng)目:P1
a.類繼承于System.MarshalByRefObject
b.定義要公有訪問的類A1
2.建立一個(gè)服務(wù)器項(xiàng)目
a.增加System.Runtime.Remototing和類庫(kù)項(xiàng)目的引用
b.增加引用:
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.啟動(dòng)服務(wù)
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);//注冊(cè)服務(wù)器通道
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello),"Hi",WellKnownObjectMode.SingleCall);//注冊(cè)遠(yuǎn)程服務(wù)對(duì)象類型
System.Console.WriteLine("hit to exit");
System.Console.ReadLine(); //等待
3.建立一個(gè)客戶端項(xiàng)目
a.增加System.Runtime.Remototing和類庫(kù)項(xiàng)目的引用
b.增加引用:
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.調(diào)用服務(wù)
ChannelServices.RegisterChannel(new TcpClientChannel());//注冊(cè)客戶通道
Hello obj=(Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8086/Hi");//使用透明代理與服務(wù)器通信
if(obj==null)//檢查通信是否成功
{
?Console.WriteLine("Can not locate server!");
?return;
}
for(int i=0;i<5;i++)//調(diào)用服務(wù)器上提供的函數(shù)
{
Console.WriteLine(obj.Greeting("glf"));
}
Console.WriteLine("hit to exit");
Console.ReadLine();
a.類繼承于System.MarshalByRefObject
b.定義要公有訪問的類A1
2.建立一個(gè)服務(wù)器項(xiàng)目
a.增加System.Runtime.Remototing和類庫(kù)項(xiàng)目的引用
b.增加引用:
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.啟動(dòng)服務(wù)
TcpServerChannel channel=new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel);//注冊(cè)服務(wù)器通道
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello),"Hi",WellKnownObjectMode.SingleCall);//注冊(cè)遠(yuǎn)程服務(wù)對(duì)象類型
System.Console.WriteLine("hit to exit");
System.Console.ReadLine(); //等待
3.建立一個(gè)客戶端項(xiàng)目
a.增加System.Runtime.Remototing和類庫(kù)項(xiàng)目的引用
b.增加引用:
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using P1;
c.調(diào)用服務(wù)
ChannelServices.RegisterChannel(new TcpClientChannel());//注冊(cè)客戶通道
Hello obj=(Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8086/Hi");//使用透明代理與服務(wù)器通信
if(obj==null)//檢查通信是否成功
{
?Console.WriteLine("Can not locate server!");
?return;
}
for(int i=0;i<5;i++)//調(diào)用服務(wù)器上提供的函數(shù)
{
Console.WriteLine(obj.Greeting("glf"));
}
Console.WriteLine("hit to exit");
Console.ReadLine();
總結(jié)
以上是生活随笔為你收集整理的最简单的c#Remoting编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WCF中的web.config配置文件
- 下一篇: 闲记(一)