/// <summary> /// Публикация события через сервис синхронизации /// </summary> /// <param name="appSessionId">Id приложения инициировшего событие</param> /// <param name="publishEvent"></param> private void PublishEventThroughService(Guid appSessionId, Action <AppSession> publishEvent) { //целевые приложения (приложения без того, которое и послало событие). var targetAppSessions = _appSessions .Where(appSession => appSession.AppSessionId != appSessionId) .ToList(); foreach (var appSession in targetAppSessions) { try { publishEvent.Invoke(appSession); } //отключаем приложение от сервиса catch (CommunicationObjectAbortedException e) { PrintMessageEvent?.Invoke($"{this.GetType().FullName}. {e.GetType().FullName}."); this.Disconnect(appSession.AppSessionId); } catch (TimeoutException e) { PrintMessageEvent?.Invoke($"{this.GetType().FullName}. {e.GetType().FullName}."); this.Disconnect(appSession.AppSessionId); } catch (Exception e) { PrintMessageEvent?.Invoke($"!Exception on Invoke {publishEvent.GetMethodInfo().Name} ({this.GetType().FullName}) by appSession {appSessionId}. \n{e.GetType().FullName}\n{e.PrintAllExceptions()}"); this.Disconnect(appSession.AppSessionId); } } PrintMessageEvent?.Invoke($"Invoke {publishEvent.GetMethodInfo().Name} by appSession {appSessionId}"); }
public void PrintMessage(string message) { if (PrintMessageEvent != null) { Console.WriteLine("Message:"); PrintMessageEvent.Invoke(message); Console.WriteLine("~~~~~~~~~~~~~~~~~~"); } }
/// <summary> /// Отключение от сервиса /// </summary> /// <param name="appSessionId">Id сессии приложения</param> public void Disconnect(Guid appSessionId) { var appSession = _appSessions.SingleOrDefault(session => session.AppSessionId == appSessionId); if (appSession != null) { _appSessions.Remove(appSession); PrintMessageEvent?.Invoke($"Disconnected (appSessionId: {appSessionId} userId: {appSession.UserId})"); } }
/// <summary> /// Публикация события через сервис синхронизации для целевых /// </summary> /// <param name="targetUserId">Id целевого пользователя</param> /// <param name="sourceEventAppSessionId">Id приложения инициировшего событие</param> /// <param name="publishEvent"></param> /// <returns>Доставлено ли уведомление целевому пользователю</returns> private bool PublishEventByServiceForUser(Guid targetUserId, Guid sourceEventAppSessionId, Func <AppSession, bool> publishEvent) { bool result = false; //целевые приложения (без того, которое и послало событие). var targetAppSessions = _appSessions .Where(appSession => appSession.UserId == targetUserId) .Where(appSession => appSession.AppSessionId != sourceEventAppSessionId) .ToList(); PrintMessageEvent?.Invoke("-------------------"); PrintMessageEvent?.Invoke($"Invoke {publishEvent.GetMethodInfo().Name} (sourceEventAppSessionId: {sourceEventAppSessionId} targetUserId: {targetUserId}"); if (targetAppSessions.Any() == false) { PrintMessageEvent?.Invoke(" - Service have no target connected user"); } else { foreach (var appSession in targetAppSessions) { try { if (publishEvent.Invoke(appSession)) { result = true; PrintMessageEvent?.Invoke($" + Success (appId: {appSession.AppSessionId})"); } } //отключаем приложение от сервиса catch (CommunicationObjectAbortedException e) { PrintMessageEvent?.Invoke($" - Faulted {e.GetType().FullName} (appId: {appSession.AppSessionId})"); PrintMessageEvent?.Invoke($"{this.GetType().FullName}. {e.GetType().FullName}."); this.Disconnect(appSession.AppSessionId); } catch (TimeoutException e) { PrintMessageEvent?.Invoke($" - Faulted {e.GetType().FullName} (appId: {appSession.AppSessionId})"); PrintMessageEvent?.Invoke($"{this.GetType().FullName}. {e.GetType().FullName}."); this.Disconnect(appSession.AppSessionId); } catch (Exception e) { PrintMessageEvent?.Invoke($" - Faulted {e.GetType().FullName} (appId: {appSession.AppSessionId})"); PrintMessageEvent?.Invoke($"!Exception on Invoke {publishEvent.GetMethodInfo().Name} ({this.GetType().FullName}) by appSession {sourceEventAppSessionId}. \n{e.GetType().FullName}\n{e.PrintAllExceptions()}"); this.Disconnect(appSession.AppSessionId); } } } PrintMessageEvent?.Invoke("-------------------"); return(result); }
/// <summary> /// Подключение к сервису /// </summary> /// <param name="appSessionId">Id сессии приложения</param> /// <param name="userId">Id юзера</param> /// <returns></returns> public bool Connect(Guid appSessionId, Guid userId) { //если приложение уже подключено к сервису if (_appSessions.Select(appSession => appSession.AppSessionId).Contains(appSessionId)) { return(false); } //подключаем новое приложение к сервису _appSessions.Add(new AppSession(appSessionId, userId, OperationContext.Current)); PrintMessageEvent?.Invoke($"Connected (appSessionId: {appSessionId} userId: {userId})."); return(true); }
/// <summary> /// Закрытие хоста /// </summary> public void Close() { foreach (var appSession in _appSessions.ToList()) { try { appSession.OperationContext.GetCallbackChannel <IEventServiceCallback>().OnServiceDisposeEvent(); PrintMessageEvent?.Invoke($"Succsess on {this.GetType().FullName}.Close() {appSession}."); this.Disconnect(appSession.AppSessionId); } catch (Exception e) { PrintMessageEvent?.Invoke($"Exception on {this.GetType().FullName}.Close() appSession={appSession}. {e.GetType().FullName} \n {e.PrintAllExceptions()}"); } } }