为什么我得到“未设置为对象实例的引用?

时间:2019-12-01 10:48:21

标签: c# azure-cosmosdb azure-cosmosdb-sqlapi

我正在使用新的CosmosDB SDK v3 https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-sdk-dotnet-standard和一个非常简单的插入方法,我已经验证了所有对象确实不是null并具有合理的值,但是我仍然收到错误消息:

<RelativeLayout
    android:layout_width="105dp"
    android:layout_height="105dp">

    <Button
        android:id="@+id/btn"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="5dp"
        android:text="Btn" />

    <ImageButton
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/btn_round" />
</RelativeLayout>

我不明白为什么这是我必须在这里错过一些真正基本的东西,但是我不能动手做的事情。

代码如下:

[1/12/2019 10:35:04] System.Private.CoreLib: Exception while executing function: HAPI_HM_Seasons. Microsoft.Azure.Cosmos.Direct: Object reference not set to an instance of an object.

我已确认已填充列表,并且循环中的season变量包含正确的数据,所以我有点卡在这里。

异常发生在最后一个foreach循环中,我尝试将List<SeasonInformation> seasonInformationList = new List<SeasonInformation>(); foreach(JObject document in listOfSeasons) { SeasonInformation seasonInformation = new SeasonInformation { id = Guid.NewGuid().ToString(), Brand = brand, IntegrationSource = source, DocumentType = Enums.DocumentType.Season, UpdatedBy = "HAPI_HM_Seasons", UpdatedDate = DateTime.Now.ToString(), UpdatedDateUtc = string.Format("{0:yyyy-MM-ddTHH:mm:ss.FFFZ}", DateTime.UtcNow), OriginalData = document }; seasonInformationList.Add(seasonInformation); } database = cosmosClient.GetDatabase(cosmosDBName); container = database.GetContainer(cosmosDBCollectionNameRawData); log.LogInformation(string.Format("HAPI_HM_Seasons BASIC setup done at {0:yyyy-MM-ddTHH:mm:ss.FFFZ}", DateTime.UtcNow)); log.LogInformation(string.Format("HAPI_HM_Seasons import {1} items BEGIN at {0:yyyy-MM-ddTHH:mm:ss.FFFZ}", DateTime.UtcNow, seasonInformationList.Count)); foreach(var season in seasonInformationList) { ItemResponse<SeasonInformation> response = await container.CreateItemAsync(season); } 插入CosmosDB

1 个答案:

答案 0 :(得分:0)

作为最佳实践,您需要在所有Cosmosdb方法中使用带有await的Async方法,只是为了确保它们正在执行并获得响应,

并按如下所示修改您的CreateItemAsync,

ItemResponse<SeasonInformation> response = await container.CreateItemAsync(season, new PartitionKey(season.whatever));

这里是Sample Repository