/// <summary>
        /// Update the service identity key value.
        /// </summary>
        private static void UpdateServiceIdentityKey(string name, byte[] keyValue, ServiceIdentityKeyType keyType)
        {
            ManagementService svc = ManagementServiceHelper.CreateManagementServiceClient();

            ServiceIdentity serviceIdentity = svc.GetServiceIdentityByName(name);

            if (serviceIdentity != null)
            {
                foreach (ServiceIdentityKey key in serviceIdentity.ServiceIdentityKeys)
                {
                    if (key.Type == keyType.ToString())
                    {
                        key.Value = keyValue;
                        svc.UpdateObject(key);
                    }
                }
            }

            svc.SaveChangesBatch();
        }
        /// <summary>
        ///   Creates  a new ServiceIdentity and an associated key of the value, type, and usage specified.
        /// </summary>
        public static ServiceIdentity CreateServiceIdentity(
            this ManagementService svc, string name, byte[] keyValue, ServiceIdentityKeyType keyType, ServiceIdentityKeyUsage keyUsage)
        {
            var sid = new ServiceIdentity {
                Name = name
            };

            DateTime startDate, endDate;

            if (keyType == ServiceIdentityKeyType.X509Certificate)
            {
                //
                // ACS requires that the key start and end dates be within the certificate's validity period.
                //
                var cert = new X509Certificate2(keyValue);

                startDate = cert.NotBefore.ToUniversalTime();
                endDate   = cert.NotAfter.ToUniversalTime();
            }
            else
            {
                startDate = DateTime.UtcNow;
                endDate   = DateTime.MaxValue;
            }

            var key = new ServiceIdentityKey
            {
                EndDate     = endDate.ToUniversalTime(),
                StartDate   = startDate.ToUniversalTime(),
                Type        = keyType.ToString(),
                Usage       = keyUsage.ToString(),
                Value       = keyValue,
                DisplayName = String.Format(CultureInfo.InvariantCulture, "{0} key for {1}", keyType, name)
            };

            svc.AddToServiceIdentities(sid);
            svc.AddRelatedObject(sid, "ServiceIdentityKeys", key);

            return(sid);
        }
        /// <summary>
        ///   Update the service identity key value.
        /// </summary>
        /// <param name = "name">Name of the <see cref = "ServiceIdentityKey" />.</param>
        /// <param name = "keyValue">The new key value.</param>
        /// <param name = "keyType">The new Key type.</param>
        public static void UpdateServiceIdentityKey(this ManagementService svc, string name, byte[] keyValue, ServiceIdentityKeyType keyType)
        {
            ServiceIdentity ServiceIdentity = svc.GetServiceIdentityByName(name);

            if (ServiceIdentity != null)
            {
                foreach (ServiceIdentityKey key in ServiceIdentity.ServiceIdentityKeys)
                {
                    if (key.Type == keyType.ToString())
                    {
                        key.Value = keyValue;
                        svc.UpdateObject(key);
                    }
                }
            }
        }