gpt4 book ai didi

Azure 表存储 - CreateTableIfNotExists 挂起 - 之前可以正常工作

转载 作者:行者123 更新时间:2023-12-02 21:04:15 26 4
gpt4 key购买 nike

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

public LdsRepo() 
{

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

我的 GetTableReference 方法(在基类上)是这样的......

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 Controller ......

[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)

您错过了“一路异步”的要点,请参阅 This question了解await Task 和Task.Result 之间的区别。

有多种选择可以解决此问题,这里仅提供两个:

  1. 您可以通过使用 CreateIfNotExists( ) 方法而不是 CreateIfNotExistsAsync()。
  2. 参见How to call async method in constructor这解释了这种情况下的一些技术

关于Azure 表存储 - CreateTableIfNotExists 挂起 - 之前可以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36867583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com