控制台应用程序无法使用Wcf服务检索数据

时间:2017-12-30 02:20:31

标签: c# wcf console-application

我正在将wcf服务用于控制台应用程序。我正在尝试根据帐号检索帐户信息。但问题是当我输入帐号并按Enter键时,它只显示帐号和其余字段为空。

这是基类。

[DataContract]
    public class Current_Account_Details
    { 
    string account_creation_date;
    string account_type;
    string branch_sort_code;
    string account_fees;
    string account_balance;
    string over_draft_limit;
    string  account_holder_id;

    [DataMember]
    public string Account_Creation_Date
    {
        get { return account_creation_date; }
        set { account_creation_date = value; }
    }
    [DataMember]
    public string Account_Type
    {
        get { return account_type; }
        set { account_type = value; }
    }
    [DataMember]
    public string Branch_Sort_Code
    {
        get { return branch_sort_code; }
        set { branch_sort_code = value; }
    }
    [DataMember]
    public string Account_Fees
    {
        get { return account_fees; }
        set { account_fees = value; }

    }
    [DataMember]
    public string Account_Balance
    {
        get { return account_balance; }
        set { account_balance = value; }


    }
    [DataMember]
    public string Over_Draft_Limit
    {

        get { return over_draft_limit; }
        set { over_draft_limit = value; }

    }
    [DataMember]
    public string Account_Holder_Id
    {

        get { return account_holder_id; }
        set { account_holder_id = value; }
    }
}
}

这是继承的类。

 [DataContract]
    public class AccountBalanceRequest : Current_Account_Details
    {
        string account_number;

        [DataMember]
        public string Account_Number
        {
            get { return account_number; }
            set { account_number = value; }
        }
    }
}

这是界面。

 [OperationContract]
        AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber);

这是我的方法。

public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
    {
        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            conn.Open();
            //use top 1 since you are only getting one record.
            //let us use string interpolation, if you are working below C#6
            //replace it with your previous value
            var cmd = new SqlCommand($@"SELECT TOP 1
                                            * 
                                        FROM 
                                            Current_Account_Details 
                                        WHERE 
                                            Account_Number ='{accountNumber.Account_Number}'", conn);
            cmd.CommandType = CommandType.Text;
            //use ExecuteReader to execute sql select
            //ExecuteNonQuery is for update, delete, and insert.
            var reader = cmd.ExecuteReader();
            //read the result of the execute command.
            while (reader.Read())
            {
                //assuming that your property is the same as your table schema. refer to your table schema Current_Account_Details
                //assuming that your datatype are string... just do the conversion...
                accountNumber.Account_Balance = reader["Account_Balance"].ToString();
                accountNumber.Account_Fees = reader["Account_Fees"].ToString();
                accountNumber.Account_Balance = reader["Account_Balance"].ToString();
                accountNumber.Over_Draft_Limit = reader["Over_Draft_Limit"].ToString();
            }
            return accountNumber;
        }

    }

以下是控制台应用程序中的代码。

     public static void Balance()
    {
        MyService.HalifaxCurrentAccountServiceClient currentAccount = new MyService.HalifaxCurrentAccountServiceClient("NetTcpBinding_IHalifaxCurrentAccountService");
        MyService.AccountBalanceRequest cs = new MyService.AccountBalanceRequest();

        string AccountNumber;


        Console.WriteLine("\nEnter your Account Number--------:");
        AccountNumber = Console.ReadLine();
        cs.Account_Number = AccountNumber;
        // MyService.AccountBalanceRequest cs1 = currentAccount.AccountBalanceCheek(AccountNumber);



        MyService.AccountBalanceRequest AccountBalance = currentAccount.AccountBalanceCheek(cs);//error on this line

               Console.WriteLine("Account Number is :" + cs.Account_Number);
                Console.WriteLine("Account creation date  :" + cs.Account_Creation_Date);

                Console.WriteLine("Account Type :" + cs.Account_Type);
                Console.WriteLine("Branch_Sort_Code:" + cs.Branch_Sort_Code);
                Console.WriteLine("Account_Fee:" + cs.Account_Fees);
                Console.WriteLine("Account Account_Balance :" + cs.Account_Balance);
                Console.WriteLine("Account Over Draft Limit :" + cs.Over_Draft_Limit);


            Console.Write("--------------------------");
                Console.ReadLine();
                //Console.Clear();


       }

以下是数据库的屏幕截图。click here to record 以下是我运行应用程序Click here to see the result时的屏幕截图。在此屏幕截图中仅显示帐号,其余字段为空

