remoting教学五:将remoting宿主到IIS

本文提供源码下载:http://download.csdn.net/source/2391281

remoting的宿主,可以为:
1:你自己的代码;
2:WINDOWS服务;
3:IIS;
而寄宿到IIS中,有些特别的优势,如:
1:可以借助于IIS的安全机制;
2:在硬件环境不允许的情况下,和IIS共用端口。

当然,寄宿IIS必然会受到一些限制,如只能使用IIS通道。另外,这里还有一份建议,如果你的IIS是部署在互联网中,则永远不要使用回调及事件。更多的资料,请查看http://www.thinktecture.com/resourcearchive/net-remoting-faq/remotingusecases

特别需要说明的几点:
1:宿主时候,必须在Global.asax的void Application_Start(object sender, EventArgs e)编写初始化代码;
2:服务器端不能指定端口,系统会自动绑定IIS的端口;
3:激活方式,采用服务器端激活。无论Singleton何SingleCall都可以。理论上客户端激活也可以,但我没有实现过。
4:客户端的端口必须指定为0,如下:
            IDictionary diction = new Hashtable();
            diction["name"] = "http";
            diction["port"] = 0;
     指定为0,意思是客户端自动选择端口。
5:本文虽实现了回调,但不建议在外网中使用。

给出部分源码:

服务器端:
void Application_Start(object sender, EventArgs e)
    {
        System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider binaryClient = new System.Runtime.Remoting.Channels.BinaryClientFormatterSinkProvider();
        System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider provider = new System.Runtime.Remoting.Channels.BinaryServerFormatterSinkProvider();

        provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
        IDictionary props = new Hashtable();
        props["name"] = "http";
        System.Runtime.Remoting.Channels.Http.HttpChannel chan =
            new System.Runtime.Remoting.Channels.Http.HttpChannel(props, binaryClient, provider);
        System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chan, false);

        //服务端激活
        System.Runtime.Remoting.RemotingConfiguration.RegisterActivatedServiceType(
            typeof(RemoteServices.RemoteService1));
        System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType
                (
                typeof(RemoteServices.RemoteService1),
                "abc.soap",
                System.Runtime.Remoting.WellKnownObjectMode.Singleton
                );
        //end 服务端激活

    }

客户端:
         static void Main(string[] args)
        {
            Console.Read();
            BinaryClientFormatterSinkProvider binaryClient = new BinaryClientFormatterSinkProvider();
            BinaryServerFormatterSinkProvider binaryServer = new BinaryServerFormatterSinkProvider();

            binaryServer.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary diction = new Hashtable();
            diction["name"] = "http";
            diction["port"] = 0;
            HttpChannel tcp = new HttpChannel(diction, binaryClient, binaryServer);
            ChannelServices.RegisterChannel(tcp, false);

            //服务器端激活
            Console.WriteLine("AppDomain.CurrentDomain.Id:" + AppDomain.CurrentDomain.Id);
            string remoteAddress = "http://localhost:7108/WebSiteRemoting/abc.soap";
            IRemoteService1 remoteobj =
                (IRemoteService1)Activator.GetObject(typeof(IRemoteService1), remoteAddress);
            //end 服务器端激活
            Console.WriteLine("1 + 2 = " + remoteobj.Add(1, 2, new Serverce1CallbackHandler()));
            Console.WriteLine("remote object hash:" + remoteobj.GetSessionID());
            Console.ReadLine();

        }

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luminji/article/details/5622381
个人分类: 分布式-remoting
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