尝试在 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);
}
有小费吗?我现在不知道如何从 OData 服务正确使用 Silverlight 的自定义服务运算符(甚至使用我的单元测试项目同步)。我还通过 Fiddler 验证了我也传递了正确的身份验证内容,甚至明确设置了凭据。为了安全起见,我什至从服务运营商中删除了进行安全调整的逻辑。