Remoting入门实例

前些时候看到园子里一位朋友写的.NET 框架 AAF (作者:http://blog.csdn.net/aafshzj),作者对其大为赞赏.

因为自己在开发过程中碰到一些性能问题,一时不知如何解决,索性就学习下AAF.因为本人对该框架知之甚少,所以就不在此赘言了.

早先时候,自己也曾写过Remoting的代码,没有实际应用到项目中去. .NET发展太快了,4.0就有出来了.微软也用新的技术来替代Remoting了,那就是WCF,不过那是后话了.

有感于AAF,作者给出Remoting的入门代码,已帮助了解该技术的Programmer.哈哈,不多说了,看例子吧.

下边以3个部分来介绍(共享库,服务器端,客户端)

Step 1:

先创建个共享库,客户端和服务器端要添加对它的引用.

复制代码
共享库
namespace ResumeServerLibrary
{
    public abstract class RemoteObject :MarshalByRefObject
    {
        public abstract string Show(string name);
        public abstract string Call(int i);
    }
}
复制代码

很简单,就两个方法,Show & Call,没有包含具体业务哦,这一点很重要.主要是安全方面的考虑.

 

Step 2:

现在看看服务器端,分两部分,

先看对共享库方法的重写

复制代码
Code
namespace ServerHello
{
    class ServerHello : ResumeServerLibrary.RemoteObject
    {
        public override string Show(string name)
        {
            return String.Format("{0}时,客户端传入\"{1}\"", DateTime.Now.ToString(), name);
        }

        public override string Call(int i)
        {
            return (Math.Abs(i) + 10000).ToString();
        }
    }
}
复制代码

 

 然后是服务器端通道的建立及侦听,代码如下

复制代码
Code
namespace ServerHello
{
    public class ServerObject
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                TcpServerChannel channel = new TcpServerChannel("TCP6666",6666);
                
                ChannelServices.RegisterChannel(channel,false);
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerHello),"Show",WellKnownObjectMode.Singleton);
                Console.WriteLine("提示,通道已打开:");
                Console.ReadLine();
 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.Source);
                Console.ReadLine();
            }
        }
    }
}
复制代码

激活模式为服务器端激活,又称为知名对象(WellKnown Objects)激活模式,服务器端激活模式根据激活方式的不同,又分为SingleTon激活模式和SingleCall激活模式。

SingleTon模式激活的对象始终只有一个,而不管客户端的多少。(你可以参考一下设计模式的SingleTon模式。)而SingleCall模式则故名思义。它的对象创建是根据调用方法来决定的,每调用一次方法,都会创建该对象;而一旦调用下次方法时,则上次创建的对象会被销毁,并重新创建新对象。

当然了,相对于服务器端激活模式,还有客户端激活模式.两者的区别是

1、服务器端激活是无状态态,它的特性更类似于Web Service。而客户端激活是有状态的,它的特性更像是你在本地实例化的一个对象。

2、服务器端激活模式中,SingleCall激活的对象是无法管理其生命周期的;而SingleTon激活模式以及客户端激活模式则可以管理生命周期。

3、服务器端激活模式对于其MarshalByRefObject对象本身只能使用默认的构造函数,而客户端激活模式则可以使用自定义的构造函数。

好了,到这里您对基本概念已经有了了解.我们接下来看客户端如何调用

Step 3. 客户端调用

本人客户端是部署在IIS中,其他环境与此类似.

复制代码
Code
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack) dataBind();
    }
    private void dataBind()
    {

        TcpClientChannel channel = new TcpClientChannel();
        IChannel[] services = ChannelServices.RegisteredChannels;
        bool isExist = false;
        for (int i = 0, j=services.Length; i < j; i++)
        {
            if("TCP6666" == services[i].ChannelName)
            {
                isExist = true;
                break;
            }
        }

        if(!isExist)
            ChannelServices.RegisterChannel(channel,false);

        string path = System.Configuration.ConfigurationManager.AppSettings["TcpUrl"];
        string uri = System.Configuration.ConfigurationManager.AppSettings["TcpUri"];

        ResumeServerLibrary.RemoteObject hello = (ResumeServerLibrary.RemoteObject)Activator.GetObject(typeof(ResumeServerLibrary.RemoteObject), String.Format("tcp://{0}/Show", path));
        ChannelServices.UnregisterChannel(channel);
        if(hello != null)
        {
            Response.Write(hello.Show("远程Show方法被调用,"));
            Response.Write("<br/>");
            Response.Write("远程Call方法被调用,返回值:"+hello.Call(2009));
        }


    }
}
复制代码

 


读者自己可以试试啦,记着服务端先运行哦.

转载于:https://www.cnblogs.com/zhangchenliang/archive/2012/08/23/2653443.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值