小编Pau*_*ulR的帖子

无法从Task <>隐式转换类型

我试图在.NET 4.5中掌握异步方法语法.我以为我已经明白了究竟例子然而无论异步方法的类型是什么(即Task<T>),我总是得到相同类型的错误错误的转换回T-我明白这是相当多的自动.以下代码生成错误:

无法隐式将类型' System.Threading.Tasks.Task<System.Collections.Generic.List<int>>' 转换为' System.Collections.Generic.List<int>'

public List<int> TestGetMethod()
{
    return GetIdList(); // compiler error on this line
}


async Task<List<int>> GetIdList()
{
    using (HttpClient proxy = new HttpClient())
    {
        string response = await proxy.GetStringAsync("www.test.com");
        List<int> idList = JsonConvert.DeserializeObject<List<int>>();
        return idList;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我显式地转换结果,它也会失败.这个:

public List<int> TestGetMethod()
{
    return (List<int>)GetIdList();  // compiler error on this line
}
Run Code Online (Sandbox Code Playgroud)

有点可以预见会导致此错误:

无法将类型' System.Threading.Tasks.Task<System.Collections.Generic.List<int>>' 转换为' System.Collections.Generic.List<int>'

任何帮助非常感谢.

c# async-await

63
推荐指数
3
解决办法
9万
查看次数

iOS中的ADAL令牌缓存失败

在我的iOS Xamarin应用程序中使用ADAL(Microsoft.IdentityModel.Clients.ActiveDirectory)库进行身份验证时,我"突然"(更新后?)遇到了2个新问题.

我已经尝试将版本恢复到之前的工作状态,或者转向最新的稳定版本,但问题仍然存在.这是我正在使用的代码.

    public async System.Threading.Tasks.Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
    {
        var authContext = new AuthenticationContext(authority);

        if (authContext.TokenCache.ReadItems().Any())
            authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
        var authResult = await authContext.AcquireTokenAsync(resource, clientId, new Uri(returnUri),
            new PlatformParameters(UIApplication.SharedApplication.KeyWindow.RootViewController));
        return authResult;
    }
Run Code Online (Sandbox Code Playgroud)

第一个问题是,当成功验证库时,无法缓存返回的令牌.在调用AcquireTokenAsync()期间,我在Debug窗口中观察到以下输出: -

AcquireTokenHandlerBase.cs: === Token Acquisition started:
    Authority: https://login.windows.net/xxx.onmicrosoft.com/
    Resource: XXXX
    ClientId: XXXX
    CacheType: null
    Authentication Target: User

TokenCache.cs: Looking up cache for a token...
TokenCache.cs: No matching token was found in the cache
TokenCache.cs: Storing token in the cache...
TokenCache.cs: …
Run Code Online (Sandbox Code Playgroud)

xamarin.ios adal

5
推荐指数
0
解决办法
604
查看次数

请求和处理包含文件作为byte []的Web-API响应的最佳方法?

我试图从我的REST API返回一个pdf文件,并将ReportController添加到我的控制器集合中,如下所示.

public class ReportController : ApiController
{
    public HttpResponseMessage Get(int id)
    {
        var result = new HttpResponseMessage(HttpStatusCode.OK);
        string fileName = id.ToString();

        MemoryStream memoryStream = GetStreamFromBlob(fileName);
        result.Content = new ByteArrayContent(memoryStream.ToArray());
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

其他控制器都可以正常工作,但这是第一个设置为返回HttpResponseMessage而不是可序列化对象或对象集合的控制器.

但是我很难从客户端消费这个.已经尝试了许多版本的代码来执行此操作,但是控制器代码永远不会受到影响,并且似乎很少有成功的方法来调用它.以下是我目前的版本: -

public async Task<string> GetPdfFile(int id)
{
    string fileName = string.Format("C:\\Code\\PDF_Client\\{0}.pdf", id);

    using (HttpClient proxy = new HttpClient())
    {
        string url = string.Format("http://localhost:10056/api/report/{0}", id);
        HttpResponseMessage reportResponse = await proxy.GetAsync(url);  //****
        byte[] b = await reportResponse.Content.ReadAsByteArrayAsync();
        System.IO.File.WriteAllBytes(fileName, b);
    }
    return fileName; …
Run Code Online (Sandbox Code Playgroud)

c# azure-storage asp.net-web-api

3
推荐指数
1
解决办法
8300
查看次数