我正在尝试使用async和await,所以我写了这个简单的代码来感受事物.我正在写一个web api和Post函数调用这个包装的storedprocedure.
public async Task<bool> AddNotificationEntryStoredProcedure(NotificationEntry NewNotification, String ExternalRefID)
{
try
{
Connection = new SqlConnection(ConnectionString);
Connection.Open();
Command = new SqlCommand("InsertNewMessage", Connection);
Command.CommandType = CommandType.StoredProcedure;
Command.Parameters.AddWithValue("@ExternalReferenceID ", ExternalRefID);
Command.Parameters.AddWithValue("@MessageID", NewNotification.id);
Command.Parameters.AddWithValue("@Recievers", NewNotification.To);
Command.Parameters.AddWithValue("@MessageBody", NewNotification.MessageBody);
Command.Parameters.AddWithValue("@DeliveryType", NewNotification.DelieveryType);
Command.Parameters.AddWithValue("@Response", NewNotification.Feedback);
Command.Parameters.AddWithValue("@CreatedOn", DateTime.Now);
//String TodoDuedate = String.Format("{0:yyyy-MM-dd}", todo.DueDate); // convert date format
await Command.ExecuteNonQuery(); /*here is where it complains*/
return true;
}
catch (Exception )
{
//err.ToString();
return false;
}
finally
{
Connection.Close();
}
}// end add function
Run Code Online (Sandbox Code Playgroud)
但我得到错误说"不能等待int".我如何解决这个问题,谢谢你的帮助.
我的web api中有一个post方法,它返回一个字符串,我从客户端调用该方法.我如何获得返回的值.
发布方法
public String Post(Models.SQNotificationDataAccessRepository.NotificationEntry notificationEntry)
{
String externalReferenceID = String.Empty;
if (notificationEntry == null)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
externalReferenceID= dbTransactionLayer.PopulateEsiTable(notification);
return externalReferenceID;
}
Run Code Online (Sandbox Code Playgroud)
客户
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:12819/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Notification notification = new Notification()
{
id = 102,
To = new String[] { "jamesBond@yahoo.com", "cmsds@email.com" },
Title = "Notification WebService Client Test",
MessageBody = "The message body will go there",
DelieveryType = "Email",
Response = true
};
HttpResponseMessage …Run Code Online (Sandbox Code Playgroud)