嘿所有,我在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
);
}
显然,问题是涉及自引用关系的扩展预测(页面可以包含其他页面)。这是在使用ADONETDataServices SL3 Update CTP3的SL4或SL3下。
我愿意接受任何工作或指向goo信息的指示,谷歌搜索错误会导致两次点击,这对我来说无法解读特别有帮助。
短错误是“无法将项目添加到集合中。当DataServiceContext跟踪DataServiceCollection中的项目时,在项目加载到集合之前无法添加新项目。”
完整错误是......
System.Reflection.TargetInvocationException 被捕了Message = Exception了 被一个人的目标所抛出 调用。堆栈跟踪: 在System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo) 方法,对象目标,对象[] 参数,SignatureStruct&amp; SIG, MethodAttributes methodAttributes, RuntimeType typeOwner) 在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数, CultureInfo文化,布尔 skipVisibilityChecks) 在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数, 文化信息文化) 在System.Reflection.MethodBase.Invoke(Object obj,Object []参数) 在System.Data.Services.Client.ClientType.ClientProperty.SetValue(Object 实例,对象值,字符串 propertyName,Boolean allowAdd) 在System.Data.Services.Client.AtomMaterializer.ApplyItemsToCollection(AtomEntry) 条目,ClientProperty属性, IEnumerable项目,Uri nextLink, ProjectionPlan continuationPlan) 在System.Data.Services.Client.AtomMaterializer.ApplyFeedToCollection(AtomEntry) 条目,ClientProperty属性, AtomFeed feed,Boolean includeLinks) 在System.Data.Services.Client.AtomMaterializer.MaterializeResolvedEntry(AtomEntry) entry,Boolean includeLinks) 在System.Data.Services.Client.AtomMaterializer.Materialize(AtomEntry entry,Type expectedEntryType,Boolean includeLinks) 在System.Data.Services.Client.AtomMaterializer.DirectMaterializePlan(AtomMaterializer) materializer,AtomEntry条目,类型 expectedEntryType) 在System.Data.Services.Client.AtomMaterializerInvoker.DirectMaterializePlan(Object materializer,对象条目,类型 expectedEntryType) 在System.Data.Services.Client.ProjectionPlan.Run(AtomMaterializer materializer,AtomEntry条目,类型 expectedType) 在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() at System.Data.Services.Client.DataServiceCollection
1.InternalLoadCollection(IEnumerable1 items) at System.Data.Services.Client.DataServiceCollection
1.StartTracking(DataServiceContext context,IEnumerable1 items, String entitySet, Func
2 entityChanged, FUNC2 collectionChanged) at System.Data.Services.Client.DataServiceCollection
1..ctor(DataServiceContext 上下文,IEnumerable1 items, TrackingMode trackingMode, String entitySetName, Func
2 entityChangedCallback,Func2 collectionChangedCallback) at System.Data.Services.Client.DataServiceCollection
1..ctor(IEnumerable1 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
1.InsertItem(Int32 指数,T项) 在System.Collections.ObjectModel.Collection`1.Add(T 项目) 的InnerException:
感谢您的帮助!
答案 0 :(得分:0)
答案 1 :(得分:0)
我认为这里的问题是DataServiceCollection未能将下载的实体添加到顶级CMSPage类型的相应DataServiceCollection属性中。我们做了一些特殊的大小写以确保DataServiceCollection只能在其开始跟踪后使用一些对象。我稍后会对此进行调查,同时尝试使用此代码:
public void LoadAllPagesExpandAll(DataHelperReturnHandler handler, string orderby)
{
DataServiceCollection<CmsPage> data = new DataServiceCollection<CmsPage>( _dataservice );
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.Load(queryResponse);
handler(data);
}
}
catch
{
handler(data);
}
}
);
},
theQuery
);
}