public static R Run <T, R>(CachedChannelFactory <T> factory, TimeSpan?timeout, Func <T, R> methodToCall) { T t = factory.Factory.CreateChannel(); R result; using (IClientChannel clientChannel = (IClientChannel)((object)t)) { if (timeout != null) { clientChannel.OperationTimeout = timeout.Value; } CommunicationState state = clientChannel.State; bool flag = false; if (state != CommunicationState.Created) { if (state != CommunicationState.Closed) { goto IL_51; } } try { clientChannel.Open(); flag = true; } catch { clientChannel.Abort(); throw; } IL_51: bool flag2 = false; try { result = methodToCall(t); } catch (Exception error) { if (WcfUtils.IsChannelException(error)) { flag2 = true; clientChannel.Abort(); } throw; } finally { if (!flag2 && flag) { WcfUtils.CloseChannel(clientChannel); } } } return(result); }
public static void CloseChannel(IClientChannel channel) { if (channel != null) { try { channel.Close(); } catch (Exception error) { channel.Abort(); if (!WcfUtils.IsChannelException(error)) { throw; } } } }