保存长度较长的字符串(> 100k char)时抛出异常 [英] Exception thrown when saving strings with long length (>100k char)

查看:116
本文介绍了保存长度较长的字符串(> 100k char)时抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体上有一个nvarchar(max)字段。 当我尝试将其设置为特别长的字符串(超过100k字符)时,保存操作将失败,并显示以下错误。 如果我将实体的数据类型更改为varbinary(max)和
将我的长字符串转换为byte [],则保存可以正常工作。 

I have an nvarchar(max) field on my entity.  When I attempt to set this to a particularly long string (over 100k characters) then the save operation fails with the following error.  If I change the data type of the entity to varbinary(max) and convert my long string to a byte[] then the save does work correctly. 

我不会我不希望数据实际上是使用查询字符串发送的,所以我想这是由Uri.EscapeDataString用作转义字符串的辅助方法引起的,但没有意识到它有32k字符的限制。

I wouldn't expect the data to be actually sent using a query string, so I imagine this is caused by the Uri.EscapeDataString being used as a helper method to escape the string, but without realisation that this has a limit of 32k characters.

批处理执行期间此查询出错。 有关详细信息,请参阅内部异常。

无效的URI:Uri字符串太长。

   at System.UriHelper.EscapeString(String input,Int32 start,Int32 end,Char [] dest,Int32& destPos,Boolean isUriString,Char force1,Char force2,Char rsvd)

  在System.Uri.EscapeDataString(String stringToEscape)

   at System.Data.Services.Parsing.WebConvert.TryKeyPrimitiveToString(Object value,String& result)

   at System.Data.Services.WebUtil.GetETagValue(Object resource,ResourceType resourceType,ICollection1 etagProperties,IDataService service,Boolean getMethod)

   at System.Data.Services.WebUtil.CompareAndGetETag(Object parentEntityResource,Object parentEntityToken,ResourceSetWrapper container,IDataService service,Boolean& writeResponseForGetMethods)

   at System.Data.Services.DataService1.CompareETagAndWriteResponse(RequestDescription description,IDataService dataService,IODataResponseMessage responseMessage)

   at System.Data.Services.DataService1.SerializeResponseBody(RequestDescription description,IDataService dataService,IODataResponseMessage responseMessage)

  在System.Data.Services.DataService1.BatchDataService.WriteRequest(RequestDescription description,BatchServiceHost batchHost)

  在System.Data.Services.DataService1.BatchDataService.HandleBatchOperation(IODataRequestMessage operationMessage,ODataBatchWriter batchWriter,Boolean ignoreCUDOperations,Boolean& serviceOperationRequests)

  在System.Data.Services.DataService1.BatchDataService.HandleBatchContent(Stream responseStream)

   at System.UriHelper.EscapeString(String input, Int32 start, Int32 end, Char[] dest, Int32& destPos, Boolean isUriString, Char force1, Char force2, Char rsvd)
   at System.Uri.EscapeDataString(String stringToEscape)
   at System.Data.Services.Parsing.WebConvert.TryKeyPrimitiveToString(Object value, String& result)
   at System.Data.Services.WebUtil.GetETagValue(Object resource, ResourceType resourceType, ICollection1 etagProperties, IDataService service, Boolean getMethod)
   at System.Data.Services.WebUtil.CompareAndGetETag(Object parentEntityResource, Object parentEntityToken, ResourceSetWrapper container, IDataService service, Boolean& writeResponseForGetMethods)
   at System.Data.Services.DataService1.CompareETagAndWriteResponse(RequestDescription description, IDataService dataService, IODataResponseMessage responseMessage)
   at System.Data.Services.DataService1.SerializeResponseBody(RequestDescription description, IDataService dataService, IODataResponseMessage responseMessage)
   at System.Data.Services.DataService1.BatchDataService.WriteRequest(RequestDescription description, BatchServiceHost batchHost)
   at System.Data.Services.DataService1.BatchDataService.HandleBatchOperation(IODataRequestMessage operationMessage, ODataBatchWriter batchWriter, Boolean ignoreCUDOperations, Boolean& serviceOperationRequests)
   at System.Data.Services.DataService1.BatchDataService.HandleBatchContent(Stream responseStream)

推荐答案

问题在于字符串用于并发控制。 用于并发控制的任何属性(默认包含字符串,但不包括二进制blob)将使其值显示在"ETag"中,这是一个HTTP头,
OData用于更改跟踪和并发  ETag的长度限制远低于100k字符:)

The problem is that the string is being used for concurrency control.  Any property being used for concurrency control (and strings are included by default, but not binary blobs) will have its values show up in the "ETag", which is an HTTP header that OData uses for change tracking and concurrency.  The ETag has a length limit well under 100k characters :)

如果您经常需要处理这么大的字符串,您应该在表中添加一个rowversion列并刷新附件LightSwitch中的数据源。 然后LightSwitch将使用rowversion列进行并发控制,并省略ETag生成中的
long值。

If you regularly need to work with strings this large, you should add a rowversion column to your table(s) and refresh the attached datasource(s) in LightSwitch.  LightSwitch will then use the rowversion columns for concurrency control and omit the long values from the ETag generation.


这篇关于保存长度较长的字符串(> 100k char)时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