代码改变世界

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

2010-08-26 23:00  Clingingboy  阅读(756)  评论(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兼可