我一直在寻找代码在内部被调用两次并在数据库中以毫秒时间保存重复记录的原因。两个记录之间的时差是:
EntryDateTime 2017-03-21 08:17:11.957(记录1) 2017-03-21 08:17:11.960(记录2)
这是代码。为简单起见,我已经删除了调试行和验证逻辑,以使其更加清晰。
// Controller
[Route("CreateUser")]
public async Task<IHttpActionResult> PostCreateUser(SignUp parameters)
{
try
{
SignUpRepository repo = new SignUpRepository();
var response = await repo.ResisterUser(parameters);
}
catch (Exception ex)
{
return Ok("Error occurred...." + ex.Message);
}
return Ok("SignUp successfully completed.");
}
// SignUpRepository Class
public async Task<String> ResisterUser(SignUp signUp)
{
SignUpManager manager = new SignUpManager();
var createUser = await CreateUserAsync(signUp, manager);
if (String.IsNullOrWhiteSpace(createUser)) {
return "No user created";
}
return "User successfully created.";
}
private Task<string> CreateUserAsync(SignUp signUp, SignUpManager manager)
{
return Task.Run<string>(() => CreateUserAccount(signUp, manager));
}
private string CreateUserAccount(SignUp signUp, SignUpManager manager)
{
Customer customer = new Customer {
User_Name = signUp.UserName,
Password = signUp.Password,
First_Name = signUp.FirstName,
Last_Name = signUp.LastName,
Email_Address = signUp.Email,
Mobile_Number = signUp.Mobile,
Country_Id = signUp.CountryId
};
return manager.RegisterUser(customer);
}
// SignUpManager Class
public string RegisterUser(Customer customer)
{
return new SQLHelper().RegisterUser(customer);
}
// SQL Helper Class
public string RegisterUser(Customer customer)
{
using (SqlConnection conn = new SqlConnection("Connection String value"))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
// cmd.CommandText = "sp_RegisterUserByThirdParty";
cmd.CommandText = "sp_RegisterUserByLEOSmart";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserName", customer.User_Name));
cmd.Parameters.Add(new SqlParameter("@Password", customer.Password));
cmd.Parameters.Add(new SqlParameter("@Full_Name", String.Format("{0} {1}", customer.First_Name, customer.Last_Name)));
cmd.Parameters.Add(new SqlParameter("@Mobile_Number", customer.Mobile_Number));
cmd.Parameters.Add(new SqlParameter("@Email", customer.Email_Address));
cmd.Parameters.Add(new SqlParameter("@Country_ID", customer.Country_Id));
cmd.Parameters.Add(new SqlParameter("@outToken", SqlDbType.VarChar, 16));
cmd.Parameters["@outToken"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
this.response = Convert.ToString(cmd.Parameters["@outToken"].Value);
}
}
return this.response;
}
存储过程本身有一些验证检查,但奇怪的是所有都是绕过并保存记录。
任何人都可以帮我解释它为什么会发生吗?
由于