我有一个使用 HttpClient 调用外部 REST API 服务的 Web API 控制器方法。然后,外部 REST 调用的结果通过调用其代理传递给 WCF 服务。
如果我先调用 WCF 代理,然后调用外部 REST 服务,一切都会按预期工作。如果我颠倒调用顺序,WCF 代理调用将失败,因为代理上的 InnerChannel (m_inner_channel = m_channel_factory.CreateChannel()) 为空。
这是一个用于说明目的的示例:
//Call to external REST API Service (works)
user = await m_http_client.GetProfileAsync(id).ConfigureAwait(false);
//Call to WCF Service (works)
using (WCFServiceProxy wcf_proxy = new WCFServiceProxy())
{
config = await wcf_proxy.GetConfigAsync(user.ssid).ConfigureAwait(false);
}
上面的代码有效,但是如果我在调用服务时在 WCF 代理的 InnerChannel (m_inner_channel = m_channel_factory.CreateChannel()) 下面实现代码为空:
//Instantiate WCF Proxy - Creates ChannelFactory
WCFServiceProxy wcf_proxy = new WCFServiceProxy()
//Call to external REST Service (works)
user = await m_http_client.GetProfileAsync(id).ConfigureAwait(false);
//Call to WCF Service (InnerChannel is no longer instantiated)
config = await wcf_service.GetConfigAsync(user.ssid).ConfigureAwait(false);
如果我如下所示更改调用顺序,它会再次起作用:
//Instantiate WCF Service
WCFServiceProxy wcf_proxy = new WCFServiceProxy()
//Call to WCF Service (works)
config = await wcf_service.GetConfigAsync("2423432").ConfigureAwait(false);
//Call to external REST Service (works)
user = await m_http_client.GetProfileAsync(id).ConfigureAwait(false);
谁能帮我确定这里发生了什么?如果我将 ConfigureAwait 值更改为 true,问题仍然存在,因此这不是上下文切换问题。
在同一个服务中有几个 Web API 方法调用上述 WCF 代理没有任何问题,只有在调用 WCF 代理对象之前调用外部服务时才会出现问题。
任何帮助和/或见解将不胜感激。
谢谢你,安德鲁