-
我在组织租户azure环境中部署了一个ASP.NET(4.5)网站 .
-
它具有向/从网络位置上载/下载/删除文件(任何类型)的功能(我们称之为nas驱动器),例如\ nas8782 \某某\ ABC \
-
上传/下载/删除工作正常(参见下面的代码) . 我们使用由Azure团队创建的CloudSdk.Azure库 .
-
nasClient.UploadAsync
-
nasClient.DownloadAsync
-
nasClient.DeleteAsync
-
nasClient.ListFilesAsync
FileService.cs
using CloudSdk.Azure;
private string userId = ConfigurationManager.AppSettings["userId"].ToString();
private string userPassword = ConfigurationManager.AppSettings["userPassword"].ToString();
private string baseNasLocation = ConfigurationManager.AppSettings["baseNasLocation"].ToString();
string env = ConfigurationManager.AppSettings["env"].ToString().ToUpper();
public async Task Upload(string fileName, string fileToSave, string projectID)
{
var nasClient = new NasClient(userId, userPassword);
var localFiles = new List<string>();
localFiles.Add(fileToSave);
await nasClient.UploadAsync(baseNasLocation + env + @"\" + projectID + @"\", localFiles);
}
public async Task Download(string fileToDownload, string projectID)
{
var nasClient = new NasClient(userId, userPassword);
Stream stream2 = nasClient.DownloadAsync(baseNasLocation + env + @"\" + projectID + @"\" + fileToDownload).Result;
var appPath2 = HostingEnvironment.MapPath("~\\TempUpload");
var localFullPath2 = string.Format("{0}\\{1}", appPath2, fileToDownload);
using (var fileStream2 = System.IO.File.Create(localFullPath2))
{
stream2.CopyTo(fileStream2);
}
}
public async Task Delete(string fileToDelete, string projectID)
{
var nasClient = new NasClient(userId, userPassword);
Task stream2 = nasClient.DeleteAsync(baseNasLocation + env + @"\" + projectID + @"\" + fileToDelete);
}
我从default.aspx.cs页面调用这些函数
private void Download(string fileName)
{
FileService fObj = new FileService();
var task = Task.Run(async () => { await fObj.Download(fileName, projectId.ToString()); });
task.Wait();
}
private void Delete(string fileName)
{
FileService fObj = new FileService();
var task = Task.Run(async () => { await fObj.Delete(fileName, projectId.ToString()); });
task.Wait();
}
private void Upload(string fileName, string fileToSave)
{
FileService fObj = new FileService();
var task = Task.Run(async () => { await fObj.Upload(fileName, fileToSave, projectId.ToString()); });
task.Wait();
}
ISSUE
- 这个CloudSdk.Azure库有另一个函数调用ListFilesAsync,它获取所提供路径中存在的所有文件的列表 .
我使用下面的代码来做同样但无法得到它 .
如果我调用这个Fileservice方法
没有异步和等待 - 它运行但保持运行并且永远不会返回结果 .
public Task<string> ListFileNames(string projectID)
{
var nasClient = new NasClient(userId, userPassword);
string nasPath = baseNasLocation + env + @"\" + projectID + @"\";
var content = nasClient.ListFilesAsync(nasPath).Result;
//return content;
return JsonConvert.DeserializeObject <Task<string>> (content);
}
使用Async和await - 它抛出编译错误> 'string'不包含'GetAwaiter'的定义,并且没有扩展方法'GetAwaiter'接受类型为'string'的第一个参数(您是否缺少using指令或程序集引用?)
public async Task<string> ListFileNames(string projectID)
{
var nasClient = new NasClient(userId, userPassword);
string nasPath = baseNasLocation + env + @"\" + projectID + @"\";
var content = await nasClient.ListFilesAsync(nasPath).Result;
//return content;
return JsonConvert.DeserializeObject <Task<string>> (content);
}
我如何从aspx.cs页面调用此方法>如何获得结果?
var task = Task.Run(async () => { await fObj.ListFileNames(projectId.ToString()); });
task.Wait();
很抱歉这个问题很长,我只想尽可能多地描述一下 . I am new to this await and async, will really appreciate all the help.
2 回答
以下是您的实施:
在等待呼叫中没有呼叫
Result
没有反序列化为
Task<string>
,它会自动将结果包装在Task<T>
中Result
,Wait
阻止了对Task
的调用,并且没有与Async-Await内联,因为操作是异步进行的 . 在少数情况下,您可以使用GetAwaiter().GetResult()
,但我找不到太多的用例 .异步方法可以具有
Task
或Task<T>
的返回类型,其中结果由设计自动包装 .Async Await的工作方式是真正的基于Async / IO的操作,它将释放调用上下文,而操作则在后台进行,从而提高了系统的可扩展性
您可以使用Task.Result以同步方式获取Task的结果 .
string result = Task.Result;
此外,您不应使用JsonConvert.DeserializeObject来创建任务 . 我不确定你在那里做什么 . 您可以使用允许动态访问的JObject .