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();

        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值