我正在将Wcf服务用于Angular JS应用程序,但它根据我的预期不能正常工作。我正在尝试使用Sql事务方法将钱汇入一个帐户到另一个帐户。当我点击角度js应用程序中的提交按钮时,它无法将输入值发布到wcf服务。我把断点放到wcf服务中并且它没有接收任何值。
这是名为Money Transfer ...的本地类......
{
[DataContract]
public class MoneyTransfer
{
string sender_account_no;
string sender_name;
string sender_sort_code;
string amount1;
string transcation_type;
string date;
string receiver_account_no;
string receiver_name;
string receiver_sort_code;
string amount2;
string transcation_type1;
string date1;
[DataMember]
public string Sender_Account_No
{
get { return sender_account_no; }
set { sender_account_no = value; }
}
[DataMember]
public string Sender_Name
{
get { return sender_name; }
set { sender_name = value; }
}
[DataMember]
public string Sender_Sort_Code
{
get { return sender_sort_code; }
set { sender_sort_code = value; }
}
[DataMember]
public string Amount
{
get { return amount1; }
set { amount1 = value; }
}
[DataMember]
public string Transcation_Type
{
get { return transcation_type; }
set { transcation_type = value; }
}
[DataMember]
public string Date
{
get { return date; }
set { date = value; }
}
[DataMember]
public string Receiver_Account_No
{
get { return receiver_account_no; }
set { receiver_account_no = value; }
}
[DataMember]
public string Receiver_Name
{
get { return receiver_name; }
set { receiver_name = value; }
}
[DataMember]
public string Receiver_Sort_Code
{
get { return receiver_sort_code; }
set { receiver_sort_code = value; }
}
[DataMember]
public string Amount1
{
get { return amount2; }
set { amount2 = value; }
}
[DataMember]
public string Transcation_Type1
{
get { return transcation_type1; }
set { transcation_type1 = value; }
}
[DataMember]
public string Date1
{
get { return date1; }
set { date1 = value; }
}
}
}
这是我的界面。
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/MoneyTranfer")]
bool MoneyTranfer(MoneyTransfer mopneyTransfer);
这是我的实施。
public bool MoneyTranfer(MoneyTransfer mopneyTransfer)
{
int amount = System.Convert.ToInt32(mopneyTransfer.Amount);
int amount1 = System.Convert.ToInt32(mopneyTransfer.Amount1);
SqlConnection cn = new SqlConnection(ConnectionString);
string sql = "select Account_Balance from Current_Account_Details where Account_Number='" + mopneyTransfer.Sender_Account_No + "'";
SqlCommand cmd = new SqlCommand(sql, cn);
if (cn.State == ConnectionState.Closed)
cn.Open();
//amount = int.Parse(cmd.ExecuteScalar().ToString());
if (amount > 0)
{
int b;
int b1;
SqlCommand cmd1 = new SqlCommand();
SqlTransaction trans;
if (cn.State == ConnectionState.Closed)
cn.Open();
trans = cn.BeginTransaction();
cmd1.Connection = cn;
cmd1.CommandType = CommandType.Text;
cmd1.Transaction = trans;
cmd1.CommandText = "update Current_Account_Details set Account_Balance=Account_Balance-'" + mopneyTransfer.Amount + "' where Account_Number='" + mopneyTransfer.Sender_Account_No + "'";
b = cmd1.ExecuteNonQuery();
cmd1.CommandText = "update Reward_Account_Details set Account_Balance=Account_Balance+'" + mopneyTransfer.Amount1 + "' where Account_Number='" + mopneyTransfer.Receiver_Account_No + "'";
b1 = cmd1.ExecuteNonQuery();
if (b == 1 && b1 == 1)
{
trans.Commit();
using (SqlConnection con = new SqlConnection(ConnectionString))
{
//Create the SqlCommand object
//Create the SqlCommand object
SqlCommand cmd3 = new SqlCommand("Current_Account_WITHDRAR", con);
//Specify that the SqlCommand is a stored procedure
cmd3.CommandType = System.Data.CommandType.StoredProcedure;
//Add the input parameters to the command object
cmd3.Parameters.AddWithValue("@Account_Number", mopneyTransfer.Sender_Account_No);
cmd3.Parameters.AddWithValue("@Account_Holder_Name", mopneyTransfer.Sender_Name);
cmd3.Parameters.AddWithValue("@Amount", mopneyTransfer.Amount);
cmd3.Parameters.AddWithValue("@Sort_Code", mopneyTransfer.Sender_Sort_Code);
cmd3.Parameters.AddWithValue("@Transcation_Type", mopneyTransfer.Transcation_Type);
cmd3.Parameters.AddWithValue("@Date", mopneyTransfer.Date);
SqlCommand cmd2 = new SqlCommand("Reward_Account_Dposit", con);
//Specify that the SqlCommand is a stored procedure
cmd2.CommandType = System.Data.CommandType.StoredProcedure;
//Add the input parameters to the command object
cmd2.Parameters.AddWithValue("@Account_Number", mopneyTransfer.Receiver_Account_No);
cmd2.Parameters.AddWithValue("@Account_Holder_Name", mopneyTransfer.Receiver_Name);
cmd2.Parameters.AddWithValue("@Amount", mopneyTransfer.Amount1);
cmd2.Parameters.AddWithValue("@Sort_Code", mopneyTransfer.Receiver_Sort_Code);
cmd2.Parameters.AddWithValue("@Transcation_Type", mopneyTransfer.Transcation_Type1);
cmd2.Parameters.AddWithValue("@Date", mopneyTransfer.Date1);
//Open the connection and execute the query
con.Open();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
return true;
//con.Close();
}
}
else
trans.Rollback();
return false;
}
return false;
}
这是脚本代码。
/// <reference path="../angular.min.js" />
var app = angular.module("WebClientModule", [])
.controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) {
$scope.OperType = 1;
//1 Mean New Entry
//To Clear all input controls.
function ClearModels() {
$scope.OperType = 1;
$scope.Sender_Account_No = "";
$scope.Serder_Name = "";
$scope.Sender_Sort_Code = "";
$scope.Amount_to_Send = "";
$scope.Transcation_Type = "";
$scope.Date = "";
$scope.Reciver_Account_No = "";
$scope.Reciver_Name = "";
$scope.Reciver_Sort_Code = "";
$scope.Amount_to_Recive = "";
$scope.Transcation_Type1 = "";
$scope.Date1 = "";
}
$scope.createuser = function () {
var User = {
Sender_Account_No: $scope.Sender_Account_No,
Serder_Name: $scope.Serder_Name,
Sender_Sort_Code: $scope.Sender_Sort_Code,
Amount_to_Send: $scope.Amount_to_Send,
Transcation_Type: $scope.Transcation_Type,
Date:$scope.Date ,
Reciver_Account_No: $scope.Reciver_Account_No,
Reciver_Name: $scope.Reciver_Name,
Reciver_Sort_Code: $scope.Reciver_Sort_Code ,
Amount_to_Recive: $scope.Amount_to_Recive,
Transcation_Type1: $scope.Transcation_Type1,
Date1:$scope.Date1
};
if ($scope.OperType === 1) {
var promisePost = myService.post(User);
promisePost.then(function (pl) {
$scope.Id = pl.data.Id;
$scope.msg = "Operation is successful";
}, function (err) {
$scope.msg = "Operation is failed !";
console.log("Some error Occured" + err);
});
} else {
}
};
}]);
app.service("myService", function ($http) {
//Create new record
this.post = function (User) {
var request = $http({
method: "post",
url: "http://localhost:52098/HalifaxIISService.svc/MoneyTranfer",
data: JSON.stringify(User)
});
return request;
}
})
以下是调试模式的屏幕截图..
这是我运行应用程序时的屏幕截图..
任何反馈或建议都是适当的。感谢