0

我有一个 Silverlight 5 应用程序,它调用 OData 服务(SharePoint 2010 中包含的 OOTB 服务)以从列表中拉回数据。该站点使用 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 中,实际上它说运行测试时错误为零。

4

1 回答 1

0

如果您在同一台机器上运行服务器和客户端,则没有外部 HTTP 流量,因此 Fiddler 没有什么可以获取的。

于 2012-05-17T10:56:43.883 回答