WCF-ServiceReference Response提供ArrayOfXElement而不是对象

时间:2017-01-03 00:15:05

标签: c# wcf xaml windows-8.1 service-reference

我正在尝试编写登录应用程序。

我的问题是Service为我提供了ArrayOfXElement而不是对象。 我不知道如何达到这个目标。

以下是代码:

StartPage.xaml.cs

using (...);

namespace MyFirstProject
{
public sealed partial class StartPage : Page
{
    ServiceReference1.Service1Client MyService;

    public StartPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        MyService = new ServiceReference1.Service1Client();
    }

    private void btnLogin_Click(object sender, RoutedEventArgs e)
    {
        ServiceReference1.GetSinglePassCmdResponse h = MyService.GetSinglePassCmdAsync(new ServiceReference1.Pass { nickName = tBoxNick_log.Text }).Result;

现在我认为,我有对象,我可以这样做:

testBlock.Text = "nickname: " + h.nickname + " password: " + h.pass;
    }}}

但我收到的错误是GetSinglePassCmdResponse不包含'昵称'

的定义

IService1.cs

[OperationContract]
    Pass GetSinglePassCmd(Pass Pass);

[DataContract]
public partial class Pass
{
    [DataMember]
    public string nickName { get; set; }
    [DataMember]
    public string password { get; set; }
    [DataMember]
    public Nullable<System.DateTime> lastLogDate { get; set; }

Service1.svc

 public Pass GetSinglePassCmd(Pass Pass)
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlCommand cmd = new SqlCommand("SELECT * FROM Passwords WHERE nickName=@nickName", con);
        cmd.Parameters.AddWithValue("@nickName", Passwords.nickName);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cmd.ExecuteNonQuery();
        con.Close();
        Pass pass = new Pass();
        int i = 0;
        if (ds.Tables[0].Rows.Count > 0)
        {
            //assign dataset values to array
            pass.nickName = ds.Tables[0].Rows[i]["nickName"].ToString();
            pass.password = ds.Tables[0].Rows[i]["password"].ToString();
            pass.lastLogDate = DateTime.Parse(ds.Tables[0].Rows[i]["lastLogDate"].ToString());

        }
        else pass = null;
        return pass;
    }

在MyFirstProject-&gt; ServiceReferences-&gt; ServiceReference1-&gt; Reference.cs-&gt; GetSinglePassCmdResponse我得到了

public partial class GetSinglePassCmdResponse {

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", Order=0)]
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public MyFirstProject.ServiceReference1.ArrayOfXElement GetSinglePassCmdResult;

    public GetSinglePassCmdResponse() {
    }

    public GetSinglePassCmdResponse(MyFirstProject.ServiceReference1.ArrayOfXElement GetSinglePassCmdResult) {
        this.GetSinglePassCmdResult = GetSinglePassCmdResult;
    }
}

有人可以帮我吗??

PS我也试过这个:

testBlock.Text = "nickname: " + h.GetSinglePassCmdResult.nickname + " password: " + h.GetSinglePassCmdResult.pass;

0 个答案:

没有答案