![](/img/trans.png)
[英]Bot Framework C# Luis Getting > Exception: Response status code does not indicate success: 400 (Bad Request)
[英]Cosmos DB: Response status code does not indicate success: BadRequest (400); Substatus: 1001; Reason: ();
我正在使用Cosmos DB 存储库并尝试为以下对象添加新项目。
[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:
插入新项目时出现异常:
“响应状态码不表示成功:BadRequest(400);Substatus:1001;ActivityId:68af0c72-ff2d-4055-8898-b94a08f18ead;原因:();”
下面是堆栈跟踪:
Microsoft.Azure.Cosmos.ResponseMessage.EnsureSuccessStatusCode() 在 Microsoft.Azure.Cosmos.CosmosResponseFactoryCore.ProcessMessage[T](ResponseMessage responseMessage, Func2 createResponse) 在 Microsoft.Azure.Cosmos.CosmosResponseFactoryCore.CreateItemResponse[T](ResponseMessage responseMessage) 在Microsoft.Azure.Cosmos.ContainerCore.d__541.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() 在 Microsoft.Azure.Cosmos.ClientContextCore.d__391.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 S 的 TaskAwaiter.ThrowForNonSuccess(Task task) ystem.Runtime.CompilerServices.TaskAwaiter1.GetResult() 在 Microsoft.Azure.Cosmos.ClientContextCore.d__301.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() 在 Microsoft.Azure.CosmosRepository.DefaultRepository1.d__7.MoveNext() 在 System.Runtime.ExceptionServices。 ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Threading.Tasks.ValueTask1.get_Result() 在 System.Runtime。 C:\notification-subscribers\dev\Notifi 中 CardGroupEventSubscriber.Publishers.UINotificationPublisher.d__2.MoveNext() 的 CompilerServices.ValueTaskAwaiter1.GetResult() 阳离子微服务\CardGroupEventSubscriber\CardGroupEventSubscriber\Publishers\UINotificationPublisher.cs:第 49 行
错误请求的故障排除文档中对此进行了介绍
带有此错误的响应意味着您正在执行操作并传递与预期属性的文档正文值不匹配的分区键值。 如果集合的分区键路径是 /myPartitionKey,则文档有一个名为 myPartitionKey 的属性,其值与调用 SDK 方法时提供的分区键值不匹配。
您没有共享调用 Cosmos DB SDK 的代码(只是调用某个存储库对象),因此很难指向正确的代码,但如果您正在调用例如:
container.CreateItem(<item>, new PartititionKey(pkValue))
此错误意味着您传递的new PartitionKey(pkValue)
不正确,它不是item
中容器的 Partition Key Definition 属性的值。
该代码可能假设分区键定义已打开(例如/Payer
),但容器的实际分区键定义是另一个(例如\id
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.