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)
&n