Skip to content

Events and Callbacks

Hagen Siegel edited this page Apr 25, 2021 · 7 revisions

CoreRemoting has built-in support for events and callbacks.

Remote Callbacks

A client can pass delegates as parameters when invoking a method of a remote service via a proxy. If the service invokes the delegate, it is called on the remote client. Just as when calling a local delegate, the executing thread waits until the delegate execution has been completed and the result to be returned.

The lifetime of client delegates, that are passed to remote services, is bound to the ServiceProxy object that is used on client side to access the remote service. Client delegates are no longer callable from remote services, after the proxy has be disposed.

The following shows how a remote callback can be used to notify the client on progress of a long running service method. Imagine your are developing a file archiving application and want to be notified about progress when archiving multiple files.

    // Client
    Dictionary<string, byte[]> filesToArchive = ... // Create a dictionary of filenames and the file contents as byte arrays

    var proxy = _client.CreateProxy<IFileArchiveService>();

    proxy.ArchiveFiles(filesToArchive, (processed, total) => 
        Console.WriteLine("{0} of {1} files processed", processed, total));

    // Server
    public class FileArchiveService : IFileArchiveService
    {
        public void ArchiveFiles(Dictionary<string, byte[]> filesToArchive, Action<int, int> progressCallback)
        {
            int total = filesToArchive.Count;
            int processed = 0;

            foreach(var fileToArchive in filesToArchive)
            {
                ArchiveFile(fileName: fileToArchive.Key, content: fileToArchive.Value);
                processed++;

                progressCallback?.Invoke(processed, total);
            }
        }
    }

Remote Events

Clone this wiki locally