private static IObservable<ServiceState> CallOperationOnService(ServiceBase serviceBase, Operation operation) { ReplaySubject<ServiceState> result = new ReplaySubject<ServiceState>(); result.OnNext(operation.InitialState); var methodObs = Observable.Start(() => { Type serviceBaseType = serviceBase.GetType(); object[] parameters = null; if (operation == Operations.Start) { parameters = new object[] { null }; } try { serviceBaseType.InvokeMember(operation.MethodCall, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, serviceBase, parameters); } catch (Exception ex) { throw new Exception(string.Format("An exception was thrown while trying to call the {0} of the {1} service. Examine the inner exception for more information.", operation, serviceBase.ServiceName), ex.InnerException); } }); methodObs.Subscribe ( _ => { result.OnNext(operation.FinishedState); result.OnCompleted(); }, ex => { result.OnNext(operation.ErrorState); result.OnError(ex); } ); return result; }