我做了一个简单的remoting学习程序,
Server把类注册为SingleCall,理论上应该是客户端调用服务器方法自动创建一个新的对象。
客户端用Activator.GetObject()方法获取服务器对象。开始每次启动客户端都可以调用服务器方法,后来发现间隔很长时间才启动客户端,服务器没有响应,怎么回事?
Server把类注册为SingleCall,理论上应该是客户端调用服务器方法自动创建一个新的对象。
客户端用Activator.GetObject()方法获取服务器对象。开始每次启动客户端都可以调用服务器方法,后来发现间隔很长时间才启动客户端,服务器没有响应,怎么回事?
'可能是服务器把方法注销了'
如果是这样,服务器岂不是要经常启动?
应该有别的原因吧。
再说,逃避不是解决问题的办法。remoting的高手,快来指点一下。
你要重写一个这个方法,把服务器对象的声明周期设置为无限大就可以了
public override object InitializeLifetimeService()
{
return 0;
}
不知道我理解的对不对,请高手指教!
问题是服务器不关闭的情况下,客户端隔了很久又重新
启动一次,这时候发现服务器没有响应了。(也就是对象没有创建)
/* *******************************************
* Filename: MMSInterface.cs
* Abstract: definition of the MMS-interfaces
* Version: 1.0.0
* Author: zdleek
* Date: 2005.6-2005.9
* ---------------------------
* Modification History:
* # [Date] [Editor] [Modification description]
* 1.--
*
* *******************************************/using System;
using System.Data;
namespace MMSInterface
{
/// IMMSAuthority:验证接口,身份和权限验证
public interface IMMSAuthenticator
{
//1).Function:string Login(string user,string pwd, out string token)
//Abstract:验证用户身份
//Parameters:user-用户名, pwd-口令,modid-发起验证请求的模块id,
// token-返回给用户的令牌,是一个唯一的随机数
//Return:成功返回000000,错误返回xxxxxx;
string Login(string user,string pwd, out string token);
}}------------------------------------------------------------------
建一个项目(Windows Application Server),
//主文件的OnStart()函数初始化TcpChannel
private void InitTcpChannelSrv()
{
m_IniInfo = new IniInfo();
ProcessIniInfo(0);//0:read from ini file
try
{
// 注册TcpChannel服务
TcpChannel chan = new TcpChannel(m_IniInfo.m_Port);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MMSServiceClass), "MMSService", WellKnownObjectMode.Singleton);
WriteSysLog("OnStart() : 创建TCP CHANNEL成功, 端口=" + m_IniInfo.m_Port);
}
catch(Exception e)
{
string strErr = "创建TCP CHANNEL失败; " + e.Message;// + "\r\n";
WriteSysLog("OnStart() : " + strErr); }
}//接口实现
//引用上面的dll
/* *******************************************
* Filename: MMSServiceClass.cs
* Abstract: implemention of the MMS-interfaces
* Version: 1.0.0
* Author: zdleek
* Date: 2005.6-2005.9
* ---------------------------
* Modification History:
* # [Date] [Editor] [Modification description]
* 1.--
*
* *******************************************/using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Constants;
using HelperLib;namespace MMSInterface
{
public class MMSServiceClass:MarshalByRefObject,IMMSAuthenticator
{
//...... // *** Implemention of MMSInterface.IMMSAuthenticator
//1).Function:string Login(string user,string pwd, out string token)
//Abstract:验证用户身份
//Parameters:user-用户名, pwd-口令,
// token-返回给用户的令牌,是一个唯一的随机数
//Return:成功返回0,错误返回xxxxxx;
string IMMSAuthenticator.Login(string user,string pwd,out string token)
{
return UserLogin(user, pwd,out token);
} //..... }//end of class
}//end of NameSpace---------------------------------------------
//建一个Windows Application应用程序项目,做客户端
//要引用上面的DLL
//
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using MMSInterface;//....
private IMMSAuthenticator m_RemAuthen; //验证接口
m_RemAuthen = (IMMSAuthenticator)
Activator.GetObject(typeof(IMMSAuthenticator),
"tcp://localhost:12366/MMSService");
string token;
string res;
res = m_RemAuthen.Login("user","pwd",out token);
//....