/// <summary> /// This is the main entry point for your service replica. /// This method executes when this replica of your service becomes primary and has write status. /// </summary> /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param> protected override async Task RunAsync(CancellationToken cancellationToken) { await StateManager.GetOrAddAsync <IReliableDictionary <string, string> >("myConnections").ConfigureAwait(false); MyHosts = await StateManager.GetOrAddAsync <IReliableDictionary <string, string> >("myHosts").ConfigureAwait(false); MyHosts.DictionaryChanged += MyHosts_DictionaryChanged; //load existing host clients and connect using (var tx = StateManager.CreateTransaction()) { var e = await MyHosts.CreateEnumerableAsync(tx, EnumerationMode.Unordered); using (var hosts = e.GetAsyncEnumerator()) { while (await hosts.MoveNextAsync(cancellationToken)) { var host = new SignalRClient(hosts.Current.Key, hosts.Current.Value); await host.ConnectAsync().ConfigureAwait(false); MyClients.Add(hosts.Current.Key, host); ServiceEventSource.Current.Message($"Registered and connected to host {host.HostId}"); } } } }
private void Save_Click(object sender, RoutedEventArgs e) { // Validate column count. if (int.TryParse(MyColumnCount.Text, out ColumnCount) == false || ColumnCount < 1 || ColumnCount > 10) { var errorWindow = DialogWindow.ErrorWindow("Please enter a valid number of columns (between 1 and 10)."); errorWindow.Owner = this; errorWindow.ShowDialog(); MyColumnCount.Focus(); MyColumnCount.SelectAll(); return; } // Validate favorite name. if (Favorite.IsTitleInvalid(MyTitle.Text)) { var errorWindow = DialogWindow.ErrorWindow(Strings.NewFavorite_Error_InvalidName); errorWindow.Owner = this; errorWindow.ShowDialog(); MyTitle.Focus(); MyTitle.SelectAll(); return; } // Split MyHosts string to array, trim each item, then convert to list. Ensure at least one host was entered. HostList = MyHosts.Text.Trim().Split(new char[] { ',', '\n' }).Select(host => host.Trim()).ToList(); if (HostList.All(x => string.IsNullOrWhiteSpace(x))) { var errorWindow = DialogWindow.ErrorWindow("You have not entered any hosts. Provide at least one host for this favorite set."); errorWindow.Owner = this; errorWindow.ShowDialog(); MyHosts.Focus(); MyHosts.SelectAll(); return; } // If creating a new favorite: Check and display warning if title already exists. // If editing a favorite and title has changed: Check and display warning if title already exists. // If editing a favorite and title has not changed: Proceed with save. No warning displayed. if ((!IsExisting && Favorite.TitleExists(MyTitle.Text)) || (IsExisting && !string.Equals(OriginalTitle, MyTitle.Text) && Favorite.TitleExists(MyTitle.Text))) { var warningWindow = DialogWindow.WarningWindow( message: $"{MyTitle.Text} {Strings.NewFavorite_Warn_AlreadyExists}", confirmButtonText: Strings.DialogButton_Overwrite); warningWindow.Owner = this; if (warningWindow.ShowDialog() == true) { // User opted to overwrite existing favorite entry. SaveFavorite(); } } else { // Checks passed. Saving. SaveFavorite(); } }
public async Task UnregisterSignalRHost(string hostName) { using (var tx = StateManager.CreateTransaction()) { await MyHosts.TryRemoveAsync(tx, hostName).ConfigureAwait(false); await tx.CommitAsync().ConfigureAwait(false); } }
public async Task RegisterSignalRHost(string hostId, string endpoint) { using (var tx = StateManager.CreateTransaction()) { if (MyHosts == null) { MyHosts = await StateManager.GetOrAddAsync <IReliableDictionary <string, string> >("myHosts").ConfigureAwait(false); } await MyHosts.AddOrUpdateAsync(tx, hostId, endpoint, (k, v) => endpoint).ConfigureAwait(false); await tx.CommitAsync().ConfigureAwait(false); if (MyClients.ContainsKey(hostId)) { MyClients.Remove(hostId); } var client = new SignalRClient(hostId, endpoint); await client.ConnectAsync().ConfigureAwait(false); MyClients.Add(hostId, client); } }