0

誰かがこの問題で私を助けてくれることを願っています。ドキュメントを Cosmos DB に挿入する方法は 2 つあります。実行するたびに1つが機能します。もう 1 つは CreateDatabaseIfNotExistsAsync まで到達し、メソッドからキックアウトして、呼び出し元のメソッドに戻ります。ItemReponse オブジェクトの Status プロパティには、「WaitingForActivation」と表示されます。

このロジックは一度は機能しましたが、現在は (変更なしで) 機能していません。以下は死ぬコードです。最初に単体テスト:

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");
}

Cosmos Emulator を実行しているため、ローカルホストの URL を追加します。次に、死ぬコードについて説明します。

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;
    }
}
4

1 に答える 1