简体   繁体   English

Cosmos Insert 只工作一次

[英]Cosmos Insert works only once

I'm hoping someone can help me with this issue.我希望有人可以帮助我解决这个问题。 I have two methods that insert a document into a Cosmos DB.我有两种将文档插入 Cosmos DB 的方法。 One works each and every time I run it.每次我运行它时都有一个工作。 The other gets as far as CreateDatabaseIfNotExistsAsync and kicks out of the method and returns to the calling method.另一个到达 CreateDatabaseIfNotExistsAsync 并退出该方法并返回到调用方法。 The ItemReponse objects Status property says "WaitingForActivation". ItemReponse 对象的状态属性显示“WaitingForActivation”。

I will say that this logic did work once and now (with no changes) it does not.我会说这个逻辑曾经工作过,现在(没有变化)它没有。 Below is the code that dies.下面是死亡的代码。 First the Unit test:首先是单元测试:

public void AddPerson()
{
    var logger = Mock.Of<ILogger<Person>>();
    var appset = Mock.Of<IAppSettings>();

    appset.DatabaseName = "PersonsDB";
    appset.ContainerName = "PersonsContainer";
    appset.EndpointUrl = "https://localhost:8081";
    appset.AuthorizationKey = "somesecretkey";

    Person person = new Person()
    {
        id = Guid.NewGuid().ToString(),
        Address_line_1 = "1932 Alakea St",
        Address_line_2 = "",
        Birth_Date = "2020-01-01",
        Gender = "m",
        Patient_First_Name = "John",
        Patient_Last_Name = "Ridley",
        Hospital_Code = "Queens Med",
        Rec_ID = "1111111111",
        VER_CRM_ID = "22222222",
        VER_EMAIL = "JR@noemail.com",
    };

    PersonCosmos personCosmos = new PersonCosmos(logger, appset);
    var myresult = personCosmos.PutPersonData(person);
    Assert.True(myresult.Exception == null, "Row added");
}

Let me add that I am running the Cosmos Emulator hence the localhost url.让我补充一点,我正在运行 Cosmos Emulator,因此是 localhost url。 Now for the code that dies.现在是死亡的代码。

public async Task<ItemResponse<Person>> PutPersonData(Person persondata)
{
    this.cosmosClient = new CosmosClient(EndpointUri, PrimaryKey);
    this.database = await this.cosmosClient.CreateDatabaseIfNotExistsAsync(DatabaseName);
    this.container = await this.database.CreateContainerIfNotExistsAsync(ContainerName, "/Hospital_Code");
    ItemResponse<Person> PutResponse = null;

    try
    {
        // Read the item to see if it exists.  
        PutResponse = await this.container.ReadItemAsync<Person>(persondata.id.ToString(), new PartitionKey(persondata.Hospital_Code ));
        return PutResponse;
    }
    catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
    {
        // Create an item in the container representing the Appointment. Note we provide the value of the partition key for this item, which is "Hospital_Code"
        PutResponse = await this.container.CreateItemAsync<Person>(persondata, new PartitionKey(persondata.Hospital_Code));
        return PutResponse;
    }
    catch (Exception ex)
    {
        // Create an item in the container representing the object. Note we provide the value of the partition key for this item, which is "Hospital_Code"
        _logger.LogError($"Error in {0} at {1} " + ex.Message, System.Reflection.MethodBase.GetCurrentMethod().Name, DateTime.UtcNow.ToLongTimeString());
        return PutResponse;
    }
}

You are mixing sync and async in your code here.您在这里的代码中混合了同步和异步。

Use await on this line.在这一行使用 await。

var myresult = **await** personCosmos.PutPersonData(person); 

and also this calling function should be async as well而且这个调用函数也应该是异步的

public void AddPerson()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM