我有一个带有以下签名的 WCF:
[WebGet]
public bool UserHasRoles(string UserName, string ApplicationName)
{
if (SecurityCheck())
{
UserRightsApplicationEntities context = new UserRightsApplicationEntities();
return context.UserRolesAndFunctions.Where(urf => urf.LogonName == UserName && urf.ApplicationName == ApplicationName).Count() > 0;
}
return false;
}
我可以编译并运行该服务,但是当我调用它时,我得到了错误;“请求 URI 无效。'UserHasRoles' 段不能包含关键谓词或空括号”。
任何帮助,将不胜感激。
这是调用代码:
public static bool UserHasRoles(string applicationName, string userName)
{
DataServiceQuery<bool> q = GetApplicationServicesDataContext()
.CreateQuery<bool>(Constants.ServiceOperations.UserHasRoles)
.AddQueryOption(Constants.ClassProperties.Application.ApplicationName, string.Format("'{0}'", applicationName))
.AddQueryOption(Constants.ClassProperties.User.UserName, string.Format("'{0}'", userName));
bool userHasRoles = q.Execute().FirstOrDefault();
return userHasRoles;
}
这是网络电话:
"http://svr-webdev1:8081/UserRightsApplicationService.svc/UserHasRoles()?ApplicationName='User Rights Management'&UserName='domain\username'"
有趣的是,如果我从 UserHasRoles 中删除括号,例如:
"http://svr-webdev1:8081/UserRightsApplicationService.svc/UserHasRoles?ApplicationName='User Rights Management'&UserName='domain\username'"
它工作正常。
好的,所以我尝试了另一种似乎可行的方法,但不确定为什么:
public static bool DoesUserHaveRoles(string applicationName, string userName)
{
string queryString = string.Format("UserHasRoles?ApplicationName='{0}'&UserName='{1}'", applicationName, userName);
bool doesUserHaveRoles = (GetApplicationServicesDataContext().Execute<bool>(new Uri(queryString, UriKind.Relative))).FirstOrDefault();
return doesUserHaveRoles;
}