gpt4 book ai didi

c# - 宇宙数据库 : Response status code does not indicate success: BadRequest (400); Substatus: 1001; Reason: ();

转载 作者:行者123 更新时间:2023-12-02 08:17:32 32 4
gpt4 key购买 nike

我正在使用Cosmos DB repository并尝试为以下对象添加新项目。

[Container(nameof(Notifications))]
[PartitionKeyPath("/UserUUId")]
public class Notifications : Item
{
[JsonProperty(PropertyName = "RequestID", Order = 2)]
public string RequestID { get; set; }

[JsonProperty(PropertyName = "NotificationType", Order = 3)]
public int NotificationType { get; set; }

[JsonProperty(PropertyName = "SubType", Order = 4)]
public string SubType { get; set; }

[JsonProperty(PropertyName = "UserUUId", Order = 5)]
public string UserUUId { get; set; }

[JsonProperty(PropertyName = "Payer", Order = 6)]
public string Payer { get; set; }

[JsonProperty(PropertyName = "PayerName", Order = 7)]
public string PayerName { get; set; }

[JsonProperty(PropertyName = "Account", Order = 8)]
public string Account { get; set; }

[JsonProperty(PropertyName = "AccountName", Order = 9)]
public string AccountName { get; set; }

[JsonProperty(PropertyName = "Created", Order = 10)]
public DateTime Created { get; set; }

[JsonProperty(PropertyName = "Updated", Order = 11)]
public DateTime? Updated { get; set; }

[JsonProperty(PropertyName = "Status", Order = 12)]
public int Status { get; set; }

[JsonProperty(PropertyName = "MetaData", Order = 13)]
public Dictionary<string, string> MetaData { get; set; }

protected override string GetPartitionKeyValue() => UserUUId;
}

这是我添加新项目的代码:

public class UINotificationPublisher : IUINotificationPublisher
{

private readonly IRepository<Notifications> _uiNotificationsRepository;

public UINotificationPublisher(
IRepositoryFactory factory

)
{
_uiNotificationsRepository = factory.RepositoryOf<Notifications>();

}

public async Task PublishAsync(IEnumerable<UserSubscriptions> userSubscriptions, CardGroupEvent @event, ILogger logger)
{
try
{
var uiSubscriptions = userSubscriptions
.Where(x => x.Events.Any(y => y.EventType == (int)EventType.CARD_GROUP_CHANGED && y.Channels.Contains((int)ChannelType.UI)));

if (uiSubscriptions.Any())
{
logger.LogInformation($"UI notification subscriptions found, requestID:{@event.RequestID}");

List<Notifications> uiNotifications = new List<Notifications>();

foreach (var item in uiSubscriptions)
{
logger.LogInformation($"creating UI notification for UUID:{item.UUID}. requestID:{@event.RequestID}");

uiNotifications.Add(CreateItemAsync(item, @event));

logger.LogInformation($"successfully created UI Notification for UUID:{item.UUID}: {@event.RequestID}");
}
await _uiNotificationsRepository.CreateAsync(uiNotifications);
}
else
logger.LogInformation($"No UI notification subscriptions found: requestID:{@event.RequestID}");
}
catch (Exception ex)
{
throw ex;
}
}

private Notifications CreateItemAsync(UserSubscriptions userSubscription, CardGroupEvent @event)
{
Notifications notifications = new Notifications
{
Id = Guid.NewGuid().ToString(),
Created = DateTime.UtcNow,
RequestID = @event.RequestID,
NotificationType = (int)EventType.CARD_GROUP_CHANGED,
SubType = @event.EventSubType,
UserUUId = userSubscription.UUID,
Payer = @event.PayerNumber,
PayerName = @event.PayerName,
Account = @event.AccountNumber,
AccountName = @event.AccountName,
MetaData = GetMetaData(@event),
Status = (int)NotificationStatus.UnRead

};

return notifications;

}

private Dictionary<string, string> GetMetaData(CardGroupEvent @event)
{


var metaData = new Dictionary<string, string>();

metaData.Add(nameof(@event.AccountNumber), @event.AccountNumber);
metaData.Add(nameof(@event.AccountName), @event.AccountName);

return metaData;
}
}

Cosmos DB 存储库 CreateAsync:

enter image description here

插入新项目时出现以下异常:

"Response status code does not indicate success: BadRequest (400);Substatus: 1001; ActivityId: 68af0c72-ff2d-4055-8898-b94a08f18ead;Reason: ();"

下面是堆栈跟踪:

at Microsoft.Azure.Cosmos.ResponseMessage.EnsureSuccessStatusCode() atMicrosoft.Azure.Cosmos.CosmosResponseFactoryCore.ProcessMessage[T](ResponseMessageresponseMessage, Func2 createResponse) atMicrosoft.Azure.Cosmos.CosmosResponseFactoryCore.CreateItemResponse[T](ResponseMessageresponseMessage) atMicrosoft.Azure.Cosmos.ContainerCore.d__541.MoveNext()at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() atSystem.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Tasktask) atSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tasktask) atSystem.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()atMicrosoft.Azure.Cosmos.ClientContextCore.d__391.MoveNext()at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() atSystem.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Tasktask) atSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tasktask) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() atMicrosoft.Azure.Cosmos.ClientContextCore.d__301.MoveNext()at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() atSystem.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Tasktask) atSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tasktask) atSystem.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()at Microsoft.Azure.CosmosRepository.DefaultRepository1.d__7.MoveNext()at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() atSystem.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Tasktask) atSystem.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tasktask) at System.Threading.Tasks.ValueTask1.get_Result() atSystem.Runtime.CompilerServices.ValueTaskAwaiter1.GetResult() atCardGroupEventSubscriber.Publishers.UINotificationPublisher.d__2.MoveNext()inC:\notification-subscribers\dev\Notification-Microservices\CardGroupEventSubscriber\CardGroupEventSubscriber\Publishers\UINotificationPublisher.cs:line49

最佳答案

这在 Troubleshooting doc for Bad Request 中有介绍。

A response with this error means you are executing an operation and passing a partition key value that does not match the document's body value for the expected property. If the collection's partition key path is /myPartitionKey, the document has a property called myPartitionKey with a value that does not match what was provided as partition key value when calling the SDK method.

您没有共享调用 Cosmos DB SDK 的代码(仅调用某些存储库对象),因此很难指向正确的代码,但如果您正在调用:

container.CreateItem(<item>, new PartititionKey(pkValue))

此错误意味着您传递的 new PartitionKey(pkValue) 不正确,它不是 item 中容器的分区键定义属性的值。

代码可能假设分区键定义已打开(例如 /Payer),但容器的实际分区键定义是另一个(例如 \id)。

关于c# - 宇宙数据库 : Response status code does not indicate success: BadRequest (400); Substatus: 1001; Reason: ();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72206084/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com