通过WCF获取返回的bool值

时间:2014-06-05 22:28:53

标签: c# wcf login return

编辑:我一直收到错误:无法等待无效。我的登录方法是bool,但VS说它的空白。我创建了一个新项目,只插入下面的代码和更新的服务参考 - 没有帮助。这是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))

1 个答案:

答案 0 :(得分:0)

更新您的服务参考,并在您的代理是最新的,然后这应该工作:

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;
    }
}

修改

在您的客户端代理中没有登录或loginAsync方法。我不知道您是否熟悉Web服务工作流程,因此有基本步骤:

  • 编写服务项目,编译并运行
  • &#34;添加服务参考&#34;使用服务wsdl(服务必须运行)并右键单击项目
  • 到客户端项目
  • 关于每次服务更改,再次编译服务项目,运行它并在客户端项目中右键单击服务引用并选择&#34;更新服务引用&#34;选项。

之后,您将客户端更新。因此,如果您在服务器端编写了方法登录,它也应该出现在客户端。