ODataサービス(SharePoint 2010に含まれるOOTBサービス)を呼び出してデータをリストからプルバックするSilverlight5アプリがあります。サイトはWindows認証を使用して保護されています。テストを実行すると、ログインするように求められますが、結果には常に結果セットに返される結果がゼロであると表示されます。
これが奇妙なことです。リストにデータがあることを知っています(そして、ODataリクエストURLを手動でプラグインすると、結果がブラウザーに返されるのがわかります)。テストの実行中にFiddlerを見ると、clientaccesspolicy.xmlに対するいくつかの要求が表示されます(すべて401応答になります)...次にログインすると、clientaccesspolicy.xmlファイルが正常に取得されます。ただし、アプリがクエリを実行して結果がゼロであると言っても、Fiddlerに実際のODataサービスリクエストが表示されません(clientaccesspolicy.xmlへの呼び出しが成功した後は何も表示されません)。
コードは次のようになります。
private DataServiceCollection<InstructorsItem> _dataCollection = new DataServiceCollection<InstructorsItem>();
private Action<IEnumerable<Instructor>> _callbackWithData;
/// <summary>
/// Retrieves a list of instructors from the data service.
/// </summary>
public void GetInstructors(Action<IEnumerable<Instructor>> callback) {
// save callbacks
ResetCallbacks();
_callbackWithData = callback;
// get the instructors
var query = from instructor in IntranetContext.Instructors
select instructor;
// execute query
RunQuery(query);
}
/// <summary>
/// Retrieves instructors from the data source based on the specified query.
/// </summary>
/// <param name="query">Query to execute</param>
private void RunQuery(IQueryable<InstructorsItem> query) {
// clear the collection & register the load completed method
_dataCollection.Clear();
_dataCollection.LoadCompleted += OnLoadDataCompleted;
// fire the load
_dataCollection.LoadAsync(query.Take(5));
}
/// <summary>
/// Handler when the data has been loaded from the service.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void OnLoadDataCompleted(object sender, LoadCompletedEventArgs e) {
// remove the event handler preventing double loads
_dataCollection.LoadCompleted -= OnLoadDataCompleted;
// convert the data to a generic list of objects
var results = _dataCollection.ToList<InstructorsItem>();
// TODO: convert results to local objects
List<Instructor> convertedResults = new List<Instructor>();
foreach (var item in results) {
convertedResults.Add(new Instructor() {
SharePointId = item.Id,
Name = item.Title
});
}
// run the callback
_callbackWithData(convertedResults);
}
そして、これがテストランナーがそれをトリガーしているように見えるものです:
[TestMethod]
[Asynchronous]
[Description("Test loading instructors from the OData Intranet service.")]
public void TestGetInstructors() {
bool asyncCallCompleted = false;
List<Instructor> result = null;
// call data service
_dataService.GetInstructors(asyncResult => {
asyncCallCompleted = true;
result = new List<Instructor>(asyncResult);
});
// run test when call completed
EnqueueConditional(() => asyncCallCompleted);
EnqueueCallback(
() => Assert.IsTrue(result.Count > 0, "Didn't retrieve any instructors."));
EnqueueTestComplete();
}
(1)エラーがないと言っているのに、なぜFiddlerにクエリが表示されないのか、実際にはテストの実行時にエラーがゼロであると表示されているのか、私にはわかりません。