19
2012
07

对Remoting进行封装,方便使用


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
using System.Xml;
using System.Runtime.Remoting;
using System.Collections;
namespace Tools
{
    /// <summary>
    /// Remote自定义操作类,更方便使用
    /// </summary>
    public class RemoteHelper
    {
        /// <summary>
        /// 服务端注册的端口信息
        /// </summary>
        public static IDictionary ServerHttpProp = new Hashtable();
        /// <summary>
        /// 客户端连接服务端的端口
        /// </summary>
        public static int ClientToServerPort = 8888;
        /// <summary>
        /// 服务端的http地址
        /// </summary>
        private string url = "";
        #region 单个实例  Instance
        private static readonly RemoteHelper instance = new RemoteHelper();
        /// <summary>
        /// 单个实例  Instance
        /// </summary>
        public static RemoteHelper Instance
        {
            get
            {
                return instance;
            }
        }
        #endregion
        #region 构造函数
        /// <summary>
        /// 构造函数 
        /// </summary>
        private RemoteHelper()
        {
        }
        #endregion
        #region 获取服务端的App.config中配置的http端口号
        /// <summary>
        /// 获取服务端的App.config中配置的http端口号 
        /// /configuration/applicationSettings/RemoteServer.Properties.Settings/setting/value 
        /// </summary>
        private void GetServerPortInfo()
        {
            string serverPort = "8888";//默认端口 
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ExecutablePath + ".config");
                XmlElement xEleml = (XmlElement)xDoc.SelectSingleNode("/configuration/applicationSettings/RemoteServer.Properties.Settings/setting/value");
                serverPort = xEleml.InnerText;//得到远程端口
            }
            catch
            {
                serverPort = "8888";
            }
            ServerHttpProp["name"] = "http" + serverPort;
            ServerHttpProp["port"] = serverPort;
        }
        private void GetServerPortInfo(int port)
        {
            string serverPort = port.ToString();//默认端口 
            ServerHttpProp["name"] = "http" + serverPort;
            ServerHttpProp["port"] = serverPort;
        }
        #endregion
        #region 服务端-注册HttpChane通道
        /// <summary>
        /// 服务端-注册HttpChane通道
        /// </summary>
        public void RegisterServerHttpChanel()
        {
            GetServerPortInfo();
            IChannel chanel = new HttpChannel(ServerHttpProp, new SoapClientFormatterSinkProvider(), new SoapServerFormatterSinkProvider());
            ChannelServices.RegisterChannel(chanel, false);
        }
        /// <summary>
        /// 服务端-注册HttpChane通道
        /// </summary>
        /// <param name="port">商品号</param>
        public void RegisterServerHttpChanel(int port)
        {
            GetServerPortInfo(port);
            IChannel chanel = new HttpChannel(ServerHttpProp, new SoapClientFormatterSinkProvider(), new SoapServerFormatterSinkProvider());
            ChannelServices.RegisterChannel(chanel, false);
        }
        #endregion
        #region 服务端-注册http远程对象,为客户端提供服务
        /// <summary>
        /// 服务端-注册http远程对象,为客户端提供服务
        /// </summary>
        /// <param name="ClassFullPath">实现接口的类全路径(前面要带上命名空间名称)</param>
        /// <param name="ServerProjectName">(服务端)实现接口的类对应的项目名称</param>
        /// <param name="InterfaceNames">服务端和客户端共享的接口名称(可以取其它名称不影响使用)</param>
        public void RegisterServerHttpObject(string ClassFullPath, string ServerProjectName, string InterfaceNames)
        {
            RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType(ClassFullPath + "," + ServerProjectName), InterfaceNames + ".soap", WellKnownObjectMode.Singleton);
        }
        #endregion
        #region 客户端-注册HttpChane通道
        /// <summary>
        /// 客户端-注册HttpChane通道
        /// 获取客户端配置的http地址和端口 /configuration/connectionStrings/add
        /// 格式为: http://127.0.0.1:8888/  最后要带“/” 
        /// </summary>
        public void RegisterClientHttpObject()
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.ExecutablePath + ".config");
                this.url = ((XmlElement)xDoc.SelectSingleNode("/configuration/connectionStrings/add")).GetAttribute("connectionString");
            }
            catch (Exception er)
            {
                this.url = "http://127.0.0.1:8888/";
                throw er;
            }
            ClientToServerPort = Convert.ToInt32(url.Split(':')[2].TrimEnd('/'));
            HttpChannel chan2 = new HttpChannel(ClientToServerPort);
            ChannelServices.RegisterChannel(chan2, false);
        }
        /// <summary>
        /// 客户端-注册HttpChane通道
        /// </summary>
        /// <param name="serverIp">服务端ip</param>
        /// <param name="serverPort">服务端端口</param>
        /// <param name="clientPort">客户端端口</param>
        public void RegisterClientHttpObject(string serverIp,int serverPort,int clientPort )
        {
            try
            {
                this.url = string.Format("http://{0}:{1}/",serverIp,serverPort);
            }
            catch (Exception er)
            {
                this.url = "http://127.0.0.1:8888/";
                throw er;
            }
            ClientToServerPort = clientPort;
            HttpChannel chan2 = new HttpChannel(ClientToServerPort);
            ChannelServices.RegisterChannel(chan2, false);
        }
        #endregion
        #region 客户端获得http远程对象
        /// <summary>
        /// 客户端获得http远程对象
        /// </summary>
        /// <param name="type">传入 typeof(存在的接口名称) 即可</param>
        /// <param name="InterfaceNames">服务端注册的(接口)名称</param>
        /// <returns></returns>
        public object GetHttpObject(Type type, string InterfaceNames)
        {
            object serviceObj = Activator.GetObject(type, this.url + InterfaceNames + ".soap");
            if (serviceObj == null)
            {
                MessageBox.Show("远程连接失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return null;
            }
            return serviceObj;
        }
        #endregion
    }
}


使用方法:


Interface1代码:

string Getinfo(string type);


serverMethod1代码:

public class serverMethod1 : MarshalByRefObject,Interface1
    {
        public string Getinfo(string type)
        {
            if (type == "1") return "这是1";
            else return "参数不是1";
        }
    }


服务端主要代码 :

 RemoteHelper rh= RemoteHelper.Instance;
           rh.RegisterServerHttpChanel(8400);
           rh.RegisterServerHttpObject("ServerServices.serverMethod1", "ServerServices", "Interface1");


客户端主要代码:

private RemoteHelper rh = RemoteHelper.Instance;
        private Interface1 intface = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            rh.RegisterClientHttpObject("127.0.0.1", 8400, 8401);
            intface = (Interface1)rh.GetHttpObject(typeof(Interface1), "Interface1");
        }
private void buttonDoMethod_Click(object sender, EventArgs e)
        {
            MessageBox.Show(intface.Getinfo(textBox1.Text.Trim()), "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }


效果:












版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

相关文章:

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。