Server hangs on a client callback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dvestal@gmail.com

    Server hangs on a client callback

    I'm new to .NET remoting, and I'm having trouble getting a callback to
    work from a server. I've been through a dozen webpages and the
    references available to me, but I'm still missing something.

    I'm trying to have a client pass a callback function to a server, and
    the server call it. The callback signature is determined by public
    interfaces referenced by both client and server. I've worked through
    about three problems on my own, but now I'm stuck.

    A supersimplified example of my code is below. What happens is that
    the client can subscribe, but when the server attempts to call the
    HandleMessage method on the client, the method is never invoked. The
    server appears to hang just before invoking the method. What am I
    doing wrong?

    SHARED LIBRARY
    public interface ISubscriber {
    void HandleMessage() ;
    }
    public interface IMessager {
    Subscribe(ISubs criber subscriber);
    }

    SERVER
    public class MessagerImp : IMessager
    {
    ISubscriber m_subscriber;

    public MessagerImp()
    {
    m_subscriber = null;

    BinaryServerFor matterSinkProvi der provider
    = new BinaryServerFor matterSinkProvi der();
    provider.TypeFi lterLevel = TypeFilterLevel .Full;
    TcpServerChanne l channel = new TcpServerChanne l
    (
    "TCP server channel with a binary formatter",
    MessageClient.M essagerPort,
    provider
    );
    ChannelServices .RegisterChanne l(channel, true);

    RemotingConfigu ration.Register WellKnownServic eType
    (
    typeof(Messager Imp),
    "tcp://MYMACHINE:1545/Messager.rpc",
    WellKnownObject Mode.SingleCall
    );

    m_worker = new Thread(new ThreadStart(Wor kerThreadMethod ));
    m_worker.Start( );
    }

    public void Subscribe(ISubs criber subscriber) {
    m_subscriber = subscriber;
    }

    public void WorkerThreadMet hod()
    {
    while(true)
    {
    // This method hangs if it tries to invoke HandleMessage.
    if(m_subscriber != null) m_subscriber.Ha ndleMessage();
    }
    }
    }

    CLIENT
    public class MessagerClient : ISubscriber
    {
    public MessagerClient( )
    {
    ChannelServices .RegisterChanne l(new TcpChannel(0), true);
    IMessager rpcSvc = (IMessager)
    Activator.GetOb ject(typeof(IMe ssager),
    "tcp://MYMACHINE:1545/Messager.rpc");
    rpcSvc.Subscrib e(this);
    }

    public void HandleMessage()
    {
    // Method is never invoked.
    }
    }

  • Doug Forster

    #2
    Re: Server hangs on a client callback

    Well your MessagerClient class needs to be derived from MarshalByRefObj ect
    to start with. Then you need to do some study of lifetime issues to deal
    with your next set of problems.

    Cheers
    Doug Forster

    <dvestal@gmail. comwrote in message
    news:1159877408 .801774.288140@ i42g2000cwa.goo glegroups.com.. .
    I'm new to .NET remoting, and I'm having trouble getting a callback to
    work from a server. I've been through a dozen webpages and the
    references available to me, but I'm still missing something.
    >
    I'm trying to have a client pass a callback function to a server, and
    the server call it. The callback signature is determined by public
    interfaces referenced by both client and server. I've worked through
    about three problems on my own, but now I'm stuck.
    >
    A supersimplified example of my code is below. What happens is that
    the client can subscribe, but when the server attempts to call the
    HandleMessage method on the client, the method is never invoked. The
    server appears to hang just before invoking the method. What am I
    doing wrong?
    >
    SHARED LIBRARY
    public interface ISubscriber {
    void HandleMessage() ;
    }
    public interface IMessager {
    Subscribe(ISubs criber subscriber);
    }
    >
    SERVER
    public class MessagerImp : IMessager
    {
    ISubscriber m_subscriber;
    >
    public MessagerImp()
    {
    m_subscriber = null;
    >
    BinaryServerFor matterSinkProvi der provider
    = new BinaryServerFor matterSinkProvi der();
    provider.TypeFi lterLevel = TypeFilterLevel .Full;
    TcpServerChanne l channel = new TcpServerChanne l
    (
    "TCP server channel with a binary formatter",
    MessageClient.M essagerPort,
    provider
    );
    ChannelServices .RegisterChanne l(channel, true);
    >
    RemotingConfigu ration.Register WellKnownServic eType
    (
    typeof(Messager Imp),
    "tcp://MYMACHINE:1545/Messager.rpc",
    WellKnownObject Mode.SingleCall
    );
    >
    m_worker = new Thread(new ThreadStart(Wor kerThreadMethod ));
    m_worker.Start( );
    }
    >
    public void Subscribe(ISubs criber subscriber) {
    m_subscriber = subscriber;
    }
    >
    public void WorkerThreadMet hod()
    {
    while(true)
    {
    // This method hangs if it tries to invoke HandleMessage.
    if(m_subscriber != null) m_subscriber.Ha ndleMessage();
    }
    }
    }
    >
    CLIENT
    public class MessagerClient : ISubscriber
    {
    public MessagerClient( )
    {
    ChannelServices .RegisterChanne l(new TcpChannel(0), true);
    IMessager rpcSvc = (IMessager)
    Activator.GetOb ject(typeof(IMe ssager),
    "tcp://MYMACHINE:1545/Messager.rpc");
    rpcSvc.Subscrib e(this);
    }
    >
    public void HandleMessage()
    {
    // Method is never invoked.
    }
    }
    >

    Comment

    • dvestal@gmail.com

      #3
      Re: Server hangs on a client callback


      Doug Forster wrote:
      Well your MessagerClient class needs to be derived from MarshalByRefObj ect
      to start with. Then you need to do some study of lifetime issues to deal
      with your next set of problems.
      >
      Cheers
      Doug Forster
      Thanks, Doug.

      The client now derives from MarshalByRefObj ect, and the server is now
      exposed as a Singleton that overrides InitializeLifet imeService to
      return null.

      The server still hangs when it tries to call the callback. Would this
      behavior occur if the client weren't listening on the appropriate port?
      To receive callbacks, does the client need to expose itself as a
      server, complete with RegisterWellKno wnServiceType and all that?

      Also, a related question. My server is inside a Windows service, and
      it needs a thread running inside it that processes things. I'm having
      trouble deciding whether the remoted object (MessagerImp) should just
      pass things to a non-remoted, stateful object to do this work, or
      whether there's a good way of keeping state and running threads inside
      a remoted class. Any tips?

      Comment

      Working...