Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用Cosmos 3.3 sdk使用CreateItemAsync写入我的容器_C#_Azure Cosmosdb_Azure Cosmosdb Sqlapi - Fatal编程技术网

C# 无法使用Cosmos 3.3 sdk使用CreateItemAsync写入我的容器

C# 无法使用Cosmos 3.3 sdk使用CreateItemAsync写入我的容器,c#,azure-cosmosdb,azure-cosmosdb-sqlapi,C#,Azure Cosmosdb,Azure Cosmosdb Sqlapi,我正在使用Cosmos 3.3 sdk,下面是要写入容器的代码 public void WriteErrorLogs(Error entity, string collectionName) { try { Container container = cosmosclient.GetContainer(databaseName, collectionName); en

我正在使用Cosmos 3.3 sdk,下面是要写入容器的代码

 public void WriteErrorLogs(Error entity, string collectionName)
        {
            try
            {
                Container container = cosmosclient.GetContainer(databaseName, collectionName);
                entity.LogID = Guid.NewGuid().ToString();          
                container.CreateItemAsync<Error>(entity, new PartitionKey(entity.LogID));
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
我将分区键定义为作为guid的LogID。由于代码约束,该方法需要同步。
不确定我的错误在哪里,但在调试时,总是会出现“容器错误CS0103:名称“容器”在当前上下文中不存在”并且没有创建日志。非常感谢您提供的任何帮助,谢谢您正在执行一个fire and forget,
CreateItemAsync
是一个
任务,一个,而你不是在等待它

public async Task WriteErrorLogs(Error entity, string collectionName)
{
    try
    {
        Container container = cosmosclient.GetContainer(databaseName, collectionName);
        entity.LogID = Guid.NewGuid().ToString();          
        await container.CreateItemAsync<Error>(entity, new PartitionKey(entity.LogID));
    }
    catch (Exception ex)
    {
        throw ex;
    }

}
在构建过程中,可能会有人指出这个特定的问题

编辑:根据OP注释,添加如何强制同步操作,但这是不推荐的,您可能会以死锁结束,请参阅

public void WriteErrorLogs(错误实体、字符串集合名称)
{
尝试
{
Container Container=cosmosclient.GetContainer(databaseName,collectionName);
entity.LogID=Guid.NewGuid().ToString();
container.CreateItemAsync(entity,new PartitionKey(entity.LogID)).GetAwaiter().GetResult();
//或者ItemResponse response=container.CreateItemAsync(entity,new PartitionKey(entity.LogID)).Result;
//或者container.CreateItemAsync(entity,new PartitionKey(entity.LogID)).Wait();
}
捕获(例外情况除外)
{
掷骰子;
}
}

我发现了问题。正如Matias所说,这是一个线程问题。我以task.run(async..functioncall.wait()的形式显式运行任务
然后在将createItemasync定义为同步调用的函数调用中。

您得到的错误是不言自明的,变量
容器
在您使用它的上下文中不存在。事实上从你发布的代码中,它只存在于你的
try
子句中。添加一个调试点,看看容器中是什么?我有一个调试器,但没有在容器中得到任何东西,因为它不在上下文中,它没有显示为对象。你们是说我需要定义上面的语句try吗?这让我很惊讶?谢谢你们的支持解释但是,由于调用代码的日志层次结构,我希望将此作为同步操作运行。例如,此函数从100个其他函数调用,而那些函数从另200个函数调用。这是一个巨大的遗留代码库,并且执行所有异步操作确实很痛苦。以同步方式调用异步代码实际上并不困难NET中建议您锁定线程,您肯定会陷入死锁状态。话虽如此,我已经编辑了答案,其中包括如何同步运行。我实现了所有三种方式,下面是发生的情况和相同的最终结果。日志没有被写入。对于您提到的所有方式,te页面加载时间很长,没有发生任何事情。也没有例外,不确定在这种情况下如何捕获异常。您可能会遇到死锁,这就是为什么不建议锁定异步操作的原因。这基本上会使此操作要么不完成,要么永远无法完成,可能在调用
.Result
时线程已经锁定。解决死锁或以异步方式正确使用异步代码。
public async Task WriteErrorLogs(Error entity, string collectionName)
{
    try
    {
        Container container = cosmosclient.GetContainer(databaseName, collectionName);
        entity.LogID = Guid.NewGuid().ToString();          
        await container.CreateItemAsync<Error>(entity, new PartitionKey(entity.LogID));
    }
    catch (Exception ex)
    {
        throw ex;
    }

}
await WriteErrorLogs(entity, "myerrorcollection")
public void WriteErrorLogs(Error entity, string collectionName)
{
    try
    {
        Container container = cosmosclient.GetContainer(databaseName, collectionName);
        entity.LogID = Guid.NewGuid().ToString();          
        container.CreateItemAsync<Error>(entity, new PartitionKey(entity.LogID)).GetAwaiter().GetResult();
        // or ItemResponse<Error> response = container.CreateItemAsync<Error>(entity, new PartitionKey(entity.LogID)).Result;
        // or container.CreateItemAsync<Error>(entity, new PartitionKey(entity.LogID)).Wait();
    }
    catch (Exception ex)
    {
        throw ex;
    }

}