如何调用返回bool的WCF数据服务获取“不能包含键谓词或空括号”。

时间:2012-11-02 21:50:56

标签: wcf odata

我有一个带有以下签名的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;
    }

0 个答案:

没有答案