在数据库中保存数据时,我遇到了很多延迟。我有一个exe(Deskptop应用程序)从串口读取数据并通过Web API服务将该条目推送到数据库,但我的应用程序在这一行上挂起:
httpClient.PostAsync("api/MyController/Save", httpConent).Result;
此exe负责调用我的Web API服务方法并将数据保存到我的数据库。
这是我的代码:
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
// Send data to whom ever interested
if (NewSerialDataRecieved != null)
{
NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}
}
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
//// Fired-off asynchronously; let the current thread continue.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
//data is converted to text
string str = Encoding.ASCII.GetString(e.Data);
if (!string.IsNullOrEmpty(str))
{
CallWebservice(str)
}
}
public void CallWebservice(string xmlRequest)
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("WebService Url");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
StringContent httpConent = new StringContent(xmlRequest, Encoding.UTF8);
HttpResponseMessage responseMessage = null;
try
{
responseMessage = httpClient.PostAsync("api/MyController/Save", httpConent).Result;
}
catch (Exception ex)
{
if (responseMessage == null)
{
responseMessage = new HttpResponseMessage();
}
responseMessage.StatusCode = HttpStatusCode.InternalServerError;
responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex);
}
}
}
我的网络API方法:
public async Task<HttpResponseMessage> Save(HttpRequestMessage request)
{
var requestdata = request.Content.ReadAsStringAsync().Result;//extract Users Id's from this
var users=context.User.Where(t => (t.Stats == userId1) || (t.Stats == userId2)).ToList();
var objUsersMapping= new UsersMapping();
objUsersMapping.Work1 = users[0].Work1;
objUsersMapping.Work2 = users[1].Work1;
await this.SaveUsersMapping(objUsersMapping);
}
public async Task<UsersMapping> SaveUsersMapping(UsersMapping objUsersMapping)
{
using (var context = new MyEntities())
{
try
{
context.UsersMapping.Add(objUsersMapping);
await context.SaveChangesAsync();
return objUsersMapping;
}
catch (Exception foExe)
{
return null;
}
}
}
我还没有在Windows应用程序上工作,所以我不明白为什么我的应用程序挂起。
注意:数据会不断传到我的串口,因此通过网络服务保存数据不应该打扰_serialPort_DataReceived
事件。
答案 0 :(得分:7)
这是我在OP的问题
下面的评论摘要您正在同步调用异步方法。这将导致当前线程阻塞。摆脱.Result
并相应地更改其余代码(例如也包括async
和await
。)
e.g。改变这一行
responseMessage = httpClient.PostAsync("api/MyController/Save", httpConent).Result;
...为:
responseMessage = await httpClient.PostAsync("api/MyController/Save", httpConent);
您的方法签名需要更改如下:
public async Task CallWebservice(string xmlRequest)
{
}
任何调用它的方法也需要async
并使用await
例如_spManager_NewSerialDataRecieved()
方法。
请注意,它已从void
更改为async void
。另请注意await
之前的CallWebservice()
。
async void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
//// Fired-off asynchronously; let the current thread continue.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
//data is converted to text
string str = Encoding.ASCII.GetString(e.Data);
if (!string.IsNullOrEmpty(str))
{
await CallWebservice(str)
}
}
因为上面的方法是一个事件处理程序,所以该方法可以是async void
。通常,您希望避免非事件处理程序代码中的async void
。有关更多信息,请参阅Stephen Cleary先生的this brilliant article。
先生这是唯一的问题吗?
您应该在服务器上修复async Save()
方法,因为它也有.Result()
。这将阻止服务器上的当前线程。用await
作为前缀。通常,您希望避免.Result
作为等待任务完成的方法。在等待它之后,可以安全地使用它作为获取结果的方法,但是有更优雅的方法可以等待并在单行代码中获得结果。例如x = await FooAsync();
。