C#远程调用实现案例
来源:互联网 发布:java 自动按键 编辑:程序博客网 时间:2024/05/22 14:58
C#实现远程调用主要用到“System.Runtime.Remoting”这个东西。下面从三个方面给于源码实例。
·服务端:
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
namespace RemoteSample
...{
class server
...{
static int Main(string[] args)
...{
//注册通道
TcpChannel chan = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
string sshan = chan.ChannelName;
System.Console.WriteLine(sshan);
//注册远程对象,即激活.
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteSample.HelloServer), "SayHello", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Hit <ennter> to exit...");
System.Console.ReadLine();
return 0;
}
}
}
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
namespace RemoteSample
...{
class server
...{
static int Main(string[] args)
...{
//注册通道
TcpChannel chan = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
string sshan = chan.ChannelName;
System.Console.WriteLine(sshan);
//注册远程对象,即激活.
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteSample.HelloServer), "SayHello", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Hit <ennter> to exit...");
System.Console.ReadLine();
return 0;
}
}
}
·客户端:
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
namespace RemoteSample
...{
public class Client
...{
public static int Main(string[] args)
...{
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
HelloServer obj =(HelloServer)Activator.GetObject(typeof(HelloServer), "tcp://localhost:8085/SayHello");
if (obj == null)
System.Console.WriteLine("Could not locate server");
else Console.WriteLine(obj.HelloMethod("Caveman"));
return 0;
}
}
}
using System.Runtime.Remoting.Channels;
namespace RemoteSample
...{
public class Client
...{
public static int Main(string[] args)
...{
TcpChannel chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
HelloServer obj =(HelloServer)Activator.GetObject(typeof(HelloServer), "tcp://localhost:8085/SayHello");
if (obj == null)
System.Console.WriteLine("Could not locate server");
else Console.WriteLine(obj.HelloMethod("Caveman"));
return 0;
}
}
}
·远程对象:(重点),该对象是一个dll的程序集,同时被客户端和服务器端引用。
namespace RemoteSample
...{
//客户端获取到服务端的对象实际是一个对象的引用,因此必须继承:MarshalByRefObject
public class HelloServer : MarshalByRefObject
...{
public HelloServer()
...{
Console.WriteLine("HelloServer activated");
}
public String HelloMethod(String name)
...{
Console.WriteLine("Hello.HelloMethod : {0}", name);
return "Hi there " + name;
}
//说明1:在Remoting中的远程对象中,如果还要调用或传递某个对象,例如类,或者结构,则该类或结构则必须实现串行化Attribute[SerializableAttribute]:
//说明2:将该远程对象以类库的方式编译成Dll。这个Dll将分别放在服务器端和客户端,以添加引用
//说明3:在Remoting中能够传递的远程对象可以是各种类型,包括复杂的DataSet对象,只要它能够被序列化
}
...{
//客户端获取到服务端的对象实际是一个对象的引用,因此必须继承:MarshalByRefObject
public class HelloServer : MarshalByRefObject
...{
public HelloServer()
...{
Console.WriteLine("HelloServer activated");
}
public String HelloMethod(String name)
...{
Console.WriteLine("Hello.HelloMethod : {0}", name);
return "Hi there " + name;
}
//说明1:在Remoting中的远程对象中,如果还要调用或传递某个对象,例如类,或者结构,则该类或结构则必须实现串行化Attribute[SerializableAttribute]:
//说明2:将该远程对象以类库的方式编译成Dll。这个Dll将分别放在服务器端和客户端,以添加引用
//说明3:在Remoting中能够传递的远程对象可以是各种类型,包括复杂的DataSet对象,只要它能够被序列化
}
注意上述代码的注释,由于远程服务的特殊性,因此在此做了详细的批注,怕大伙不理解。
OK。C#的远程调用就实现完成了,这中应用一般在三层架构中应该比较平常使用。至于这种方式的优缺点,在下还不好说,希望有过实际应用的同志给总结一些,谢谢!!!
- C#远程调用实现案例
- Hessian实现远程调用的简单案例
- C#远程调用方法
- c#调用TeamViewer或AnyDesk实现远程控制
- c#调用TeamViewer或AnyDesk实现远程控制
- Spring实现远程调用
- rmi实现远程调用
- Hessian实现远程调用
- HttpClient实现远程调用
- HttpClient实现远程调用
- CXF实现远程调用
- C#实现远程控制
- c# 实现远程关机
- c#实现远程连接
- C#异步调用案例 BeginInvoke用法
- C# webservices 之调用天气预报案例【1】
- c#创建调用bat文件简单案例
- C#调用支付宝接口案例
- 一个在MFC环境中使用的 InfoZip打包类
- vc中程序最小化到托盘(任务拦)的实现
- Symbian 开发人员证书申请流程
- reportviewer动态加载报表的实现以及动态控制报表,套打,存折打印模式等的一些探讨,欢迎批评指正!
- 一个托盘程序演示 -闹钟 Alert
- C#远程调用实现案例
- 20071113听力原文
- Linux下多个jdk引发的问题
- [翻译]ASP.NET AJAX调用Web Service(超级好)
- 1、面向对象的特征有哪些方面
- 20071114听力原文
- CDownloadGroup 和 CDownloadGroups类
- Atlas控件使用说明
- J2EE学习笔记三:EJB基础概念和知识