昨天的那篇隨筆﹐全部是使用配置文件來實現的。那么根據配置文件﹐如何生成代碼訪問模式?
1﹑首先將RemoteServer 和RemoteClient中的App.Config文件移除專案﹐注意是移除﹐不是刪除﹐要不﹐你以后要想再使用配置文件話﹐直接包含進來就可以啦。
2﹑在frmServer的建構改為﹕
1﹑首先將RemoteServer 和RemoteClient中的App.Config文件移除專案﹐注意是移除﹐不是刪除﹐要不﹐你以后要想再使用配置文件話﹐直接包含進來就可以啦。
2﹑在frmServer的建構改為﹕
public frmServer()
{
InitializeComponent();
//使用配置文件
//RemotingConfiguration.Configure("RemoteServer.exe.config", false);
//使用代碼
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemoteObject.RemoteObject),
"RemoteObject.RemoteObject",
WellKnownObjectMode.Singleton);
IChannel icTcp = new TcpChannel(8080);
ChannelServices.RegisterChannel(icTcp, false);
}
3﹑在frmClient中的Load事件的方法改為﹕{
InitializeComponent();
//使用配置文件
//RemotingConfiguration.Configure("RemoteServer.exe.config", false);
//使用代碼
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemoteObject.RemoteObject),
"RemoteObject.RemoteObject",
WellKnownObjectMode.Singleton);
IChannel icTcp = new TcpChannel(8080);
ChannelServices.RegisterChannel(icTcp, false);
}
private void frmClient_Load(object sender, EventArgs e)
{
//Listen the Client request,config the client infomation
//RemotingConfiguration.Configure("RemoteClient.exe.config", false);
RemoteObject.RemoteObject obj = (RemoteObject.RemoteObject)Activator.GetObject(typeof(RemoteObject.RemoteObject),
"tcp://localhost:8080/RemoteObject");
}
余皆不變前一篇﹐運行測試就OK了。{
//Listen the Client request,config the client infomation
//RemotingConfiguration.Configure("RemoteClient.exe.config", false);
RemoteObject.RemoteObject obj = (RemoteObject.RemoteObject)Activator.GetObject(typeof(RemoteObject.RemoteObject),
"tcp://localhost:8080/RemoteObject");
}