簡體   English   中英

從Silverlight使用WCF數據服務服務運營商(WebGet)異步

[英]Consuming WCF Data Services Service Operator (WebGet) async from Silverlight

嘗試在Silverlight的WCF數據服務中使用簡單的服務運算符時遇到很多問題。 我已經驗證了以下服務運營商正在瀏覽器中進行測試:

[WebGet]
public IQueryable<SecurityRole> GetSecurityRolesForUser(string userName) {
  string currentUsername = HttpContext.Current.User.Identity.Name;

  // if username passed in, verify current user is admin and is getting someone else's permissions
  if (!string.IsNullOrEmpty(userName)) {
    if (!SecurityHelper.IsUserAdministrator(currentUsername))
      throw new DataServiceException(401, Properties.Resources.RequiestDeniedInsufficientPermissions);
  } else // else nothing passed in, so get the current user's permissions
    userName = currentUsername;

  return SecurityHelper.GetUserRoles(userName).AsQueryable<SecurityRole>();
}

然而,無論我如何嘗試使用我在各種在線資源中找到的不同方法,我都無法使用這些數據。 我已經嘗試過在使用DataServiceContext和DataServiceQuery時使用BeginExecute()方法,但是我在EndExecute方法中一直收到錯誤或沒有返回數據。 我必須做一些簡單的錯誤...這是我的SL代碼:

private void InitUserSecurityRoles() {
  MyEntities context = new MyEntities(new Uri("http://localhost:9999/MyService.svc"));
  context.BeginExecute<SecurityRole>(new Uri("http://localhost:9999/MyService.svc/GetSecurityRolesForUser"), OnComplete, context);

  DataServiceQuery<SecurityRole> query = context.CreateQuery<SecurityRole>("GetSecurityRolesForUser");
  query.BeginExecute(OnComplete, query);
}

private void OnComplete(IAsyncResult result) {
  OnDemandEntities context = result.AsyncState as OnDemandEntities;
  var x = context.EndExecute<SecurityRole>(result);
}

有小費嗎? 我現在不知道如何從ODlight服務中正確使用Silverlight中的自定義服務運算符(甚至使用我的單元測試項目進行同步)。 我還通過Fiddler驗證了我正在傳遞正確的身份驗證內容,甚至可以明確設置憑據。 為了安全起見,我甚至從執行安全修整的服務運營商中刪除了邏輯。

由於@kaevanshttp://blogs.msdn.com/b/kaevans )得到了它的工作:

private void InitUserSecurityRoles() {
  DataServiceContext context = new DataServiceContext(new Uri("http://localhost:9999/MyService.svc"));
  context.BeginExecute<SecurityRole>(new Uri("/GetSecurityRolesForUser", UriKind.Relative),
    (result) => {
      SmartDispatcher.BeginInvoke(
        () => {
          var roles = context.EndExecute<SecurityRole>(result);

          UserSecurityRoles = new List<SecurityRole>();
          foreach (var item in roles) {
            UserSecurityRoles.Add(item);
          }
        });
    }, null);
}

我必須創建SmartDispatcher,因為這是在ViewModel中發生的。 否則我可能剛剛使用了靜態Dispatcher.BeginInvoke()。 無法使用各種技術將角色變量直接插入到我的UserSecurityRoles(類型List)中,因此我只是手動添加它(代碼不經常調用,也不是超過10個項目的集合)最大...大多數<5)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM