WCF代理在调用外部API后失败
本文关键字:API 失败 外部 调用 代理 WCF | 更新日期: 2025-02-19 13:50:44
我有一个Web API控制器方法,该方法使用HttpClient调用外部REST API服务。然后,外部REST调用的结果通过对其代理的调用传递给WCF服务。
如果我先调用WCF代理,然后再调用外部REST服务,那么一切都会按预期进行。如果颠倒调用顺序,WCF代理调用将失败,因为代理上的InnerChannel(m_inner_channel=m_channel_factory.CreateChannel())为null。
以下是一个示例:
//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代理对象之前调用外部服务时才会出现问题。
如有任何帮助和/或见解,我们将不胜感激。
谢谢,Andrew
我终于解决了上面的问题。我从一个名为InnerChannel的属性中检索ClientChannel,该属性使用Monitor。TryEnter以确保通道的创建由单个线程执行。创建此锁时出现问题,导致InnerChannel未被实例化。为了解决这个问题,我重写了Monitor代码的关键部分。
谢谢你的帮助1.618。
Andrew