.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值