我做了一个简单的remoting学习程序,
Server把类注册为SingleCall,理论上应该是客户端调用服务器方法自动创建一个新的对象。
客户端用Activator.GetObject()方法获取服务器对象。开始每次启动客户端都可以调用服务器方法,后来发现间隔很长时间才启动客户端,服务器没有响应,怎么回事?

解决方案 »

  1.   

    songhtao(三十年孤独):
    '可能是服务器把方法注销了'
    如果是这样,服务器岂不是要经常启动?
    应该有别的原因吧。
      

  2.   

    在调用GetObject之前调用一次System.Configuration.ConfigurationSettings.GetConfig("YourConfigSection")获得一下配置文件试一试,可能会有用。
      

  3.   

    建議用WEB SERVIECS ,這樣可以解決
      

  4.   

    Web service用在局域网不是很合适吧?
    再说,逃避不是解决问题的办法。remoting的高手,快来指点一下。
      

  5.   

    服务端的对象是否继承自System.MarshalByRefObject??
    你要重写一个这个方法,把服务器对象的声明周期设置为无限大就可以了
    public override object InitializeLifetimeService()
    {
    return 0;
    }
      

  6.   

    一般继承自MarshalByRefObject的对象有一个默认的生命周期,过了生命周期就自动销毁了
      

  7.   

    的确是继承自MarshalByRefObject,但服务器配置为SingleCall知名对象,应该是每次自动创建,自动销毁,生命周期是针对Singleton起作用吧。
    不知道我理解的对不对,请高手指教!
      

  8.   

    可能各位有误解。客户端多次启动,当然应该多次创建Singlecall对象。
    问题是服务器不关闭的情况下,客户端隔了很久又重新
    启动一次,这时候发现服务器没有响应了。(也就是对象没有创建)
      

  9.   

    下面是我做过的一个项目的大概流程建一个MMSInterface.dll,定义接如下
    /* *******************************************
     * 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);


    //....