当你有服务器端代码(即一些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) 这是使用WampSharp的最新预发布版本的非常简单的代码:
var channelFactory = new DefaultWampChannelFactory();
var channel = channelFactory.CreateMsgpackChannel("wss://api.poloniex.com", "realm1");
await channel.Open();
var realmProxy = channel.RealmProxy;
Console.WriteLine("Connection established");
int received = 0;
IDisposable subscription = null;
subscription =
realmProxy.Services.GetSubject("ticker")
.Subscribe(x =>
{
Console.WriteLine("Got Event: " + x);
received++;
if (received > 5)
{
Console.WriteLine("Closing ..");
subscription.Dispose();
}
});
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
但是不起作用,订阅中的代码永远不会运行.也尝试过CreateJsonChannel
,这也不起作用.
什么想法可能是错的?
我有一些看起来像这样的WP7代码:
using System.Windows;
using System.Windows.Threading;
public static class GlobalDispatcher
{
public static Dispatcher Current { get { return Deployment.Current.Dispatcher; } }
}
Run Code Online (Sandbox Code Playgroud)
WinRT中的等价物是什么?是否没有全局可访问的Dispatcher(或CoreDispatcher)对象?