您的位置:首页 > 编程语言

【Remoting-5代码实现】

2015-09-20 20:04 169 查看
服务端

class RemotingServiceHelper
{
private static string m_protocolType;
private static string urlString = "{0}://{1}:{2}/{3}";
/// <summary>
/// 注册Ichannel通道
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static bool RegisterChannel(ChannelType type)
{
try
{
IChannel channel;
IDictionary tables=new Hashtable();
tables["port"] = 9011;
switch (type)
{
case ChannelType.HttpChannel:
tables["name"] = "HttpChannel";
channel=new HttpChannel(tables,new SoapClientFormatterSinkProvider(),
new SoapServerFormatterSinkProvider());
m_protocolType = "http";
break;
case ChannelType.TcpChannel:
tables["name"] = "TcpChannel";
channel=new TcpChannel(tables,new BinaryClientFormatterSinkProvider(),
new BinaryServerFormatterSinkProvider() );
m_protocolType = "tcp";
break;
case ChannelType.IpcChannl:
tables["name"] = "IpcChannel";
channel=new IpcChannel(tables,new BinaryClientFormatterSinkProvider(),
new BinaryServerFormatterSinkProvider());
m_protocolType = "ipc";
break;
default:
channel = null;
return false;

}
ChannelServices.RegisterChannel(channel,false);
Console.WriteLine("服务注册成功!端口号为:{0},通道类型为{1}", tables["port"],type);
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
//  channel = null;
return false;
}
}

public static bool ActivatedServiceObject(ActivatedType type)
{
RemotingConfiguration.ApplicationName = "RemotingService";
string url=String.Empty;
string objUri = type.ToString();
switch (type)
{
case ActivatedType.Client:
Console.WriteLine("激活方式:客户端激活");
//在服务端上,指定使用客户端激活方式激活远程服务对象
RemotingConfiguration.RegisterActivatedServiceType(typeof(ServiceObj));
url = string.Format(urlString, m_protocolType, IPAddress.Loopback, 9011,
RemotingConfiguration.ApplicationName);
break;
case ActivatedType.ServiceSingleCall:
Console.WriteLine("激活方式:服务端ServiceSingleCall激活");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceObj),"ServiceSingleCall",WellKnownObjectMode.SingleCall);
url = string.Format(urlString+"/{4}", m_protocolType, IPAddress.Loopback, 9011,
RemotingConfiguration.ApplicationName, objUri);
break;
case ActivatedType.ServiceSingleton:
Console.WriteLine("激活方式:服务端ServiceSingleton激活");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceObj), "ServiceSingleton", WellKnownObjectMode.Singleton);
url = string.Format(urlString + "/{4}", m_protocolType, IPAddress.Loopback, 9011,
RemotingConfiguration.ApplicationName, objUri);
break;
default :
return false;
}
Console.WriteLine("远程对象的Url地址为:{0}",url);
return true;
}
}

enum ChannelType
{
TcpChannel,
HttpChannel,
IpcChannl,
}

enum ActivatedType
{
Client,
ServiceSingleton,
ServiceSingleCall,
}


class Program
{
static void Main(string[] args)
{
Console.WriteLine("注册Ichannel通道");
if (RemotingServiceHelper.RegisterChannel(ChannelType.HttpChannel))
{
// Console.WriteLine("通道注册成功,通道类型为{0}",ChannelType.TcpChannel);
if (RemotingServiceHelper.ActivatedServiceObject(ActivatedType.ServiceSingleton))
{
Console.WriteLine("远程服务对象激活成功!");
Console.ReadKey();
}
}
}
}


客户端:

【注意对服务程序集的引用或是分离】

