通过WCF获取返回的bool值

本文关键字:bool 返回 WCF 获取 通过 | 更新日期: 2023-09-27 18:06:53

编辑:我一直得到错误:不能等待无效。我的登录方法是bool,但VS说它是void。我创建了一个新项目,只插入了下面的代码和更新的服务引用-没有帮助。下面是一个WP代码:

private async void Button_Click(object sender, RoutedEventArgs e)
    {
        WCFserviceReference.Service1Client proxy = new WCFserviceReference.Service1Client();
        bool returnStr = await proxy.loginAsync(passTxtBx.Text, loginTxtBx.Text);
        if (returnStr == true)
        {
            PhoneApplicationService.Current.State["UserName"] = loginTxtBx.Text;
            NavigationService.Navigate(new Uri("/Pages/usrPage.xaml", UriKind.RelativeOrAbsolute));
        }
        else MessageBox.Show("Invalid user credentials");
    }

这是我的服务合同:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    bool login(string usn, string pwd);
}

和服务实现:

 public bool login(string usn, string pwd)
        {
            DataClasses1DataContext auth = new DataClasses1DataContext();
            var message = from p in auth.Users
                          where p.usrName == usn && p.usrPass == pwd
                          select p;
            if (message.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

它不是空的,不是吗?那么到底出了什么问题呢?

下面是客户端生成的代理:

public partial class Service1Client : System.ServiceModel.ClientBase<PhoneApp.WCFserviceReference.IService1>, PhoneApp.WCFserviceReference.IService1 {
    private BeginOperationDelegate onBeginloginDelegate;
    private EndOperationDelegate onEndloginDelegate;
    private System.Threading.SendOrPostCallback onloginCompletedDelegate;
    private BeginOperationDelegate onBeginOpenDelegate;
    private EndOperationDelegate onEndOpenDelegate;
    private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
    private BeginOperationDelegate onBeginCloseDelegate;
    private EndOperationDelegate onEndCloseDelegate;
    private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
    public Service1Client() {
    }
    public Service1Client(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }
    public Service1Client(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }
    public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }
    public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

当我将鼠标指向proxy.loginAsync(passTxtBx.Text, loginTxtBx.Text);时,它显示:void Service1Client.loginAsync(string usn, string pwd) (+1 overload(s))

通过WCF获取返回的bool值

更新您的服务引用,当您的代理是最新的,那么这应该工作:

private async void loginBtn_Click(object sender, RoutedEventArgs e)
{
    WCFserviceReference.WCFserviceClient proxy = new WCFserviceReference.WCFserviceClient();
    bool returnStr = await proxy.loginAsync(passTxtBx.Text, loginTxtBx.Text);
    if (returnStr == true)
    {
        PhoneApplicationService.Current.State["UserName"] = loginTxtBx.Text;
        NavigationService.Navigate(new Uri("/Pages/usrPage.xaml", UriKind.RelativeOrAbsolute));
    }
    else MessageBox.Show("Invalid user credentials");
}
编辑:

确保服务器端的方法由[OperationContract]属性装饰:

[OperationContract]
public bool login(string usn, string pwd)
{
    DataClasses1DataContext auth = new DataClasses1DataContext();
    var message = from p in auth.Users
                    where p.usrName == usn && p.usrPass == pwd
                    select p;
    if (message.Count() > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
编辑:

在您的客户端代理中没有login或loginAsync方法。我不知道你是否熟悉webservices的工作流程,所以这里有基本的步骤:

  • 编写服务项目,编译并运行
  • "添加服务引用"到客户端项目使用服务wsdl(服务必须运行)和右键单击项目
  • 每次更改服务,重新编译服务项目,运行它,并在客户端项目中右键单击服务引用并选择"更新服务引用"选项。

之后,您拥有最新的客户端。因此,如果您在服务器端编写了方法login,它也应该出现在客户端。