public static void SetRegistrations(IEnumerable <RegistrationDescription> collection)
 {
     Registrations.Clear();
     if (collection != null)
     {
         foreach (var registration in collection)
         {
             var registrationInfo = new RegistrationInfo
             {
                 Registration   = registration,
                 ETag           = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Tags           = registration.Tags != null && registration.Tags.Any() ?
                                  registration.Tags.Aggregate((a, t) => a + "," + t) :
                                  null
             };
             if (registration is WindowsRegistrationDescription)
             {
                 registrationInfo.ChannelUri = ((WindowsRegistrationDescription)registration).ChannelUri.ToString();
             }
             if (registration is MpnsRegistrationDescription)
             {
                 registrationInfo.ChannelUri = ((MpnsRegistrationDescription)registration).ChannelUri.ToString();
             }
             if (registration is AppleRegistrationDescription)
             {
                 registrationInfo.ChannelUri = ((AppleRegistrationDescription)registration).DeviceToken;
             }
             if (registration is GcmRegistrationDescription)
             {
                 registrationInfo.ChannelUri = ((GcmRegistrationDescription)registration).GcmRegistrationId;
             }
             Registrations.Add(registrationInfo);
         }
     }
 }
 public static void SetRegistrations(IEnumerable<RegistrationDescription> collection)
 {
     Registrations.Clear();
     if (collection != null)
     {
         foreach (var registration in collection)
         {
             var registrationInfo = new RegistrationInfo
                 {
                     Registration = registration,
                     ETag = registration.ETag,
                     ExpirationTime = registration.ExpirationTime,
                     RegistrationId = registration.RegistrationId,
                     Tags = registration.Tags != null && registration.Tags.Any() ? 
                            registration.Tags.Aggregate((a, t) => a + "," + t) : 
                            null
                 };
             if (registration is WindowsRegistrationDescription)
             {
                 registrationInfo.ChannelUri = ((WindowsRegistrationDescription)registration).ChannelUri.ToString();
             }
             if (registration is MpnsRegistrationDescription)
             {
                 registrationInfo.ChannelUri = ((MpnsRegistrationDescription)registration).ChannelUri.ToString();
             }
             if (registration is AppleRegistrationDescription)
             {
                 registrationInfo.ChannelUri = ((AppleRegistrationDescription)registration).DeviceToken;
             }
             if (registration is GcmRegistrationDescription)
             {
                 registrationInfo.ChannelUri = ((GcmRegistrationDescription)registration).GcmRegistrationId;
             }
             Registrations.Add(registrationInfo);
         }
     }
 }
 private async void btnCreateRegistration_Click(object sender, EventArgs e)
 {
     using (var form = new RegistrationForm())
     {
         if (form.ShowDialog() != DialogResult.OK)
         {
             return;
         }
         if (form.RegistrationObject == null || form.RegistrationType == null)
         {
             return;
         }
         var bindingList = registrationsBindingSource.DataSource as SortableBindingList<RegistrationInfo>;
         if (bindingList == null)
         {
             return;
         }
         var tagsAsList = form.RegistrationObject[Tags] as List<TagInfo>;
         HashSet<string> tags = null;
         if (tagsAsList != null)
         {
             tags = new HashSet<string>(tagsAsList.Select(t => t.Tag));
         }
         RegistrationInfo registrationInfo = null;
         if (form.RegistrationType == typeof (MpnsRegistrationDescription))
         {
             var registration = tags == null || tags.Count == 0?
                                new MpnsRegistrationDescription(form.RegistrationObject[ChannelUri] as string) :
                                new MpnsRegistrationDescription(form.RegistrationObject[ChannelUri] as string, tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
                 {
                     ChannelUri = registration.ChannelUri.AbsoluteUri,
                     ETag = registration.ETag,
                     ExpirationTime = registration.ExpirationTime,
                     RegistrationId = registration.RegistrationId,
                     Registration = registration,
                     Tags = registration.Tags != null && registration.Tags.Any() ?
                            registration.Tags.Aggregate((a, t) => a + "," + t) :
                            null
                 };
         }
         else if (form.RegistrationType == typeof(MpnsTemplateRegistrationDescription))
         {
             var mpnsHeadersAsList = form.RegistrationObject[MpnsHeaders] as List<NotificationInfo>;
             var mpnsHeaders = new MpnsHeaderCollection();
             if (mpnsHeadersAsList != null)
             {
                 foreach (var item in mpnsHeadersAsList)
                 {
                     mpnsHeaders.Add(item.Name, item.Value);
                 }
             }
             var registration = tags == null || tags.Count == 0 ?
                                new MpnsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                        form.RegistrationObject[BodyTemplate] as string) :
                                mpnsHeaders.Count == 0 ?
                                new MpnsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                        form.RegistrationObject[BodyTemplate] as string,
                                                                        tags) :
                                new MpnsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                        form.RegistrationObject[BodyTemplate] as string,
                                                                        mpnsHeaders,
                                                                        tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
                 {
                 ChannelUri = registration.ChannelUri.AbsoluteUri,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(WindowsRegistrationDescription))
         {
             var registration = tags == null || tags.Count == 0 ?
                                new WindowsRegistrationDescription(form.RegistrationObject[ChannelUri] as string) :
                                new WindowsRegistrationDescription(form.RegistrationObject[ChannelUri] as string, tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
                 {
                 ChannelUri = registration.ChannelUri.AbsoluteUri,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(WindowsTemplateRegistrationDescription))
         {
             var wnsHeadersAsList = form.RegistrationObject[WnsHeaders] as List<NotificationInfo>;
             var wnsHeaders = new WnsHeaderCollection();
             if (wnsHeadersAsList != null)
             {
                 foreach (var item in wnsHeadersAsList)
                 {
                     wnsHeaders.Add(item.Name, item.Value);
                 }
             }
             var registration = tags == null || tags.Count == 0 ?
                                new WindowsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                          form.RegistrationObject[BodyTemplate] as string) :
                                wnsHeaders.Count == 0 ?
                                new WindowsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                           form.RegistrationObject[BodyTemplate] as string,
                                                                           tags) :
                                new WindowsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                           form.RegistrationObject[BodyTemplate] as string,
                                                                           wnsHeaders,
                                                                           tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
                 {
                 ChannelUri = registration.ChannelUri.AbsoluteUri,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(AppleRegistrationDescription))
         {
             var registration = tags == null || tags.Count == 0 ?
                                new AppleRegistrationDescription(form.RegistrationObject[DeviceToken] as string) :
                                new AppleRegistrationDescription(form.RegistrationObject[DeviceToken] as string, tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
             {
                 ChannelUri = registration.DeviceToken,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(AppleTemplateRegistrationDescription))
         {
             var bodyTemplate = form.RegistrationObject.Properties.Any(p => string.Compare(p.Name, BodyTemplate, StringComparison.InvariantCultureIgnoreCase) == 0)
                                    ? form.RegistrationObject[BodyTemplate] as string
                                    : null;
             var registration = tags != null && tags.Count > 0 ?
                                new AppleTemplateRegistrationDescription(form.RegistrationObject[DeviceToken] as string, bodyTemplate, tags) :
                                string.IsNullOrWhiteSpace(bodyTemplate) ?
                                new AppleTemplateRegistrationDescription(form.RegistrationObject[DeviceToken] as string) :
                                new AppleTemplateRegistrationDescription(form.RegistrationObject[DeviceToken] as string, bodyTemplate);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
             {
                 ChannelUri = registration.DeviceToken,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(GcmRegistrationDescription))
         {
             var registration = tags == null || tags.Count == 0 ?
                                new GcmRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string) :
                                new GcmRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string, tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
             {
                 ChannelUri = registration.GcmRegistrationId,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(GcmTemplateRegistrationDescription))
         {
             var bodyTemplate = form.RegistrationObject.Properties.Any(p => string.Compare(p.Name, BodyTemplate, StringComparison.InvariantCultureIgnoreCase) == 0)
                                    ? form.RegistrationObject[BodyTemplate] as string
                                    : null;
             var registration = tags != null && tags.Count > 0 ?
                                new GcmTemplateRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string, bodyTemplate, tags) :
                                string.IsNullOrWhiteSpace(bodyTemplate) ?
                                new GcmTemplateRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string) :
                                new GcmTemplateRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string, bodyTemplate);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
             {
                 ChannelUri = registration.GcmRegistrationId,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         if (registrationInfo == null)
         {
             return;
         }
         writeToLog(string.Format(RegistrationCreatedMessage, 
                                  registrationInfo.PlatformType,
                                  notificationHubDescription.Path));
         bindingList.Add(registrationInfo);
         registrationsBindingSource.DataSource = null;
         registrationsBindingSource.DataSource = new SortableBindingList<RegistrationInfo>(RegistrationInfo.Registrations);
         registrationsDataGridView.DataSource = registrationsBindingSource;
     }
 }