the sample console app from the emulator to create a database is throwing error

Divya Ravindran 0 Reputation points
2024-09-17T00:57:58.1266667+00:00

Hello,

I'm downloaded and trying to execute the sample console app for database creation for Azure cosmos emulator. However it throwing me the below error.

IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request..

Here is the code on Program.cs

CosmosClientOptions options = new CosmosClientOptions

{

HttpClientFactory = () =>

{

    return new HttpClient(new HttpClientHandler

    {

        // Disable SSL validation for development purposes only.

        ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator

    });

},

ConnectionMode = ConnectionMode.Gateway,

};

// Ensure correct disposal pattern for CosmosClient

using CosmosClient client = new CosmosClient(

accountEndpoint: "https://localhost:8081",

authKeyOrResourceToken: "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",

clientOptions: options

);

// Create the database if it doesn't exist

DatabaseResponse databaseResponse = await client.CreateDatabaseIfNotExistsAsync("sample");

Console.WriteLine($"Database created: {databaseResponse.Database.Id}");

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,672 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sai Raghunadh M 1,045 Reputation points Microsoft Vendor
    2024-09-17T15:16:48.57+00:00

    Hi @Divya Ravindran,

    Thanks for the question and using MS Q&A platform.

    I tested the same code you're using, and it worked perfectly on my end. Could you please try the following steps and see if it works for you too?

    First make sure that your Cosmos DB Emulator is running.

    1. It should be run inhttps://localhost:8081/_explorer/index.html in your browser. User's image
      1. Create a .NET Console Application using Visual Studio.
      "Create a new project" and select "Console App" from the list. Click "Next," name your project (e.g., CosmosDbSampleApp), and click "Create."

    User's image

    1. Install the Azure Cosmos DB SDK.

    Open the NuGet Package Manager (right-click on the project in Solution Explorer, then select "Manage NuGet Packages"). Search for Microsoft.Azure.Cosmos and install it.

    User's imageUser's image

    Paste your code in program.cs and click on "start" button to build and run your application.

    User's image

    You will not find any issues and the database is created.

    User's image

    Now, Open the Cosmos DB Emulator and you can find the Data Base there.

    User's image

    If the issue still persists, please check the following:

    Step1: Ensure the emulator is running by visiting https://localhost:8081/_explorer/index.html in your browser.

    User's image

    Step2: Ensure that your firewall or antivirus software is not blocking connections to localhost.

    Step3: Ensure that Microsoft.Azure.Cosmos is up to date.

    Step4: Sometimes restarting the emulator and your system can resolve connection issues.

    For more details, refer the Develop locally using the Azure Cosmos DB emulator and checkout a video on Setting Up Azure Cosmos DB Emulator for Development.

    I hope this information helps, please do let us know if you have any Queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.