相关疑难解决方法(0)

为所有服务器端代码调用ConfigureAwait的最佳实践

当你有服务器端代码(即一些ApiController)并且你的函数是异步的 - 所以它们返回Task<SomeObject>- 你认为最好的做法是等待你调用的函数ConfigureAwait(false)吗?

我已经读过它更高效,因为它不必将线程上下文切换回原始线程上下文.但是,使用ASP.NET Web Api,如果您的请求是在一个线程上进行的,并且等待某些函数和调用ConfigureAwait(false),则可能会在返回ApiController函数的最终结果时将您置于不同的线程上.

我在下面输入了一个我正在谈论的例子:

public class CustomerController : ApiController
{
    public async Task<Customer> Get(int id)
    {
        // you are on a particular thread here
        var customer = await SomeAsyncFunctionThatGetsCustomer(id).ConfigureAwait(false);

        // now you are on a different thread!  will that cause problems?
        return customer;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# task-parallel-library async-await asp.net-web-api

517
推荐指数
4
解决办法
18万
查看次数

HttpContent.ReadAsAsync在哪里?

我在Web上看到了大量使用新HttpClient对象(作为新Web API的一部分)的示例,应该有HttpContent.ReadAsAsync<T>方法.但是,MSDN没有提到这种方法,IntelliSense也没有找到它.

它去了哪里,我该如何解决它?

.net c# asp.net-web-api httpcontent

257
推荐指数
6
解决办法
15万
查看次数

GetAsync:不返回HttpResponseMessage

应用程序应该从LoginUser()接收httpresponsemessage但它没有响应.

    private void button1_Click(object sender, EventArgs e)
    {
        if (LoginUser(tUser.Text, Password.Text).Result.IsSuccessStatusCode)
        {
            Notifier.Notify("Successfully logged in.. Please wait!");

        }
        else
        {
            Notifier.Notify("Please check your Credential..");
        }            
    }
Run Code Online (Sandbox Code Playgroud)
    public async Task<HttpResponseMessage> LoginUser(string userid, string password)
    {
        string URI = "http://api.danubeco.com/api/userapps/authenticate";

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("c291cmF2OmtheWFs");

            using (var response = await client.GetAsync(String.Format("{0}/{1}/{2}", URI, userid, password)))
            {
                return response;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

请帮忙!

c# asynchronous async-await webapi2 httpresponsemessage

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