create database in microsoft azure from code c #

0

I'm trying to create a database with a program written in c #. I created a free account and created a resource of azure cosmos db. The connection to the cosmos account db is done correctly but at the time of creating the database I do not get an answer, it stays and waits. Is it possible that I need a larger subscription to do this? The code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json;

namespace AzureApp
{
  class Program
  {
    private const string EndpointUrl = "<endpoint>";
    private const string PrimaryKey = "<privatekey>";
    private DocumentClient client;

    static void Main(string[] args)
    {
        try
        {
            Program p = new Program();
            p.GetStartedDemo().Wait();
        }
        catch (DocumentClientException de)
        {
            Exception baseException = de.GetBaseException();
            Console.WriteLine("{0} error occurred: {1}, Message: {2}", de.StatusCode, de.Message, baseException.Message);
        }
        catch (Exception e)
        {
            Exception baseException = e.GetBaseException();
            Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
        }
        finally
        {
            Console.WriteLine("End of demo, press any key to exit.");
            Console.ReadKey();
        }
    }

    private async Task GetStartedDemo()
    {
        this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);

        await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = "FamilyDB" });
    }

    private void WriteToConsoleAndPromptToContinue(string format, params object[] args)
    {
        Console.WriteLine(format, args);
        Console.WriteLine("Press any key to continue ...");
        Console.ReadKey();
    }
  }
}

Thanks in advance.

    
asked by Xim123 18.10.2018 в 09:03
source

1 answer

0

I understand these steps of this documentation

Azure Cosmos DB: SQL API getting started tutorial

since the code that I observe is identical to the one you have put

I think that the first step would be validated besides that the EndpointUrl and the PrimaryKey are correct, this is fundamental

Now I would also recommend trying to apply these other steps

Quickstart: Build a .NET web app with Azure Cosmos DB using the SQL API and the Azure portal

as you see it differs from the previous one since it creates the db directly in the portal, maybe some issue with the functionality of the creation of the db is affecting the code, so you could create the db from the portal and just connect

In this other example we use

await client.CreateDatabaseAsync(new Database { Id = DatabaseId });

is different from the CreateDatabaseIfNotExistsAsync() , but I would recommend you see the already created db to see if it turns out

On the other hand if you want to validate the connectivity, you could see to use the

Manage Azure Cosmos DB in Azure Storage Explorer

as you will notice is an application that is downloaded and you can connect to the subscription of cosmos db

Another alternative would be

CosmosDbExplorer

is a desktop application that although this is the code, you will see there that you can download the .exe

Another way is with a VS Code plugin

Azure Cosmos DB

As you can see there are several ways to manage db cosmos and validate if you can create the db having access, it would be rare that you can manage it with these tools you can not do it from code

    
answered by 18.10.2018 в 14:03