@@ -131,11 +131,23 @@ protected override void ProcessCompletedTask(AsyncDataSourcePageTaskHolder compl
131131 {
132132 ODataVirtualDataSourceProviderTaskDataHolder h = ( ODataVirtualDataSourceProviderTaskDataHolder ) taskDataHolder ;
133133 IDataSourceSchema schema = null ;
134- IEnumerable < IDictionary < string , object > > result ;
135- Task < IEnumerable < IDictionary < string , object > > > task = ( Task < IEnumerable < IDictionary < string , object > > > ) completedTask . Task ;
134+ IEnumerable < IDictionary < string , object > > result = null ;
135+ int schemaFetchCount = - 1 ;
136+
136137 try
137138 {
138- result = task . Result ;
139+ if ( pageIndex == SchemaRequestIndex )
140+ {
141+ Task < int > task = ( Task < int > ) completedTask . Task ;
142+
143+ schemaFetchCount = task . Result ;
144+ }
145+ else
146+ {
147+ Task < IEnumerable < IDictionary < string , object > > > task = ( Task < IEnumerable < IDictionary < string , object > > > ) completedTask . Task ;
148+
149+ result = task . Result ;
150+ }
139151 }
140152 catch ( AggregateException e )
141153 {
@@ -169,31 +181,52 @@ protected override void ProcessCompletedTask(AsyncDataSourcePageTaskHolder compl
169181 DataSourcePageLoadedCallback pageLoaded ;
170182 lock ( SyncLock )
171183 {
172- var completedAnnotation = h . CompletedAnnotation ;
173- if ( completedAnnotation . Count . HasValue )
184+ if ( schemaFetchCount >= 0 )
174185 {
175- ActualCount = ( int ) completedAnnotation . Count . Value ;
186+ ActualCount = schemaFetchCount ;
176187 }
177-
178- schema = ActualSchema ;
179- if ( schema == null )
188+ else
180189 {
181- schema = ResolveSchema ( result . FirstOrDefault ( ) ) ;
190+ var completedAnnotation = h . CompletedAnnotation ;
191+ if ( completedAnnotation . Count . HasValue )
192+ {
193+ ActualCount = ( int ) completedAnnotation . Count . Value ;
194+ }
182195 }
196+
197+ schema = ActualSchema ;
198+ }
199+
200+ if ( schema == null )
201+ {
202+ schema = ResolveSchema ( ) ;
203+ }
204+
205+ lock ( SyncLock )
206+ {
183207 ActualSchema = schema ;
184208 executionContext = ExecutionContext ;
185209 pageLoaded = PageLoaded ;
186210 }
187211
188- ODataDataSourcePage page = new ODataDataSourcePage ( result , schema , pageIndex ) ;
189- lock ( SyncLock )
212+ ODataDataSourcePage page = null ;
213+
214+ if ( result != null )
190215 {
191- if ( ! IsLastPage ( pageIndex ) && page . Count ( ) > 0 && ! PopulatedActualPageSize )
216+ page = new ODataDataSourcePage ( result , schema , pageIndex ) ;
217+ lock ( SyncLock )
192218 {
193- PopulatedActualPageSize = true ;
194- ActualPageSize = page . Count ( ) ;
219+ if ( ! IsLastPage ( pageIndex ) && page . Count ( ) > 0 && ! PopulatedActualPageSize )
220+ {
221+ PopulatedActualPageSize = true ;
222+ ActualPageSize = page . Count ( ) ;
223+ }
195224 }
196225 }
226+ else
227+ {
228+ page = new ODataDataSourcePage ( null , schema , pageIndex ) ;
229+ }
197230
198231 if ( PageLoaded != null )
199232 {
@@ -225,13 +258,8 @@ protected override void ProcessCompletedTask(AsyncDataSourcePageTaskHolder compl
225258
226259
227260
228- private IDataSourceSchema ResolveSchema ( IDictionary < string , object > item )
261+ private IDataSourceSchema ResolveSchema ( )
229262 {
230- if ( item == null )
231- {
232- return null ;
233- }
234-
235263 var t = this . _client . GetMetadataDocumentAsync ( ) ;
236264 t . Wait ( ) ;
237265 var metadataDocument = t . Result ;
@@ -241,6 +269,7 @@ private IDataSourceSchema ResolveSchema(IDictionary<string, object> item)
241269
242270 private string _filterString = null ;
243271 private string _selectedString = null ;
272+ public const int SchemaRequestIndex = - 1 ;
244273
245274 protected override void MakeTaskForRequest ( AsyncDataSourcePageRequest request , int retryDelay )
246275 {
@@ -315,10 +344,20 @@ protected override void MakeTaskForRequest(AsyncDataSourcePageRequest request, i
315344 }
316345 }
317346
318- Task < IEnumerable < IDictionary < string , object > > > task = client
319- . Skip ( request . Index * actualPageSize )
320- . Top ( actualPageSize )
321- . FindEntriesAsync ( annotations ) ;
347+ Task task ;
348+ if ( request . Index == SchemaRequestIndex )
349+ {
350+ task = client
351+ . Count ( )
352+ . FindScalarAsync < int > ( ) ;
353+ }
354+ else
355+ {
356+ task = client
357+ . Skip ( request . Index * actualPageSize )
358+ . Top ( actualPageSize )
359+ . FindEntriesAsync ( annotations ) ;
360+ }
322361
323362 request . TaskHolder = new AsyncDataSourcePageTaskHolder ( ) ;
324363 request . TaskHolder . Task = task ;
0 commit comments