class ClientProxy
{
private string urlString = "{0}://{1}:{2}/{3}";
private ServiceObj _serviceObj;

private string m_protocol;
private string m_appName;
private ActivatedType m_activatedType;
public ClientProxy(string protocol, string appName, ActivatedType activatedType)
{
m_protocol = protocol;
m_appName = appName;
m_activatedType = activatedType;
}

public ServiceObj ServiceObj
{
get
{
if (ActivatorServiceObj())
{
return _serviceObj;
}
else
{
Console.WriteLine("激活远程对象失败");
return null;
}

}
}

private bool ActivatorServiceObj()
{
string url = GetUrlString();
// string url = "";
try
{
//  _serviceObj = (ServiceObj)Activator.GetObject(typeof(ServiceObj), url);
switch (m_activatedType)
{
case ActivatedType.Client:
//先激活远程对象,再调用构造函数创建对象
RemotingConfiguration.RegisterActivatedClientType(typeof(ServiceObj), url);
_serviceObj = new ServiceObj();
break;
case ActivatedType.ServiceSingleCall:
_serviceObj = (ServiceObj)RemotingServices.Connect(typeof(ServiceObj), url);
break;
case ActivatedType.ServiceSingleton:
_serviceObj = (ServiceObj)Activator.GetObject(typeof(ServiceObj), url);
break;
}
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}

private string GetUrlString()
{
string url = string.Empty;
switch (m_activatedType)
{
case ActivatedType.Client:
url = string.Format(urlString, m_protocol, IPAddress.Loopback, 9011,
m_appName);
break;
case ActivatedType.ServiceSingleCall:
url = string.Format(urlString + "/{4}", m_protocol, IPAddress.Loopback, 9011,
m_appName, "ServiceSingleCall");
break;
case ActivatedType.ServiceSingleton:
url = string.Format(urlString + "/{4}", m_protocol, IPAddress.Loopback, 9011,
m_appName, "ServiceSingleton");
break;
}
return url;
}

}

enum ChannelType
{
TcpChannel,
HttpChannel,
IpcChannl,
}

enum ActivatedType
{
Client,
ServiceSingleton,
ServiceSingleCall,
}


class Program
{
static void Main(string[] args)
{
ClientProxy proxy = new ClientProxy("http", "RemotingService", ActivatedType.ServiceSingleton);
ServiceObj obj = proxy.ServiceObj;
if (obj != null)
{
obj.PrintAppDomain();
obj.PrintAppDomain();
obj.ShowCount("张三");
Console.WriteLine(obj.GetSum(10, 12));
Console.WriteLine(obj.GetStrInfo("张三"));
Console.WriteLine("count:{0}", obj.Count);
}
Console.WriteLine();
ClientProxy proxy2 = new ClientProxy("http", "RemotingService", ActivatedType.ServiceSingleton);
ServiceObj obj2 = proxy2.ServiceObj;
if (obj2 != null)
{
obj2.PrintAppDomain();
obj2.PrintAppDomain();
obj2.ShowCount("张三");
Console.WriteLine(obj.GetSum(332, 12));
Console.WriteLine(obj.GetStrInfo("李四"));
Console.WriteLine("count:{0}", obj.Count);
}

Console.ReadKey();
}
}


远程服务对象

public class ServiceObj:MarshalByRefObject,IServiceObj
{
private int count = 0;
public ServiceObj()
{
Console.WriteLine("服务对象的构造函数");
Console.WriteLine();
}

public int Count
{
get { return count; }
}

public void ShowCount(string name)
{
count++;
Console.WriteLine("{0},count的值为{1}",name,Count);
}

public void PrintAppDomain()
{
// AppDomain currentDomain = AppDomain.CurrentDomain;
AppDomain currentDomain = Thread.GetDomain();//获取当前线程所在的应用程序域
Console.WriteLine(currentDomain.FriendlyName);
}

public int GetSum(int a, int b)
{
Console.WriteLine("A+B={0}",a+b);
return a + b;
}

public string GetStrInfo(string arg)
{
Console.WriteLine(arg);
return string.Format("返回一个字符串加参数{0}", arg);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: