异步/等待和同步代码常见吗? [英] Async/Await and synchronous code common?
问题描述
我创建了一个包含每个方法的同步和异步版本的库。每种方法都有很多共同的代码,但方法中的差异是存在await关键字和方法签名。
我想做的是有一种方法,所以我不必在两种方法之间复制代码。我只是不确定如何做到这一点。我已经读过,包装任何一个版本和从另一个版本调用是一个坏主意。
以下是我所谈论的一个例子。我有两种GetDirectoryListing方法 - GetDirectoryListing和GetDirectoryListingAsync。我希望有办法消除重复的代码。
谢谢。
I have created a library that has both synchronous and asynchronous versions of each method. Each method has a lot of common code, but the differences within the methods are the presence of the await keyword and the method signature.
What I would like to do is have a way so that I don't have to replicate code between the two methods. I am just not sure as to how to do this. I have read that wrapping either version and calling from the other one is a bad idea.
Below is an example of what I am talking about. I have two methods for GetDirectoryListing - GetDirectoryListing and GetDirectoryListingAsync. I'd like to have a way to eliminate the duplicated code.
Thanks.
public static List<string> GetDirectoryListing(FTPLoginCredentials credentials)
{
List<string> directoryListing = new List<string>();
FtpWebRequest ftpReq = CreateFtpWebRequest(credentials, WebRequestMethods.Ftp.ListDirectory);
using (FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse())
{
string html;
using (StreamReader sr = new StreamReader(ftpResp.GetResponseStream(), System.Text.Encoding.UTF8))
html = sr.ReadToEnd();
directoryListing.AddRange(html.Split(new string[] { Environment.NewLine, "\r" }, StringSplitOptions.RemoveEmptyEntries));
}
return directoryListing;
}
}
public static async Task<List<string>> GetDirectoryListingAsync(FTPLoginCredentials credentials)
{
List<string> directoryListing = new List<string>();
FtpWebRequest ftpReq = CreateFtpWebRequest(credentials, WebRequestMethods.Ftp.ListDirectory);
using (FtpWebResponse ftpResp = (FtpWebResponse)await ftpReq.GetResponseAsync())
{
string html;
using (StreamReader sr = new StreamReader(ftpResp.GetResponseStream(), System.Text.Encoding.UTF8))
html = await sr.ReadToEndAsync().ConfigureAwait(false);
directoryListing.AddRange(html.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
}
return directoryListing;
}
推荐答案
简单的方法是将阻塞代码实现为异步代码的瘦包装器。< br $> b $ b
The easy way out is to have the blocking code implemented as a thin wrapper around the async code.
public static async Task<list<string>> GetDirectoryListingAsync(FTPLoginCredentials credentials)
{
List<string> directoryListing = new List<string>();
FtpWebRequest ftpReq = CreateFtpWebRequest(credentials, WebRequestMethods.Ftp.ListDirectory);
using (FtpWebResponse ftpResp = (FtpWebResponse)await ftpReq.GetResponseAsync())
{
string html;
using (StreamReader sr = new StreamReader(ftpResp.GetResponseStream(), System.Text.Encoding.UTF8))
html = await sr.ReadToEndAsync().ConfigureAwait(false);
directoryListing.AddRange(html.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));
}
return directoryListing;
}
public static List<string> GetDirectoryListing(FTPLoginCredentials credentials)
{
Task<list<string>> task = GetDirectoryListingAsync(credentials);
task.Wait();
return task.Result;
}
这篇关于异步/等待和同步代码常见吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!