2

我在使用 ASP .NET Web API 设置 OData Web 服务时遇到问题。我需要将该服务作为数据源提供给 Lightswitch。获取请求正常工作,但是当我尝试 POST 时 - 这就是我卡住的地方。

错误

在 Lightswitch 我得到错误:

{"Message":"批处理请求的媒体类型必须为 'multipart/mixed'。"}

调用添加/编辑命令时。

由于 Lightswitch 需要 $batch 端点,我安装了支持它们的最新 RC 版本的 Web API。同样在开始配置批处理端点之前,我遵循了以下教程:http ://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api

然后我按照本教程设置批处理端点:https ://aspnetwebstack.codeplex.com/wikipage?title=Web%20API%20Request%20Batching

我重温了这篇文章:如何为 asp.net OData webservice 创建批处理端点/在哪里,并设法使项目使用 DefaultODataBatchHandler 进行编译。我仍然无法进行批量请求/响应工作。

我尝试的场景的详细信息:

请求/响应 - 来自 Fiddler 的数据

从 Lightswich 客户端发送的请求:

POST http://localhost:3352/ContainerData.svc/$batch HTTP/1.1
DataServiceVersion: 1.0
Accept: multipart/mixed
Content-Type: multipart/mixed;boundary=batch_730c-bd5b-40de
MaxDataServiceVersion: 3.0
Referer: http://localhost:3352/HTMLClient/
Accept-Language: en-GB
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)
Connection: Keep-Alive
Content-Length: 535
DNT: 1
Host: localhost:3352
Pragma: no-cache


--batch_730c-bd5b-40de
Content-Type: multipart/mixed; boundary=changeset_2952-4c78-0ea1

--changeset_2952-4c78-0ea1
Content-Type: application/http
Content-Transfer-Encoding: binary

POST ServiceProviders HTTP/1.1
Content-ID: 0
Accept: application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, */*;q=0.1
DataServiceVersion: 1.0
Content-Type: application/json
MaxDataServiceVersion: 3.0

{"ID":2,"CategoryID":1,"Name":"fdfdrer","Description":"fefdf"}
--changeset_2952-4c78-0ea1--

--batch_730c-bd5b-40de--

Web API 的响应:

HTTP/1.1 202 Accepted
Cache-Control: no-cache
Content-Length: 2886
Content-Type: multipart/mixed; boundary=batchresponse_663f69c3-6315-447b-b179-00bf407c3b09
Server: Microsoft-IIS/8.0
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcdmd0MWcxMlxEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEyXFByb2plY3RzXE1ha2VZb3VyQ2hvaWNlXE1ha2VZb3VyQ2hvaWNlVUlcTWFrZVlvdXJDaG9pY2VVSVxCaW5cRGVidWdcQ29udGFpbmVyRGF0YS5zdmNcJGJhdGNo?=
X-Powered-By: ASP.NET
Date: Fri, 05 Jul 2013 11:01:49 GMT

--batchresponse_663f69c3-6315-447b-b179-00bf407c3b09
Content-Type: multipart/mixed; boundary=changesetresponse_d1537003-f0cd-4fe2-b4b1-2f562b3179d0

--changesetresponse_d1537003-f0cd-4fe2-b4b1-2f562b3179d0
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 500 Internal Server Error
Content-ID: 0
X-Content-Type-Options: nosniff
Cache-Control: no-cache
DataServiceVersion: 1.0;
Content-Type: application/json;odata=verbose;charset=utf-8

{"error":{"code":"1","message":{"lang":"en-GB","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>One or more of the changes failed to save.  To ensure consistency, refresh and re-apply your changes.\r\nInner exception message:\r\nAn error occurred while processing this request.\r\nInner exception message:\r\n{\"Message\":\"The batch request must have 'multipart/mixed' as the media type.\"}</Message><StackTrace>
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.AstoriaDataServiceDataProvider.&lt;&gt;c__DisplayClass1.&lt;SubmitCore&gt;b__0()\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.AstoriaDataServiceDataProvider.InvokeAstoriaOperation(DataServiceContext context, Action astoriaOperation)\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.AstoriaDataServiceDataProvider.SubmitCore(IEnumerable`1 changes)\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.DataProvider.Submit(IEnumerable`1 changes)\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.PerformPersistCore(IEnumerable`1 eventsChangeSetItems)\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.&lt;&gt;c__DisplayClass46.&lt;PerformPersist&gt;b__45()\r\n
at Microsoft.LightSwitch.Threading.DualDispatcherObject.Mutate(IDispatcher logicDispatcher, MutatorHost host, Action mutator)\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.PerformPersist(IEnumerable`1 eventsChangeSetItems)\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.Microsoft.LightSwitch.ServerGenerated.Implementation.IServerDataServiceImplementationCore.Submit()\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataService`1.&lt;&gt;c__DisplayClasse.&lt;Microsoft.LightSwitch.ServerGenerated.Implementation.IODataService.SaveChanges&gt;b__c()\r\n
at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.InvokeOperationCore[T](String operationName, Object[] args, Boolean invokedFromODataClient, Func`1 invokeOperation, Action catchCallback, Action`2 serializeCustomExceptionInfo)</StackTrace><ErrorInfo /></ExceptionInfo>"}}}
--changesetresponse_d1537003-f0cd-4fe2-b4b1-2f562b3179d0--
--batchresponse_663f69c3-6315-447b-b179-00bf407c3b09--

来自 Web API 的日志:

iisexpress.exe Information: 0 : Request, Method=POST,    Url=http://localhost:3346/odata/$batch, Message='http://localhost:3346/odata/$batch'
iisexpress.exe Information: 0 : Request, Method=POST, Url=http://localhost:3346/odata/ServiceProviders, Message='http://localhost:3346/odata/ServiceProviders'
iisexpress.exe Information: 0 : Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance
iisexpress.exe Information: 0 : Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
iisexpress.exe Information: 0 : Operation=InspectHandler.SendAsync, Status=400 (BadRequest)
iisexpress.exe Information: 0 : Response, Status=400 (BadRequest), Method=POST, Url=http://localhost:3346/odata/ServiceProviders, Message='Content-type='application/json; charset=utf-8', content-length=unknown'
iisexpress.exe Information: 0 : Operation=InspectHandler.SendAsync, Status=202 (Accepted)
iisexpress.exe Information: 0 : Response, Status=202 (Accepted), Method=POST, Url=http://localhost:3346/odata/$batch, Message='Content-type='multipart/mixed; boundary=batchresponse_580061e2-8dc6-4676-b207-1b6ad0ff8847', content-length=unknown'
iisexpress.exe Information: 0 : Operation=JsonMediaTypeFormatter.WriteToStreamAsync
4

0 回答 0