C# 异步web请求在WCF中不起作用
我正在使用第三方API添加日志API调用,我希望异步调用此API,以不影响主API调用的计时,而且此过程的优先级较低(甚至不会使用API结果) 我尝试了以下代码,但对我来说似乎不起作用:C# 异步web请求在WCF中不起作用,c#,.net,wcf,asynchronous,C#,.net,Wcf,Asynchronous,我正在使用第三方API添加日志API调用,我希望异步调用此API,以不影响主API调用的计时,而且此过程的优先级较低(甚至不会使用API结果) 我尝试了以下代码,但对我来说似乎不起作用: string strLogURL = "www.example.com"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(strLogURL); httpWebRequest.ContentType = "application/json"; ht
string strLogURL = "www.example.com";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(strLogURL);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(objAPILog);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
httpWebRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), httpWebRequest);
有人知道如何从WCF异步调用API吗?您可以创建如下方法:
private static T Call<T>(string url, string body, int timeOut = 60)
{
var contentBytes = Encoding.UTF8.GetBytes(body);
var request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = timeOut * 1000;
request.ContentLength = contentBytes.Length;
request.Method = "DELETE";
request.ContentType = @"application/json";
using (var requestWritter = request.GetRequestStream())
requestWritter.Write(contentBytes, 0, (int)request.ContentLength);
var responseString = string.Empty;
var webResponse = (HttpWebResponse)request.GetResponse();
var responseStream = webResponse.GetResponseStream();
using (var reader = new StreamReader(responseStream))
{
reader.BaseStream.ReadTimeout = timeOut * 1000;
responseString = reader.ReadToEnd();
}
return JsonConvert.DeserializeObject<T>(responseString);
}
Task.Run(() => Call<dynamic>("www.example.com", "body"));
private静态T调用(字符串url,字符串正文,int timeOut=60)
{
var contentBytes=Encoding.UTF8.GetBytes(body);
var request=(HttpWebRequest)WebRequest.Create(url);
请求超时=超时*1000;
request.ContentLength=contentBytes.Length;
request.Method=“DELETE”;
request.ContentType=@“应用程序/json”;
使用(var requestwriter=request.GetRequestStream())
Write(contentBytes,0,(int)request.ContentLength);
var responseString=string.Empty;
var webResponse=(HttpWebResponse)request.GetResponse();
var responseStream=webResponse.GetResponseStream();
使用(变量读取器=新的流读取器(responseStream))
{
reader.BaseStream.ReadTimeout=超时*1000;
responseString=reader.ReadToEnd();
}
返回JsonConvert.DeserializeObject(responseString);
}
然后可以像这样异步调用它:
private static T Call<T>(string url, string body, int timeOut = 60)
{
var contentBytes = Encoding.UTF8.GetBytes(body);
var request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = timeOut * 1000;
request.ContentLength = contentBytes.Length;
request.Method = "DELETE";
request.ContentType = @"application/json";
using (var requestWritter = request.GetRequestStream())
requestWritter.Write(contentBytes, 0, (int)request.ContentLength);
var responseString = string.Empty;
var webResponse = (HttpWebResponse)request.GetResponse();
var responseStream = webResponse.GetResponseStream();
using (var reader = new StreamReader(responseStream))
{
reader.BaseStream.ReadTimeout = timeOut * 1000;
responseString = reader.ReadToEnd();
}
return JsonConvert.DeserializeObject<T>(responseString);
}
Task.Run(() => Call<dynamic>("www.example.com", "body"));
Task.Run(()=>调用(“www.example.com”,“body”);
如果异步是您所追求的全部,那么您可以通过使用HttpClient
和async
/wait
来实现这一点。然后,您可以启动并忘记,或者在离开调用方法之前确保它已完成
public void DoSomeWork()
{
PerformWebWork("http://example.com", new object());
// Perform other work
// Forget webWork Task
// Finish
}
public async Task DoSomeWorkAsync()
{
Task webWorkTask = PerformWebWork("http://example.com", new object());
// Perform other work
// Ensure webWorkTask finished
await webWorkTask;
// Finish
}
public async Task PerformWebWork(string url, object objAPILog)
{
string serializedContent = new JavaScriptSerializer().Serialize(objAPILog);
using (HttpClient client = new HttpClient())
{
StringContent content = new StringContent(serializedContent);
HttpResponseMessage postResponse = await client.PostAsync(url, content);
}
}
很高兴我能帮忙。