当前位置: 首页 > news >正文

.NET Remoting Basic(10)-创建不同宿主的客户端与服务器端

除了控制台之外,现有.net 客户端分为asp.net,WinForm和WPF,Silverlight则无权限直接访问.不过本质流程是相同的.

一.控制台

一直以来都是以控制台来演示。为保持完整性,重新来一遍.

1.配置文件

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
     <system.runtime.remoting>
         <application>
             <channels>
                 <channel ref="tcp" port="1234">
                     <serverProviders>
                         <formatter ref="binary" />
                     </serverProviders>
                 </channel>
             </channels>
             <service>
                 <wellknown    type="Server.ServerImpl, Server" 
                                     objectUri="MyServer.rem"
                                     mode="Singleton" />
             </service>
         </application>
     </system.runtime.remoting>
 </configuration>

2.Server端代码初始化

    [STAThread]
     static void Main(string[] args)
     {
         System.Console.WriteLine("Starting server...");
         RemotingConfiguration.Configure("Server.exe.config");
 
         System.Console.WriteLine("Server configured, waiting for requests!");
         System.Console.ReadLine();
     }
 }


3.客户端

3.1配置文件

<configuration>
     <system.runtime.remoting>
         <application name="FirstServer">
             <channels>
                 <channel ref="tcp" />
             </channels>
             <client>
                 <wellknown    type="General.IRemoteFactory, General"
                                     url="tcp://localhost:1234/MyServer.rem" />
             </client>
         </application>
     </system.runtime.remoting>
 </configuration>
 

3.2客户端测试

[STAThread]
 static void Main(string[] args)
 {
     System.Console.WriteLine("Configuring client...");
     RemotingConfiguration.Configure("ConsoleClient.exe.config");
 
     System.Console.WriteLine("Calling server 1...");
     IRemoteFactory factory = (IRemoteFactory)RemotingHelper.CreateProxy(typeof(IRemoteFactory));
     Person p = factory.GetPerson();
     System.Console.WriteLine(">> Person retrieved: {0} {1}, {2}", p.Firstname, p.Lastname, p.Age.ToString());
     System.Console.WriteLine();
 
  Console.ReadLine();
 }

二.以WinForm为客户端

服务器端如上不变

1.配置客户端初始化

public class WinApplication
     {
         private static IRemoteFactory _factory = null;
 
         public static IRemoteFactory ServerProxy 
         {
             get 
             {
                 if(_factory == null) 
                 {
                     _factory = (IRemoteFactory)RemotingHelper.CreateProxy(typeof(IRemoteFactory));
                 }
 
                 return _factory;
             }
         }
 
         public static void Main(string[] args) 
         {
             // First of all configure remoting services
             RemotingConfiguration.Configure("WinClient.exe.config");
 
             // Create the windows form and start message looping
             Application.Run(new MainForm());
         }
     }

2.UI界面

private void ActionCall_Click(object sender, System.EventArgs e)
 {
     // Get the transparent proxy for the factory
     IRemoteFactory proxy = WinApplication.ServerProxy;
     Person p = proxy.GetPerson();
     
     TextResults.AppendText(
         string.Format("{0} {1}, {2}\r\n", p.Firstname, p.Lastname, p.Age));
 }


三.以ASP.NET为客户端

1.将配置文件配置在web.config中

2.在Global.asax的ApplicationStart事件中初始化客户端

protected void Application_Start(Object sender, EventArgs e)
 {
     // Configure the remoting server
     RemotingConfiguration.Configure(Server.MapPath("web.config"));
 }


四.以ASP.NET为服务器端

web容易部署,可以直接部署在IIS上

1.配置一个xml文件,如RemotingClient.config
2.在Global.asax的ApplicationStart事件中初始化服务器端

protected void Application_Start(Object sender, EventArgs e)
 {
     RemotingConfiguration.Configure(Server.MapPath("RemotingClient.config"));
 }

访问可以用控制台,WinForm,ASP.NET兼可

转载于:https://www.cnblogs.com/Clingingboy/archive/2010/08/26/1809585.html

相关文章:

  • Delphi 中的DLL 封装和调用对象技术
  • 如何找回自己!
  • PHP中魔术方法
  • SQL Server到Oracle程序迁移
  • Vistual Studio XML 智能提示
  • [转]关于Linux 静态库和动态库
  • muduo 与 libevent2 吞吐量对比
  • TabController控件测试
  • 第13届年度Webby奖采用Silverlight / 13th Annual Webby Awards powered by Silverlight
  • 网页-CSS
  • 王德兴读《吴越品水浒》有感15:吴越对“鲁智深出家”的无知和诬蔑
  • C#读取特定目录下的所有文件
  • debug下运行不成功
  • [转]Apache虚拟目录设置方法
  • 11个IT人士必备的Chrome 插件
  • Druid 在有赞的实践
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • Java程序员幽默爆笑锦集
  • Leetcode 27 Remove Element
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Nodejs和JavaWeb协助开发
  • PHP 的 SAPI 是个什么东西
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • 从零开始的webpack生活-0x009:FilesLoader装载文件
  • 计算机在识别图像时“看到”了什么?
  • 记录一下第一次使用npm
  • 力扣(LeetCode)56
  • 温故知新之javascript面向对象
  • 你对linux中grep命令知道多少?
  • Spring第一个helloWorld
  • 湖北分布式智能数据采集方法有哪些?
  • ​flutter 代码混淆
  • ​TypeScript都不会用,也敢说会前端?
  • ​卜东波研究员:高观点下的少儿计算思维
  • ​马来语翻译中文去哪比较好?
  • ​批处理文件中的errorlevel用法
  • (备忘)Java Map 遍历
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • .NET Entity FrameWork 总结 ,在项目中用处个人感觉不大。适合初级用用,不涉及到与数据库通信。
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET开发人员必知的八个网站
  • .NET开源项目介绍及资源推荐:数据持久层 (微软MVP写作)
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • .Net转Java自学之路—基础巩固篇十三(集合)
  • .vimrc php,修改home目录下的.vimrc文件,vim配置php高亮显示
  • [ Algorithm ] N次方算法 N Square 动态规划解决
  • [ vulhub漏洞复现篇 ] ECShop 2.x / 3.x SQL注入/远程执行代码漏洞 xianzhi-2017-02-82239600
  • [20170713] 无法访问SQL Server
  • [AX]AX2012开发新特性-禁止表或者表字段
  • [bzoj1038][ZJOI2008]瞭望塔
  • [C++]高精度 bign (重载运算符版本)
  • [CTO札记]盛大文学公司名称对联
  • [emacs] CUA的矩形块操作很给力啊
  • [IE编程] 如何获得IE版本号
  • [IE编程] 如何设置IE8的WebBrowser控件(MSHTML) 的渲染模式