简单Remoting例子

来源:互联网 发布:淘宝客户端删除评价 编辑:程序博客网 时间:2024/06/07 06:56

参考MSDN给出的例子

 

包含Remoting两种协议(tcp,http)的代码,其中在RemoteObject类添加数据访问的代码,则可实现NET Framwork间的简单分布式数据库程序。

 

 

1.服务端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Security.Permissions;
using RemotingModel;

namespace RemotingServer
{
    class Program
    {
        [SecurityPermission(SecurityAction.Demand)]
        static void Main(string[] args)
        {
            //TcpServer();

            HttpServer();
        }

        private static void TcpServer()
        {
            TcpChannel serverChannel = new TcpChannel(9090);
            ChannelServices.RegisterChannel(serverChannel, false);
            ChannelDataStore data = (ChannelDataStore)serverChannel.ChannelData;
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject.rem", WellKnownObjectMode.Singleton);

            Console.WriteLine("Press ENTER to exit the server.");
            Console.ReadLine();
        }

        private static void HttpServer()
        {
            HttpChannel serverChannel = new HttpChannel(9090);
            ChannelServices.RegisterChannel(serverChannel,false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject.rem", WellKnownObjectMode.Singleton);
           
            Console.WriteLine("Press ENTER to exit the server.");
            Console.ReadLine();
        }
    }
}

 

2.客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using RemotingModel;
using System.Runtime.Remoting.Messaging;

namespace RemotingClient
{
    class Program
    {
        [SecurityPermission(SecurityAction.Demand)]
        static void Main(string[] args)
        {
            //TcpClient();

            HttpClient();
        }

        private static void TcpClient()
        {
            TcpChannel clientChannel = new TcpChannel();
            ChannelServices.RegisterChannel(clientChannel,false);
            WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(RemoteObject), "tcp://localhost:9090/RemoteObject.rem");
            RemotingConfiguration.RegisterWellKnownClientType(remoteType);

            RemoteObject service = new RemoteObject();
            Console.WriteLine("The remote object has been called {0} times.", service.GetCount());
        }

        private static void HttpClient()
        {
            HttpClientChannel clientChannel = new HttpClientChannel();
            ChannelServices.RegisterChannel(clientChannel,false);
            WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(RemoteObject),"http://localhost:9090/RemoteObject.rem");
            RemotingConfiguration.RegisterWellKnownClientType(remoteType);
           
            RemoteObject service = new RemoteObject();
            Console.WriteLine("The remote object has been called {0} times.", service.GetCount());

        }

    }
}

 

3.对象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;

namespace RemotingModel
{
    public class RemoteObject : MarshalByRefObject
    {
        private int callCount = 0;

        public int GetCount()
        {
            callCount++;
            return (callCount);
        }
    }
}

 

0 0