WCF Proxy fails following call to external API

163 Views Asked by At

I have a Web API controller method that calls an external REST API service using the HttpClient. The results of the external REST call are then passed to a WCF Service via a call to its proxy.

If I call the WCF proxy first and then call the external REST service everything works as expected. If I reverse the order of the calls, the WCF proxy call fails because the InnerChannel (m_inner_channel = m_channel_factory.CreateChannel()) on the proxy is null.

Here is an example for illustration purposes:

 //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);
 }

The code above works, however if I implement the code below the WCF proxy's InnerChannel (m_inner_channel = m_channel_factory.CreateChannel()) is null when I make the call to the service:

 //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);

If I change the order of the calls as shown below it works again:

 //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);

Can anyone help me determine what is going on here? The problem still occurs if I change the ConfigureAwait values to true, so it's not a context switching issue.

There are several Web API methods within this same service that call the WCF proxy above without any issues, the problem only occurs when I call an external service before making a call to the WCF proxy object.

Any help and/or insights would be greatly appreciated.

Thank you, Andrew

1

There are 1 best solutions below

0
On BEST ANSWER

I finally resolved my issue above. I retrieve the ClientChannel from a property called InnerChannel, which uses Monitor.TryEnter to ensure the creation of the channel is performed by a single thread. There was a problem with the creation of this lock, which resulted in the InnerChannel not being instantiated. I rewrote the critical section of the Monitor code to solve the problem.

Thank you for your assistance 1.618.

Andrew