Skip to content

Instantly share code, notes, and snippets.

@gmantri
Created October 23, 2017 10:39
Show Gist options
  • Save gmantri/2f28244ff1e0e0a2594f3a28b5fdbcc9 to your computer and use it in GitHub Desktop.
Save gmantri/2f28244ff1e0e0a2594f3a28b5fdbcc9 to your computer and use it in GitHub Desktop.
using System;
using Microsoft.Azure.Documents;
using System.Threading.Tasks;
using Microsoft.Azure.Documents.Client;
namespace StackOverflow.NetCore
{
class Program
{
private static IDocumentClient client = new Microsoft.Azure.Documents.Client.DocumentClient(new Uri("https://myaccount.documents.azure.com"), "accountkey==");
private static string _databaseId = "digivate_clients_0_blogging";
static void Main(string[] args)
{
CreateDatabaseIfNotExistsAsync().GetAwaiter().GetResult();
Console.ReadLine();
}
public static async Task CreateDatabaseIfNotExistsAsync()
{
try
{
await client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(_databaseId));
}
catch (DocumentClientException e)
{
if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await client.CreateDatabaseAsync(new Database { Id = _databaseId }); // Throws Not Found Here!
}
else
{
Console.Write(e.StatusCode);
Console.ReadKey();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment