private async Task InitializeEncryptionKeys() { var client = await KeyManagementServiceClient.CreateAsync(); var keyRingName = KeyRingName.FromProjectLocationKeyRing(KmsKeyName.ProjectId, KmsKeyName.LocationId, KmsKeyName.KeyRingId); try { await client.GetKeyRingAsync(keyRingName); } catch (RpcException e) when(e.StatusCode == StatusCode.NotFound) { await client.CreateKeyRingAsync(new CreateKeyRingRequest { ParentAsLocationName = LocationName.FromProjectLocation(keyRingName.ProjectId, keyRingName.LocationId), KeyRingId = KmsKeyName.KeyRingId, KeyRing = new KeyRing(), }); } var keyName = Google.Cloud.Kms.V1.CryptoKeyName.FromProjectLocationKeyRingCryptoKey(KmsKeyName.ProjectId, KmsKeyName.LocationId, KmsKeyName.KeyRingId, KmsKeyName.CryptoKeyId); try { await client.GetCryptoKeyAsync(keyName); } catch (RpcException e) when(e.StatusCode == StatusCode.NotFound) { await client.CreateCryptoKeyAsync(new CreateCryptoKeyRequest { ParentAsKeyRingName = keyRingName, CryptoKeyId = keyName.CryptoKeyId, CryptoKey = new CryptoKey { Purpose = CryptoKey.Types.CryptoKeyPurpose.EncryptDecrypt, }, }); } }