2 个答案:

答案 0 :(得分:1)

您的 Current_Account_Details 是基类, AccountBalanceRequest 是您发布的问题的派生类。

如果我们有通过继承相关的类,wcf服务通常接受并返回基类型。如果您希望服务接受并返回继承的类型,请使用 KnownType 属性。 所以如果你用契约装饰基类并尝试它就足够了。

[KnownType(typeof(AccountBalanceRequest))]
[DataContract]
    public class Current_Account_Details
    { 
    string account_creation_date;
    string account_type;
    string branch_sort_code;
    string account_fees;
    string account_balance;
    string over_draft_limit;
    string  account_holder_id;

    [DataMember]
    public string Account_Creation_Date
    {
        get { return account_creation_date; }
        set { account_creation_date = value; }
    }
    [DataMember]
    public string Account_Type
    {
        get { return account_type; }
        set { account_type = value; }
    }
    [DataMember]
    public string Branch_Sort_Code
    {
        get { return branch_sort_code; }
        set { branch_sort_code = value; }
    }
    [DataMember]
    public string Account_Fees
    {
        get { return account_fees; }
        set { account_fees = value; }

    }
    [DataMember]
    public string Account_Balance
    {
        get { return account_balance; }
        set { account_balance = value; }


    }
    [DataMember]
    public string Over_Draft_Limit
    {

        get { return over_draft_limit; }
        set { over_draft_limit = value; }

    }
    [DataMember]
    public string Account_Holder_Id
    {

        get { return account_holder_id; }
        set { account_holder_id = value; }
    }
}
}
 public class AccountBalanceRequest : Current_Account_Details
    {
        string account_number;
              public string Account_Number
        {
            get { return account_number; }
            set { account_number = value; }
        }
    }

只需检查您从控制台读取的帐号是否在accountnumber变量中传递。

var cmd = new SqlCommand("SELECT * FROM Current_Account_Details WHERE Account_Number = '" + accountNumber + "'", conn);

编辑1 :您的服务无法检索记录,因为您正在传递AccountBalanceRequest对象,并且对该对象所做的更改不会反映在该方法之外。

 MyService.AccountBalanceRequest cs = new MyService.AccountBalanceRequest();

将其更改为。

     public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
            {
using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                //use top 1 since you are only getting one record.
                //let us use string interpolation, if you are working below C#6
                //replace it with your previous value
                var cmd = new SqlCommand($@"SELECT TOP 1
                                                * 
                                            FROM 
                                                Current_Account_Details 
                                            WHERE 
                                                Account_Number ='{accountNumber.Account_Number}'", conn));
                cmd.CommandType = CommandType.Text;
                //use ExecuteReader to execute sql select
                //ExecuteNonQuery is for update, delete, and insert.
                var reader = cmd.ExecuteReader();
                //read the result of the execute command.
                while(reader.Read())
                {
                    //assuming that your property is the same as your table schema. refer to your table schema Current_Account_Details
                   //assuming that your datatype are string... just do the conversion...
                    accountNumber.Account_Balance = reader["Account_Balance"].ToString();
                    accountNumber.Account_Fee = reader["Account_Fee"].ToString();
                    accountNumber.Account_Balance = reader["Account_Balance"].ToString();
                    accountNumber.Over_Draft_Limit = reader["Over_Draft_Limit"].ToString();
                }
                return accountNumber;
            }
        }

在你的控制台中检索它,

MyService.AccountBalanceRequest AccountBalance =currentAccount.AccountBalanceCheek(cs);
  Console.WriteLine("Your Account Number is :" + cs.Account_Number)
  ...

答案 1 :(得分:0)

我认为您的问题是您正在传递对WCF服务方法的引用并将其更改为而不是返回。文档说:

  

每个操作都有一个返回值和一个参数,即使这些操作无效。但是,与本地方法不同,本地方法可以将对象的引用从一个对象传递到另一个对象,服务操作不会传递对象的引用。相反,他们传递对象的副本

尝试更改代码,以便方法返回更改的对象。在AccountBalanceCheek方法结束时,而不是返回 true 返回 accountNumber

然后在Balance方法中 - 删除if语句并更改为:

....
cs = currentAccount.AccountBalanceCheek(cs);
Console.WriteLine("Your Account Number is :" + cs.Account_Number);
Console.WriteLine("Your Account Type :" + cs.Account_Balance);
....

要详细了解它:Is it possible to pass parameters by reference with WCF