void IRequestConfigurator <TRequest> .Watch <T>(MessageHandler <T> handler, Action <IHandlerConfigurator <T> > configure) { if (_connections.ContainsKey(typeof(T))) { throw new RequestException($"Only one handler of type {TypeMetadataCache<T>.ShortName} can be registered"); } var configurator = new RequestHandlerConfigurator <T>(handler); configure?.Invoke(configurator); var connectHandle = configurator.Connect(_connector, _requestId); _connections.Add(typeof(T), new RequestHandlerHandle <T>(connectHandle)); }
Task <T> IRequestConfigurator <TRequest> .Handle <T>(MessageHandler <T> handler, Action <IHandlerConfigurator <T> > configure) { if (_connections.ContainsKey(typeof(T))) { throw new RequestException($"Only one handler of type {TypeMetadataCache<T>.ShortName} can be registered"); } var source = new TaskCompletionSource <T>(); MessageHandler <T> messageHandler = async context => { try { await Task.Factory.StartNew(() => handler(context), context.CancellationToken, TaskCreationOptions.None, _taskScheduler) .Unwrap() .ConfigureAwait(false); source.TrySetResult(context.Message); Complete(); } catch (Exception ex) { source.TrySetException(ex); Fail(ex); } }; var configurator = new RequestHandlerConfigurator <T>(messageHandler); configure?.Invoke(configurator); var connectHandle = configurator.Connect(_connector, _requestId); _connections.Add(typeof(T), new RequestHandlerHandle <T>(connectHandle, source, _context.CancellationToken)); return(source.Task); }
Task <T> IRequestConfigurator <TRequest> .Handle <T>(Action <IHandlerConfigurator <T> > configure) { if (_connections.ContainsKey(typeof(T))) { throw new RequestException($"Only one handler of type {TypeMetadataCache<T>.ShortName} can be registered"); } var source = new TaskCompletionSource <T>(); MessageHandler <T> messageHandler = context => { try { source.TrySetResult(context.Message); Complete(); } catch (Exception ex) { source.TrySetException(ex); Fail(ex); } return(TaskUtil.Completed); }; var configurator = new RequestHandlerConfigurator <T>(messageHandler); configure?.Invoke(configurator); var connectHandle = configurator.Connect(_connector, _requestId); _connections.Add(typeof(T), new RequestHandlerHandle <T>(connectHandle, source, _context.CancellationToken)); return(source.Task); }