SL3/SL4 - 新 DataServiceCollection(queryResponse) 期间 Ado.Net 数据服务错误
大家好,我在 SL 项目 (VS2010) 中有两个函数,它们执行几乎完全相同的操作,但一个函数抛出错误,另一个函数则不抛出错误。这似乎与预测有关,但我不确定解决的最佳方法。
有效的函数是...
public void LoadAllChunksExpandAll(DataHelperReturnHandler handler, string orderby)
{
DataServiceCollection<CmsChunk> data = null;
DataServiceQuery<CmsChunk> theQuery = _dataservice
.CmsChunks
.Expand("CmsItemState")
.AddQueryOption("$orderby", orderby);
theQuery.BeginExecute(
delegate(IAsyncResult asyncResult)
{
_callback_dispatcher.BeginInvoke(
() =>
{
try
{
DataServiceQuery<CmsChunk> query = asyncResult.AsyncState as DataServiceQuery<CmsChunk>;
if (query != null)
{
//create a tracked DataServiceCollection from the result of the asynchronous query.
QueryOperationResponse<CmsChunk> queryResponse =
query.EndExecute(asyncResult) as QueryOperationResponse<CmsChunk>;
data = new DataServiceCollection<CmsChunk>(queryResponse);
handler(data);
}
}
catch
{
handler(data);
}
}
);
},
theQuery
);
}
这将按预期编译并运行。一个非常非常相似的函数(如下所示)失败了......
public void LoadAllPagesExpandAll(DataHelperReturnHandler handler, string orderby)
{
DataServiceCollection<CmsPage> data = null;
DataServiceQuery<CmsPage> theQuery = _dataservice
.CmsPages
.Expand("CmsChildPages")
.Expand("CmsParentPage")
.Expand("CmsItemState")
.AddQueryOption("$orderby", orderby);
theQuery.BeginExecute(
delegate(IAsyncResult asyncResult)
{
_callback_dispatcher.BeginInvoke(
() =>
{
try
{
DataServiceQuery<CmsPage> query = asyncResult.AsyncState as DataServiceQuery<CmsPage>;
if (query != null)
{
//create a tracked DataServiceCollection from the result of the asynchronous query.
QueryOperationResponse<CmsPage> queryResponse = query.EndExecute(asyncResult) as QueryOperationResponse<CmsPage>;
data = new DataServiceCollection<CmsPage>(queryResponse);
handler(data);
}
}
catch
{
handler(data);
}
}
);
},
theQuery
);
}
显然,问题是涉及自引用关系的展开投影(页面可以包含其他页面)。这是在 SL4 或 SL3 下使用 ADONETDataServices SL3 更新 CTP3。
我对任何解决办法或指向粘性信息的指针持开放态度,谷歌搜索错误结果会出现两次点击,这对我来说都不是特别有帮助。
简短的错误是“无法将项目添加到集合中。当 DataServiceCollection 中的项目由 DataServiceContext 跟踪时,在将项目加载到集合中之前无法添加新项目。”
完整的错误是...
System.Reflection.TargetInitationException 被捕获消息=异常有 被目标投掷 调用。堆栈跟踪: 在 System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo 方法、对象目标、对象[] 参数、SignatureStruct&签名, MethodAttributes 方法属性, RuntimeType(类型所有者) 在 System.Reflection.RuntimeMethodInfo.Invoke(对象 obj、BindingFlags invokeAttr、Binder 绑定器,Object[] 参数, CultureInfo 文化,布尔值 跳过可见性检查) 在 System.Reflection.RuntimeMethodInfo.Invoke(对象 obj、BindingFlags invokeAttr、Binder 绑定器,Object[] 参数, 文化信息文化) 在 System.Reflection.MethodBase.Invoke(对象 obj、Object[] 参数) 在 System.Data.Services.Client.ClientType.ClientProperty.SetValue(对象 实例、对象值、字符串 propertyName,布尔值allowAdd) 在 System.Data.Services.Client.AtomMaterializer.ApplyItemsToCollection(AtomEntry 条目、ClientProperty 属性、 IEnumerable items,Uri nextLink, 投影计划延续计划) 在 System.Data.Services.Client.AtomMaterializer.ApplyFeedToCollection(AtomEntry 条目、ClientProperty 属性、 AtomFeed feed,布尔值 includeLinks) 在 System.Data.Services.Client.AtomMaterializer.MaterializeResolvedEntry(AtomEntry 条目,布尔值 includeLinks) 在 System.Data.Services.Client.AtomMaterializer.Materialize(AtomEntry 条目,类型预期条目类型,布尔值 包括链接) 在 System.Data.Services.Client.AtomMaterializer.DirectMaterializePlan(AtomMaterializer 物化器、AtomEntry 条目、类型 预期条目类型) 在 System.Data.Services.Client.AtomMaterializerInvoker.DirectMaterializePlan(对象 物化器、对象输入、类型 预期条目类型) 在 System.Data.Services.Client.ProjectionPlan.Run(AtomMaterializer 物化器、AtomEntry 条目、类型 预期类型) 在 System.Data.Services.Client.AtomMaterializer.Read() 在 System.Data.Services.Client.MaterializeAtom.MoveNextInternal() 在 System.Data.Services.Client.MaterializeAtom.MoveNext() 在 System.Linq.Enumerable.d__b1
1.MoveNext() 在 System.Data.Services.Client.DataServiceCollection
1.InternalLoadCollection(IEnumerable1 项) 在 System.Data.Services.Client.DataServiceCollection
1.StartTracking(DataServiceContext 上下文,IEnumerable1 项,字符串 实体集,功能
2实体更改, Func2 集合已更改) 在 System.Data.Services.Client.DataServiceCollection
1..ctor(DataServiceContext 上下文,IEnumerable1 项, TrackingMode 跟踪模式,字符串 实体集名称,功能
2 entityChangedCallback,Func<代码>2 集合更改回调) 在 System.Data.Services.Client.DataServiceCollection1..ctor(IEnumerable1 项目) 在 Phinli.Dashboard.Silverlight.Helpers.DataHelper.<>c__DisplayClass44.<>c__DisplayClass46.
1.InsertItem(Int32 索引、T 项) 在 System.Collections.ObjectModel.Collection`1.Add(T 物品) 内部异常:b__43() 内部异常: 系统无效操作异常 消息=无法将项目添加到集合中。当物品在 DataServiceCollection 的跟踪方式为 DataServiceContext,新项目 在项目被添加之前无法添加 加载到集合中。 堆栈跟踪: 在 System.Data.Services.Client.DataServiceCollection
感谢您的帮助!
Hey all, I have two functions in a SL project (VS2010) that do almost exactly the same thing, yet one throws an error and the other does not. It seems to be related to the projections, but I am unsure about the best way to resolve.
The function that works is...
public void LoadAllChunksExpandAll(DataHelperReturnHandler handler, string orderby)
{
DataServiceCollection<CmsChunk> data = null;
DataServiceQuery<CmsChunk> theQuery = _dataservice
.CmsChunks
.Expand("CmsItemState")
.AddQueryOption("$orderby", orderby);
theQuery.BeginExecute(
delegate(IAsyncResult asyncResult)
{
_callback_dispatcher.BeginInvoke(
() =>
{
try
{
DataServiceQuery<CmsChunk> query = asyncResult.AsyncState as DataServiceQuery<CmsChunk>;
if (query != null)
{
//create a tracked DataServiceCollection from the result of the asynchronous query.
QueryOperationResponse<CmsChunk> queryResponse =
query.EndExecute(asyncResult) as QueryOperationResponse<CmsChunk>;
data = new DataServiceCollection<CmsChunk>(queryResponse);
handler(data);
}
}
catch
{
handler(data);
}
}
);
},
theQuery
);
}
This compiles and runs as expected. A very, very similar function (shown below) fails...
public void LoadAllPagesExpandAll(DataHelperReturnHandler handler, string orderby)
{
DataServiceCollection<CmsPage> data = null;
DataServiceQuery<CmsPage> theQuery = _dataservice
.CmsPages
.Expand("CmsChildPages")
.Expand("CmsParentPage")
.Expand("CmsItemState")
.AddQueryOption("$orderby", orderby);
theQuery.BeginExecute(
delegate(IAsyncResult asyncResult)
{
_callback_dispatcher.BeginInvoke(
() =>
{
try
{
DataServiceQuery<CmsPage> query = asyncResult.AsyncState as DataServiceQuery<CmsPage>;
if (query != null)
{
//create a tracked DataServiceCollection from the result of the asynchronous query.
QueryOperationResponse<CmsPage> queryResponse = query.EndExecute(asyncResult) as QueryOperationResponse<CmsPage>;
data = new DataServiceCollection<CmsPage>(queryResponse);
handler(data);
}
}
catch
{
handler(data);
}
}
);
},
theQuery
);
}
Clearly the issue is the Expand projections that involve a self referencing relationship (pages can contain other pages). This is under SL4 or SL3 using ADONETDataServices SL3 Update CTP3.
I am open to any work around or pointers to goo information, a Google search for the error results in two hits, neither particularly helpful that I can decipher.
The short error is "An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection."
The full error is...
System.Reflection.TargetInvocationException
was caught Message=Exception has
been thrown by the target of an
invocation. StackTrace:
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo
method, Object target, Object[]
arguments, SignatureStruct& sig,
MethodAttributes methodAttributes,
RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object
obj, BindingFlags invokeAttr, Binder
binder, Object[] parameters,
CultureInfo culture, Boolean
skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object
obj, BindingFlags invokeAttr, Binder
binder, Object[] parameters,
CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object
obj, Object[] parameters)
at System.Data.Services.Client.ClientType.ClientProperty.SetValue(Object
instance, Object value, String
propertyName, Boolean allowAdd)
at System.Data.Services.Client.AtomMaterializer.ApplyItemsToCollection(AtomEntry
entry, ClientProperty property,
IEnumerable items, Uri nextLink,
ProjectionPlan continuationPlan)
at System.Data.Services.Client.AtomMaterializer.ApplyFeedToCollection(AtomEntry
entry, ClientProperty property,
AtomFeed feed, Boolean includeLinks)
at System.Data.Services.Client.AtomMaterializer.MaterializeResolvedEntry(AtomEntry
entry, Boolean includeLinks)
at System.Data.Services.Client.AtomMaterializer.Materialize(AtomEntry
entry, Type expectedEntryType, Boolean
includeLinks)
at System.Data.Services.Client.AtomMaterializer.DirectMaterializePlan(AtomMaterializer
materializer, AtomEntry entry, Type
expectedEntryType)
at System.Data.Services.Client.AtomMaterializerInvoker.DirectMaterializePlan(Object
materializer, Object entry, Type
expectedEntryType)
at System.Data.Services.Client.ProjectionPlan.Run(AtomMaterializer
materializer, AtomEntry entry, Type
expectedType)
at System.Data.Services.Client.AtomMaterializer.Read()
at System.Data.Services.Client.MaterializeAtom.MoveNextInternal()
at System.Data.Services.Client.MaterializeAtom.MoveNext()
at System.Linq.Enumerable.d__b11.MoveNext()
1.InternalLoadCollection(IEnumerable
at System.Data.Services.Client.DataServiceCollection1 items)
1.StartTracking(DataServiceContext
at System.Data.Services.Client.DataServiceCollection
context, IEnumerable1 items, String
2 entityChanged,
entitySet, Func
Func2 collectionChanged)
1..ctor(DataServiceContext
at System.Data.Services.Client.DataServiceCollection
context, IEnumerable1 items,
2
TrackingMode trackingMode, String
entitySetName, Func
entityChangedCallback, Func2
1..ctor(IEnumerable
collectionChangedCallback)
at System.Data.Services.Client.DataServiceCollection1
1.InsertItem(Int32
items)
at Phinli.Dashboard.Silverlight.Helpers.DataHelper.<>c__DisplayClass44.<>c__DisplayClass46.<LoadAllPagesExpandAll>b__43()
InnerException:
System.InvalidOperationException
Message=An item could not be added to the collection. When items in
a DataServiceCollection are tracked by
the DataServiceContext, new items
cannot be added before items have been
loaded into the collection.
StackTrace:
at System.Data.Services.Client.DataServiceCollection
index, T item)
at System.Collections.ObjectModel.Collection`1.Add(T
item)
InnerException:
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来有点像数据服务中的错误。
我将与数据服务团队联系并给您答复。
由于周末,可能需要几天时间。
Alex James
数据服务 PM
This sounds a little like a bug in Data Services.
I'll follow up with the Data Services Team and get back to you with an answer.
Might take a couple of days because of the weekend.
Alex James
Data Services PM
我认为这里的问题是 DataServiceCollection 无法将下载的实体添加到顶级 CMSPage 类型的相应 DataServiceCollection 属性中。我们做了一些特殊的大小写以确保 DataServiceCollection 只能在开始跟踪某些对象后才能使用。我稍后会对此进行调查,同时尝试以下代码:
I think the issue here is that the DataServiceCollection is failing to add the downloaded entities into the corresponding DataServiceCollection properties of the top level CMSPage types.We do some special casing to make sure that the DataServiceCollection can only be used after its started tracking some objects. I'll investigate this later, meanwhile try this code instead :