mafu
mafu

Reputation: 32680

.NET Remoting callback

Is it possible to transmit a callback via remoting? I'd like to do something along the lines of myRemoteObject.PerformStuff( x => Console.WriteLine(x) );

If not, how would I implement a equivalent functionality?

Edit: I'm aware Remoting got superseded by WCF, but I'm still interested in this specific case. (Out of curiosity though, how's the issue in WCF?)

Upvotes: 4

Views: 3122

Answers (3)

Vlad P.
Vlad P.

Reputation: 99

Important to add that you need to register duplex channels, not just a server or a client. Otherwise the callback will not work.

E.g. TcpChannel/HttpChannel/IpcChannel and NOT TcpServerChannel/TcpClientChannel and so on.

For firewalled solutions make sure that the callback port is opened on the 'client' for both TCP and HTTP channels (IPC does not use ports).

Upvotes: 5

Andrew Shepherd
Andrew Shepherd

Reputation: 45252

Remoting provides support for events. The client code can simply subscribe to an event that's exposed by the server class. (There are a large list of caveats here).

Also, you can pass a callback interface to a remote class, providing your implementation of the interface is a MarshalByRef object.

interface IMyCallback
{
      void OnSomethingHappened(string information);
}


class MyCallbackImplementation : MarshalByRefObject, IMyCallback
{
    public void OnSomethingHappened(string information)
    {
        // ...
    }

}



class MyServerImplementation : MarshalByRefObject
{
     List<IMyCallback> _callbacks = new List<IMyCallback>();

     public void RegisterCallback(IMyCallback _callback)
     {
           _callbacks.Add(_callback);
     }

     public void InvokeCalbacks(string information)
     {
         foreach(IMyCallback callback in _callbacks)
         {
              _callback.OnSomethingHappened(information);
         }
     }

}

Upvotes: 3

RichardOD
RichardOD

Reputation: 29157

In WCF you use callback operations. If you can use WCF or remoting, choose WCF. Remoting is classed as legacy.

Upvotes: -1

Related Questions