0

In my project, I want to create a class that handles all the dirty Azure work and provide a clean interface where I simply provide entities to store.

I tried creating the table in my constructor but it does not allow it. And I cannot figure what is wrong with this construction.

Any idea or alternative way of doing things ?

Cheers, Alex

using System;
using System.Threading.Tasks;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;

namespace MyProject
{
    public class AzureStorage
    {
        public async Task<AzureStorage> AzureStorage()
        {
            table = await CreateTableAsync("mytable");
        }

        CloudTable table { get; set; }
    }
}
Community
  • 1
  • 1
AlexandreG
  • 1,633
  • 2
  • 14
  • 18

1 Answers1

1

As far as I know, async constructors are not allowed. This blog written by Stephen Cleary discussed about this issue and shared some alternatives, please read it.

Asynchronous construction poses an interesting problem. It would be useful to be able to use await in a constructor, but this would mean that the constructor would have to return a Task representing a value that will be constructed in the future, instead of a constructed value. This kind of concept would be very difficult to work into the existing language.

Besides, this thread: Can constructors be async? would help you understand async constructor issue.

Community
  • 1
  • 1
Fei Han
  • 26,415
  • 1
  • 30
  • 41