C# C中异步创建新DocumentDB文档的问题#
我刚开始使用DocumentDB,所以我下载了.NETSDK。按照从Azure网站下载的使用示例,我尝试创建一些文档以保存到DocumentDB。示例代码如下所示C# C中异步创建新DocumentDB文档的问题#,c#,azure,azure-cosmosdb,C#,Azure,Azure Cosmosdb,我刚开始使用DocumentDB,所以我下载了.NETSDK。按照从Azure网站下载的使用示例,我尝试创建一些文档以保存到DocumentDB。示例代码如下所示 public static void Main(string[] args) { try { GetStartedDemo().Wait(); } catch (DocumentClientException de)
public static void Main(string[] args)
{
try
{
GetStartedDemo().Wait();
}
catch (DocumentClientException de)
{
// omissis
}
}
private static async Task GetStartedDemo()
{
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
Family andersonFamily = new Family
{
Id = "AndersenFamily",
LastName = "Andersen",
Parents = new Parent[] {
new Parent { FirstName = "Thomas" },
new Parent { FirstName = "Mary Kay"}
},
Children = new Child[] {
new Child
{
FirstName = "Henriette Thaulow",
Gender = "female",
Grade = 5,
Pets = new Pet[] {
new Pet { GivenName = "Fluffy" }
}
}
},
Address = new Address { State = "WA", County = "King", City = "Seattle" },
IsRegistered = true
};
await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, andersonFamily);
client.Dispose();
}
public static async Task Save(int UserId, int productID, string code, string language, string dataOraLettura, string longitudine, string latitudine, string nazione_lettura)
{
CultureInfo ciEN = CultureInfo.GetCultureInfo("en-US");
CodeType ScannedCode = new CodeType
{
Code = code,
ProductId = productID,
ScanLog = new List<CodeType.ScanDataType>()
};
ScannedCode.ScanLog.Add(new CodeType.ScanDataType
{
Location = new Microsoft.Azure.Documents.Spatial.Point(double.Parse(longitudine, ciEN.NumberFormat), double.Parse(latitudine, ciEN.NumberFormat)),
ScanType = CodeType.ScanDataType.eScanType.Alert,
UserId = UserId.ToString(),
TimeStamp = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmssffff"))
});
await ScannedCode.AddAsync();
}
public async Task AddAsync()
{
using (DocumentClient client = new DocumentClient(new Uri(WebConfigurationManager.AppSettings["DbURI"]), WebConfigurationManager.AppSettings["DbKEY"]))
{
var CodeDocuments = client.CreateDocumentQuery<CodeType>("dbs/" + database.Id + "/colls/" + coll.Id).Where(a => a.Code == this.Code).ToArray();
if (CodeDocuments.Length == 0)
await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + coll.Id, this);
}
}
这段代码运行良好。我的代码几乎相同,除了我传递的用于将JSON保存到DB中的类。它总是停在线路上
await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + coll.Id, this);
我的全部代码如下
public static void Main(string[] args)
{
try
{
GetStartedDemo().Wait();
}
catch (DocumentClientException de)
{
// omissis
}
}
private static async Task GetStartedDemo()
{
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
Family andersonFamily = new Family
{
Id = "AndersenFamily",
LastName = "Andersen",
Parents = new Parent[] {
new Parent { FirstName = "Thomas" },
new Parent { FirstName = "Mary Kay"}
},
Children = new Child[] {
new Child
{
FirstName = "Henriette Thaulow",
Gender = "female",
Grade = 5,
Pets = new Pet[] {
new Pet { GivenName = "Fluffy" }
}
}
},
Address = new Address { State = "WA", County = "King", City = "Seattle" },
IsRegistered = true
};
await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + documentCollection.Id, andersonFamily);
client.Dispose();
}
public static async Task Save(int UserId, int productID, string code, string language, string dataOraLettura, string longitudine, string latitudine, string nazione_lettura)
{
CultureInfo ciEN = CultureInfo.GetCultureInfo("en-US");
CodeType ScannedCode = new CodeType
{
Code = code,
ProductId = productID,
ScanLog = new List<CodeType.ScanDataType>()
};
ScannedCode.ScanLog.Add(new CodeType.ScanDataType
{
Location = new Microsoft.Azure.Documents.Spatial.Point(double.Parse(longitudine, ciEN.NumberFormat), double.Parse(latitudine, ciEN.NumberFormat)),
ScanType = CodeType.ScanDataType.eScanType.Alert,
UserId = UserId.ToString(),
TimeStamp = long.Parse(DateTime.Now.ToString("yyyyMMddHHmmssffff"))
});
await ScannedCode.AddAsync();
}
public async Task AddAsync()
{
using (DocumentClient client = new DocumentClient(new Uri(WebConfigurationManager.AppSettings["DbURI"]), WebConfigurationManager.AppSettings["DbKEY"]))
{
var CodeDocuments = client.CreateDocumentQuery<CodeType>("dbs/" + database.Id + "/colls/" + coll.Id).Where(a => a.Code == this.Code).ToArray();
if (CodeDocuments.Length == 0)
await client.CreateDocumentAsync("dbs/" + database.Id + "/colls/" + coll.Id, this);
}
}
public static async Task Save(int UserId、int productID、string code、string language、string dataOraLettura、string longitudine、string latitudine、string nazione_lettura)
{
CultureInfo ciEN=CultureInfo.GetCultureInfo(“en-US”);
代码类型ScannedCode=新代码类型
{
代码=代码,
ProductId=ProductId,
ScanLog=新列表()
};
ScannedCode.ScanLog.Add(新代码类型.ScandaType
{
Location=new Microsoft.Azure.Documents.Spatial.Point(double.Parse(longitudine,ciEN.NumberFormat),double.Parse(latitudine,ciEN.NumberFormat)),
ScanType=CodeType.ScandaType.eScanType.Alert,
UserId=UserId.ToString(),
TimeStamp=long.Parse(DateTime.Now.ToString(“yyyymmddhhmmssfff”))
});
等待ScannedCode.AddAsync();
}
公共异步任务AddAsync()
{
使用(DocumentClient client=new DocumentClient(新Uri(WebConfiguration Manager.AppSettings[“DbURI”]),WebConfiguration Manager.AppSettings[“DbKEY”]))
{
var CodeDocuments=client.CreateDocumentQuery(“dbs/”+database.Id+“/colls/”+coll.Id)。其中(a=>a.Code==this.Code)。ToArray();
如果(CodeDocuments.Length==0)
等待client.CreateDocumentAsync(“dbs/”+database.Id+“/colls/”+coll.Id,this);
}
}
异步任务似乎会阻塞,并且它不会将控制权返回给调用方,然后保持循环执行。正确,您有一个典型的异步阻塞另一个异步的情况。 在wait ScannedCode.AddAsync()上,应将ConfigureWait设置为false 这使其在线程池线程而不是ASP.NET请求上下文上恢复
您可以阅读下面的文章,其中很好地解释了这一点以及使用异步代码时的其他最佳实践。Buncha小东西:ScannedCode.AddAsync()看起来不是静态的,但它被称为静态的。你确定你没有超载吗?在提取“dbs/../colls/”模式之前,需要重复多少次?如果你在一个方法上有20个参数,你肯定错过了一个——把它们放在一个类中。不要遵循示例如何隐藏exception@StenPetrov这不是代码审查;OP询问了一个异步问题。无需对编码风格提出批评。ScannedCode.AddAsync()似乎不是静态的,这是我首先要检查的。这个问题本身需要一个回顾,没有足够的信息来继续。。。。在某些情况下,如果OP重构自己的代码,他们会自己发现错误。你的观点很好,因此我的回答是评论,而不是回答。尝试将
wait client.CreateDocumentAsync
更改为client.CreateDocumentAsync(…).Result