[英]Consuming WCF service in Console Application
I am New to this site. 我是这个网站的新手。 I am currently working on wcf . 我目前正在wcf上工作。 I am trying to achieve somethig from this hard work but i am facing some problems.I have WCF service running in my MS Visual Stuido 2015 which have local class named AccountBalance and this class got several string propertys like account_number, account_balace etc .I created database in MS Sql server. 我正在尝试通过这项艰苦的工作来实现目标,但遇到了一些问题。我在MS Visual Stuido 2015中运行了WCF服务,该服务具有名为AccountBalance的本地类,并且该类具有一些字符串属性,例如account_number,account_balace等。我创建了数据库在MS Sql服务器中。 Currently i am consuming wcf service in a console Application .I am trying to retrive the record from database in console window by using account number as key. 目前,我正在控制台应用程序中使用wcf服务。我试图通过使用帐号作为键从控制台窗口的数据库中检索记录。 when i enter the account number it should display the rest of record from database in console application but i getting error saying that and i can not run it . 当我输入帐号时,它应该在控制台应用程序中显示数据库中的其余记录,但是我收到错误消息,说我无法运行它。 cannot convert from 'string' to MyService.AccountBalanceRequest' Here is class code which inherits from another base class.. 无法从'string'转换为MyService.AccountBalanceRequest'这是从另一个基类继承的类代码。
[DataContract]
public class AccountBalanceRequest : Current_Account_Details
{
string account_number;
[DataMember]
public string Account_Number
{
get { return account_number; }
set { account_number = value; }
}
}
} }
Here is ADO.NET CODE.. 这是ADO.NET代码。
public bool AccountBalanceCheek(AccountBalanceRequest accountNumber)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM
Current_Account_Details WHERE Account_Number ='" +
accountNumber.Account_Number + "'", conn))
{
cmd.Parameters.AddWithValue("@Account_Number",
accountNumber.Account_Number);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
return true;
}
}
}
Here is Console Application Code.. 这是控制台应用程序代码。
public static void Balance()
{
MyService.HalifaxCurrentAccountServiceClient currentAccount = new MyService.HalifaxCurrentAccountServiceClient("NetTcpBinding_IHalifaxCurrentAccou
ntService");
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);//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();
}
i also tried this way as well. 我也尝试过这种方式。 the code did rise any error but did not give expected result. 该代码确实出现任何错误,但未给出预期结果。 here is the code. 这是代码。
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))
{
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();
}
}
Here is the image for out put. 这是输出图像。 This is image of the out put 这是输出的图像
Right way is: 正确的方法是:
currentAccount.AccountBalanceCheek(cs);
Not: 不:
currentAccount.AccountBalanceCheek(AccountNumber);
Because the type of parameter is MyService.AccountBalanceRequest
, not string
. 因为参数的类型是MyService.AccountBalanceRequest
,而不是string
。
I hope to be helpful for you :) 希望对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.