6

I'm trying to use azure blob storage. I uploaded some images successfully, but all the sudden I get the error:

An existing connection was forcibly closed by the remote host

I looked into it and the exception is thrown whenever I try to check if a blob container exists.

This is my code:

BlobClient getter property: (note, I have marked sensitive data in the connection string with **)

static string connectionString = "DefaultEndpointsProtocol=https;AccountName=**;AccountKey=**;BlobEndpoint=https://**.blob.core.windows.net/;TableEndpoint=https://**.table.core.windows.net/;QueueEndpoint=https://**.queue.core.windows.net/;FileEndpoint=https://**.file.core.windows.net/";
public static CloudBlobClient BlobClient
{
            get
            {
                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

                // Create the blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                return blobClient;
            }
}

The actual code throwing the exception:

 CloudBlobContainer container = BlobClient.GetContainerReference(containerName);
 if (!container.Exists())

To be precise, the exception occurs at the line where I check if the container exists.

I have no idea what's wrong. I am positive that the connection string is right(I copied it in).

I would REALLY appreciate if someone could tell me what the issue possibly could be.

juvchan
  • 6,113
  • 2
  • 22
  • 35
J. Doe
  • 677
  • 7
  • 20

2 Answers2

0

A best practice for scalability is to increase the .NET default connection limit to 100. By default in a client environment the default is 2. The default connection limit must be set prior to opening any connections. For this an other scalability best practice please see the Microsoft Azure Storage Performance and Scalability Checklist.

0

It can also happen due to a timeout. In such case, you can use BlobRequestOptions to set the timeout of your choice. (I found it useful in CloudBlobContainer.ListBlobsSegmented method.)

Here is the code example for your question:

CloudBlobContainer container = blobClient.GetContainerReference(containerName);

var containerExists = container.Exists(new BlobRequestOptions() {
    ServerTimeout = TimeSpan.FromMinutes(5) 
});

if (!containerExists)
// ...
Dima G
  • 1,945
  • 18
  • 22