扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
在本页阅读全文(共2页)
编程式信道注册-客户端激活
远程对象
public class MyClass:MarshalByRefObject
{
public MyClass()
{
string con = "时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "远程对象构造函数被调用。";
File.AppendAllText(@"F:/a.txt", con + "\r\n");
}
public void WriteFile(string content)
{
string con = "时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 内容:" + content;
File.AppendAllText(@"F:/a.txt", con+"\r\n");
}
}
宿主
using RemoteServer;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting;
private void ServerHostDialog_Load(object sender, EventArgs e)
{
IChannel tcpChannel = new TcpChannel(12345);
ChannelServices.RegisterChannel(tcpChannel,false);
IChannel httpChannel = new HttpChannel(45678);
ChannelServices.RegisterChannel(httpChannel,false );
IChannel ipcChannel = new IpcChannel("MyIPC");
ChannelServices.RegisterChannel(ipcChannel,true );
}
private void button1_Click(object sender, EventArgs e)
{
Type serverType = typeof(MyClass);
RemotingConfiguration.RegisterActivatedServiceType(serverType);
}
客户端
using RemoteServer;
using System.Runtime.Remoting;
private void button1_Click(object sender, EventArgs e)
{
MyClass obj= new MyClass();
obj.WriteFile("名柄为"+this.Handle .ToString ());
}
Type serverType = typeof(MyClass);
private void Form1_Load(object sender, EventArgs e)
{
RemotingConfiguration.RegisterActivatedClientType(serverType, “tcp://localhost:12345”);//第二个参数可换成: http://localhost:45678或ipc://MyIPC
}
编程式信道注册-Single Call激活
远程对象不变
宿主
using RemoteServer;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting;
private void ServerHostDialog_Load(object sender, EventArgs e)
{
IChannel tcpChannel = new TcpChannel(12345);
ChannelServices.RegisterChannel(tcpChannel,false);
IChannel httpChannel = new HttpChannel(45678);
ChannelServices.RegisterChannel(httpChannel,false );
IChannel ipcChannel = new IpcChannel("MyIPC");
ChannelServices.RegisterChannel(ipcChannel,true );
}
private void button1_Click(object sender, EventArgs e)
{
Type serverType = typeof(MyClass); RemotingConfiguration.RegisterWellKnownServiceType(serverType,“RemoteServer”, WellKnownObjectMode.SingleCall);//RemoteServer为远程对象的名称空间
}
客户端
using RemoteServer;
using System.Runtime.Remoting;
private void button1_Click(object sender, EventArgs e)
{
MyClass obj= new MyClass();
obj.WriteFile("名柄为"+this.Handle .ToString ());
}
Type serverType = typeof(MyClass);
private void Form1_Load(object sender, EventArgs e)
{
RemotingConfiguration.RegisterWellKnownClientType(serverType, "tcp://localhost:12345/RemoteServer");
//第二个参数可换成: http://localhost:45678/RemoteServer或ipc://MyIPC/RemoteServer
}
编程式信道注册-Singleton激活
远程对象不变
宿主
using RemoteServer;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting;
private void ServerHostDialog_Load(object sender, EventArgs e)
{
IChannel tcpChannel = new TcpChannel(12345);
ChannelServices.RegisterChannel(tcpChannel,false);
IChannel httpChannel = new HttpChannel(45678);
ChannelServices.RegisterChannel(httpChannel,false );
IChannel ipcChannel = new IpcChannel("MyIPC");
ChannelServices.RegisterChannel(ipcChannel,true );
}
private void button1_Click(object sender, EventArgs e)
{
Type serverType = typeof(MyClass); RemotingConfiguration.RegisterWellKnownServiceType(serverType,“RemoteServer”, WellKnownObjectMode. Singleton);//RemoteServer为远程对象的名称空间
}
客户端
using RemoteServer;
using System.Runtime.Remoting;
private void button1_Click(object sender, EventArgs e)
{
MyClass obj= new MyClass();
obj.WriteFile("名柄为"+this.Handle .ToString ());
}
Type serverType = typeof(MyClass);
private void Form1_Load(object sender, EventArgs e)
{
RemotingConfiguration.RegisterWellKnownClientType(serverType, "tcp://localhost:12345/RemoteServer");
//第二个参数可换成: http://localhost:45678/RemoteServer或ipc://MyIPC/RemoteServer
}
管理式配置
依照上面的例子,远程对象不变,只用在宿主端和客户端的项目中添加“应用程序配置文件”(App.config)文件。
配置文件保存后的名称为:应用程序名.exe.config
比如宿主的配置文件名为RemoteServerHost.exe.config
客户商配置文件名为Client.exe.config
在宿主中和客户端,读取配置文件
在Main函数中
using System.Runtime.Remoting;
[STAThread]
static void Main()
{
RemotingConfiguration.Configure(AppDomain.CurrentDomain.FriendlyName + ".config",false);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
远程对象依然不变
宿主端配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting >
<application>
<service>
<!--<activated type="RemoteServer.MyClass,RemoteServer"></activated>-->
<!--<wellknown type="RemoteServer.MyClass,RemoteServer" mode="Singleton" objectUri="RemoteServer"></wellknown>-->
<wellknown type="RemoteServer.MyClass,RemoteServer" mode="SingleCall" objectUri="RemoteServer"></wellknown>
</service>
<channels>
<channel ref="tcp" port="8005"></channel>
<channel ref="http" port="8006"></channel>
<channel ref="ipc" portName="MyIPC"></channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>
客户端配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting >
<application>
<!--<client url="tcp://localhost:8005">-->
<!--<activated type="RemoteSrerver.MyClass,ServerAssembly"></activated>-->
<!--</client>-->
<client>
<wellknown type="RemoteServer.MyClass,RemoteServer" url="tcp://localhost:8005/RemoteServer"></wellknown>
</client>
</application>
</system.runtime.remoting>
</configuration>
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者