Created
October 23, 2017 10:39
-
-
Save gmantri/2f28244ff1e0e0a2594f3a28b5fdbcc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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