Azure Table 存储 - CreateTableIfNotExists 挂起 - 之前工作正常

Azure Table Storage - CreateTableIfNotExists hanging - was previously working

我一直在努力让一些代码在新机器上工作。我以为我终于让它工作了,但现在当我 运行 它时,代码在尝试创建 table 存储(或者实际上得到对它的引用,因为它已经存在)......我知道必须确保它一直是异步的并且这段代码正在工作......我现在已经改变了它试图改进它并让它在这台机器上工作,但有些地方不对...所以在 Repo 的构造函数中我这样做....

public LdsRepo() 
    {

        if (!GetTableRef("licensedatesummary").Result)
        {
            throw new Exception("It broke!");
        }
    }

我的 GetTableReference 方法(基于 class)是这样的...

protected async Task<bool>  GetTableRef(string tableRef)
    {
        try
        {
            if (string.IsNullOrEmpty(StorageAccountName) || string.IsNullOrEmpty(AuthKey))
            {
                throw new ArgumentNullException(nameof(tableRef),
                    "storageaccountname or authk are not set in the config");
            }
            if (_tableClient == null)
                _tableClient =
                    new CloudStorageAccount(new StorageCredentials(StorageAccountName, AuthKey), true)
                        .CreateCloudTableClient();
            if (_table == null)
            {
                // Create the CloudTable object that represents the referenced table.
                _table = _tableClient.GetTableReference(tableRef);
                var x =  _table.CreateIfNotExistsAsync();
                return await x;
            }
            return true;
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
            Trace.WriteLine(ex.ToString());
            return false;
        }
    }

如果有帮助...这是我的包配置...

<packages>
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Data.Edm" version="5.7.0" targetFramework="net452" />
<package id="Microsoft.Data.OData" version="5.7.0" targetFramework="net452" />
<package id="Microsoft.Data.Services.Client" version="5.7.0" targetFramework="net452" />
<package id="Microsoft.Web.WebJobs.Publish" version="1.0.11" targetFramework="net452" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net452" />
<package id="System.Spatial" version="5.7.0" targetFramework="net452" />
<package id="WindowsAzure.Storage" version="7.0.0" targetFramework="net452" /> </packages>

最后是我的实际 WebApi 控制器....

[EnableCors(origins: "*", headers: "*", methods: "*")]
[RoutePrefix("api")]
public class LinkDataController : ApiController
{
    private readonly IFloatingObjectRecordRepo _repo;
    public LinkDataController()
    {
        _repo = new FloatingObjectRecordRepo();
    }
    [HttpGet, Route("linkdata/{id}")]
    public async Task<IHttpActionResult> Get(string id)
    {
        try
        {
            if (string.IsNullOrEmpty(id))
            {
                return BadRequest();
            }
            var retStr = await  _repo.SearchById(id);
            if (retStr == "Not found")
                return new IsoncOkResult<KeyValuePair<string, string>>(
                    new KeyValuePair<string, string>(id, retStr), this);
            var g = Guid.NewGuid();
            var f = await _repo.Add(new FlorData
            {
                CreatedBy = "Ais Integration Search",
                Mmsi = id,
                CreatedOn = DateTime.Now,
                GeneratedId = g,
                RowKey = g.ToString()
            });
            retStr = f.GeneratedId.ToString();
            return new IsoncOkResult<KeyValuePair<string, string>>(new KeyValuePair<string, string>(id, retStr), this);
        }
        catch (Exception ex)
        {
            Trace.TraceError($"Error Searching / Creating AIS Link Data: {ex}");
            return BadRequest(ex.ToString());
        }
    }
   }

我在这里进行了相当广泛的搜索,但每次都是有人没有一直关注异步链的地方,我想我在做什么?

使用时

if (!GetTableRef("licensedatesummary").Result)

您错过了 "Async all the way" 的要点,请参阅 This question 了解 await Task 和 Task.Result 之间的区别。

为了解决这个问题,有几种选择,这里只是两个:

  1. 您可以将 "GetTableRef" 更改为同步(如果这是您对该方法的唯一用途,那么这应该是一个不错的选择,因为仅在阻塞上下文中使用异步方法没有任何好处)使用 CreateIfNotExists() 方法而不是 CreateIfNotExistsAsync()。
  2. 请参阅 How to call async method in constructor,其中解释了针对此类场景的一些技巧