我们的OData端点是自托管的(OWIN)。对于单个请求:创建,更新,修补和删除一切都很好,但问题是我发送批处理请求。 我看了很多文章,但仍然无法解决问题。 在OData文档中,它说: 我的问题是:如何将“Content-ID”设置为批量请求的标题?
在端点批处理已启用:
HttpConfiguration config = new HttpConfiguration();
var odataBatchHandler = new DefaultODataBatchHandler(new HttpServer(config));
config.MapODataServiceRoute("ODataApi", null, builder.GetEdmModel(), odataBatchHandler);
config.Count().Filter().OrderBy().Expand().MaxTop(null).Select();
appBuilder.UseWebApi(config);
以下是测试:
[TestMethod]
public void BatchRequestTest()
{
var odataAddress = "https://localhost:23170/Sample/Sample/OData/";
var batchUrl = $"{odataAddress}$batch";
HttpClient http = new HttpClient();
// Global batch request
HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, batchUrl);
batchRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", "QWRtaW5pc3RyYXRvcjpwdw==");
MultipartContent batchContent = new MultipartContent("mixed", "batch_" + Guid.NewGuid().ToString());
// Third element: POST user creation
// 1: a multipart content that represents the changeset container
MultipartContent changeSet = new MultipartContent("mixed", "changeset_" + Guid.NewGuid().ToString());
// 2: one request message object per post request
HttpRequestMessage postRequest = new HttpRequestMessage(HttpMethod.Post, $"{odataAddress}/SATeacher");
postRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", "QWRtaW5pc3RyYXRvcjpwdw==");
postRequest.Content = new StringContent("", Encoding.UTF8, "application/json");
// 3: one message content per corresponding post request
HttpMessageContent postRequestContent = new HttpMessageContent(postRequest);
postRequestContent.Headers.Remove("Content-Type");
postRequestContent.Headers.Add("Content-Type", "application/http");
postRequestContent.Headers.Add("Content-Transfer-Encoding", "binary");
// Add this POST content to the changeset
changeSet.Add(postRequestContent);
// Add the changeset to the batch content
batchContent.Add(changeSet);
// Here we go
batchRequest.Content = batchContent;
HttpResponseMessage response = http.SendAsync(batchRequest).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
}
这是一个例外:
{“消息”:“发生了错误。”,“ExceptionMessage”:“批处理操作的标头集合中没有名称为'Content-ID'的标头。”,“ExceptionType”:“Microsoft .OData.ODataException“,”StackTrace“:”Microsoft.OData.ODataBatchReader.CreateOperationRequestMessageImplementation()\ r \ n在Microsoft.OData.ODataBatchReader.InterceptException [T](Func 1 action)\r\n at Microsoft.OData.ODataBatchReader.CreateOperationRequestMessage()\r\n at System.Web.OData.Batch.ODataBatchReaderExtensions.<ReadOperationInternalAsync>d__6.MoveNext() in C:\\Builds\\Eagle\\Eagle\\Framework\\Server\\OData\\Source\\System.Web.OData\\OData\\Batch\\ODataBatchReaderExtensions.cs:line 149\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\ r \ n \ n在C:\ Builds \ Eagle \ Eagle \ Framework \ Server \ OData \ Source \ System.Web.OData \ OData \ Batch \ ODataBatchReaderExtensions.cs中的System.Web.OData.Batch.ODataBatchReaderExtensions.d__1.MoveNext()处:第63行\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n中的\ r \ n。 System.Web.OData.Batch.DefaultODataBatchHandler.d__1.MoveNext上的System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()\r\n at System.Web.OData.Batch.DefaultODataBatchHandler.<ParseBatchRequestsAsync>d__3.MoveNext() in C:\\Builds\\Eagle\\Eagle\\Framework\\Server\\OData\\Source\\System.Web.OData\\OData\\Batch\\DefaultODataBatchHandler.cs:line 141\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\ r \ n的CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n( )在C:\ Builds \ Eagle \ Eagle \ Framework \ Server \ OData \ Source \ System.Web.OData \ OData \ Batch \ DefaultODataBatchHandler.cs:第44行\ r \ n ---从上一个位置开始的堆栈跟踪异常被抛出--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在System.Web上.Http.Batch.HttpBatchHandler.d__0.MoveNext()“}
排队异常:HttpResponseMessage response = http.SendAsync(batchRequest).Result;
有什么想法吗?
提前致谢,
答案 0 :(得分:1)
我认为错误是描述性的。你可以尝试,postRequestContent.Headers.Add("Content-ID", "1");
,
更多信息, http://www.odata.org/documentation/odata-version-3-0/batch-processing/