代码改变世界

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

  Clingingboy  阅读(757)  评论(0编辑  收藏  举报

除了控制台之外,现有.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兼可
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2009-08-26 Spring.NET学习笔记(6)-基础AOP
点击右上角即可分享
微信分享提示