我在控制台应用程序中使用wcf服务。我想根据帐号检索帐户信息。当我输入帐号时,它能够方法没有任何错误,但我在控制台应用程序中遇到以下错误。
:'格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数http://tempuri.org/:AccountBalanceCheekResult时出错。 InnerException消息是'反序列化System.Boolean类型的对象时出错。值''无法解析为'Boolean'类型。'。有关详细信息,请参阅InnerException。'
这是班级。
[DataContract]
public class AccountBalanceRequest : Current_Account_Details
{
string account_number;
[DataMember]
public string Account_Number
{
get { return account_number; }
set { account_number = value; }
}
}
}
这是实现,我没有在ADO.NET代码中发现任何错误。
public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
var cmd = new SqlCommand("SELECT * FROM Current_Account_Details WHERE Account_Number = '" + accountNumber.Account_Number + "'", conn);
cmd.CommandType = CommandType.Text;
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
accountNumber.Account_Number = reader["Account_Number"].ToString();
accountNumber.Account_Creation_Date = reader["Account_Creation_Date"].ToString();
accountNumber.Account_Type = reader["Account_Type"].ToString();
accountNumber.Branch_Sort_Code = reader["Branch_Sort_Code"].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);
if (currentAccount.AccountBalanceCheek(cs))**//Error on this line**
{
Console.WriteLine("Your Account Number is :" + cs.Account_Number);
Console.WriteLine("Your Account Type :" + cs.Account_Balance);
Console.WriteLine("Your Account Account Fee :" + cs.Account_Fee);
Console.WriteLine("Your Account Balance:" + cs.Account_Balance);
Console.WriteLine("Your Account Over Draft Limit :" + cs.Over_Draft_Limit);
Console.Write("--------------------------");
Console.ReadLine();
//Console.Clear();
}
}
这是我运行应用程序时的屏幕截图。
答案 0 :(得分:3)
在您的代码中,您有测试
if (currentAccount.AccountBalanceCheek(cs))
这假设AccountBalanceCheek()
函数返回bool,但函数的签名表明它返回AccountBalanceRequest
类型而不是布尔值。
public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
这里有几个选项,调整AccountBalanceCheek
以返回布尔值或调整if函数进行比较,即
if(currentAccount.AccountBalanceCheek(cs) == someExpectedType)
答案 1 :(得分:0)
还有另外一种方法可以处理。
由于您正在从控制台读取帐号,并且读者对象能够找出至少一条记录,这意味着存在具有此类号码/帐号的帐户,否则其他帐号不存在。
bool IsExist=false;
while (reader.Read())
{
//into the loop means at least one record found so
IsExist=true;
accountNumber.Account_Number = reader["Account_Number"].ToString();
}
return IsExist;
还可以从
更改方法的签名public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
到
public bool AccountBalanceCheek(AccountBalanceRequest accountNumber)