Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 1.85 KB

best-practices-intermediaries.md

File metadata and controls

34 lines (28 loc) · 1.85 KB
description title ms.date ms.assetid
Learn more about: Best Practices: Intermediaries
Best Practices: Intermediaries
03/30/2017
2d41b337-8132-4ac2-bea2-6e9ae2f00f8d

Best Practices: Intermediaries

Care must be taken to handle faults correctly when calling intermediaries to make sure service side channels on the intermediary are closed properly.

Consider the following scenario. A client makes a call to an intermediary which then calls a back-end service. The back-end service defines no fault contract, so any fault thrown from that service will be treated as an un-typed fault. The back-end service throws an xref:System.ApplicationException and WCF correctly aborts the service-side channel. The xref:System.ApplicationException then surfaces as a xref:System.ServiceModel.FaultException that is thrown to the intermediary. The intermediary re-throws the xref:System.ApplicationException. WCF interprets this as an un-typed fault from the intermediary and forwards it on to the client. Upon receiving the fault, both the intermediary and the client fault their client-side channels. The intermediary’s service-side channel however remains open because WCF doesn’t know the fault is fatal.

The best practice in this scenario is to detect if the fault coming from the service is fatal and if so the intermediary should fault its service-side channel as shown in the following code snippet.

catch (Exception e)  
{  
    bool faulted = service.State == CommunicationState.Faulted;  
    service.Abort();  
    if (faulted)  
    {  
        throw new ApplicationException(e.Message);  
    }  
    else  
    {  
        throw;  
    }  
}  

See also