示例#1
0
        /// <summary>
        /// 关闭通道连接并释放资源
        /// </summary>
        /// <example>WcfClientProxy.CloseAndDispose((IClientChannel)_IService);</example>
        /// <param name="serviceProxy">代理实例</param>
        public static void CloseAndDispose(IClientChannel serviceProxy)
        {
            if (serviceProxy == null)
            {
                return;
            }

            try
            {
                if (serviceProxy.State == CommunicationState.Opened)
                {
                    serviceProxy.Close();
                }
                serviceProxy.Dispose();
            }
            catch (CommunicationException) { serviceProxy.Abort(); }
            catch (TimeoutException) { serviceProxy.Abort(); }
            catch (Exception)
            {
                serviceProxy.Abort();
                //throw;
                ///todo:未知异常,暂不抛出,考虑记录日志
            }
            finally
            {
                serviceProxy = null;
            }
        }
示例#2
0
 public void unregisterSeller(Uri request, string name)
 {
     if (FlightSearchLogic.Instance.delegates.ContainsKey(name))
     {
         ISellerService tsqs;
         bool           gotValue = FlightSearchLogic.Instance.delegates.TryGetValue(name, out tsqs);
         if (gotValue)
         {
             IClientChannel currChannel = (IClientChannel)tsqs;
             try
             {
                 currChannel.Close();
                 currChannel.Abort();
             }
             catch (Exception)
             {
                 currChannel.Abort();
                 Console.WriteLine("Closing of stale channel {0} failed, ignoring", currChannel.RemoteAddress.Uri.ToString());
             }
             ISellerService victimChannel;
             FlightSearchLogic.Instance.delegates.TryRemove(name, out victimChannel);
             Console.WriteLine("Successfully remove old seller {0} by name", name);
         }
     }
 }
示例#3
0
 public static TResultType CallFunc(Func <TFuncType, TResultType> func, IClientChannel proxy)
 {
     try
     {
         return(func((TFuncType)proxy));
     }
     catch (CommunicationException ex)
     {
         proxy.Abort();
         throw ex;
     }
     catch (TimeoutException ex)
     {
         proxy.Abort();
         throw ex;
     }
     catch (Exception ex)
     {
         proxy.Abort();
         throw ex;
     }
     finally
     {
         if (proxy.State == CommunicationState.Opened)
         {
             proxy.Close();
         }
     }
 }
示例#4
0
 public static void CallAction(Action <TActionType> action, IClientChannel proxy)
 {
     try
     {
         action((TActionType)proxy);
     }
     catch (CommunicationException ex)
     {
         proxy.Abort();
         throw ex;
     }
     catch (TimeoutException ex)
     {
         proxy.Abort();
         throw ex;
     }
     catch (Exception ex)
     {
         proxy.Abort();
         throw ex;
     }
     finally
     {
         if (proxy.State == CommunicationState.Opened)
         {
             proxy.Close();
         }
     }
 }
        /// <summary>
        /// Executes the specified action.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="action">The action.</param>
        /// <returns>the result</returns>
        public TResult Execute <TResult>(Func <T, TResult> action)
        {
            IClientChannel clientChannel = (IClientChannel)ChannelFactory.CreateChannel();
            TResult        result        = default(TResult);

            try
            {
                result = action((T)clientChannel);
                clientChannel.Close();
            }
            catch (FaultException e)
            {
                Console.WriteLine(e.Message);
                clientChannel.Abort();
            }
            catch (CommunicationException e)
            {
            }
            catch (Exception e)
            {
                clientChannel.Abort();
            }
            finally
            {
            }
            return(result);
        }
    public static void Use(UseServiceDelegate <T> codeBlock)
    {
        IClientChannel proxy   = (IClientChannel)_channelFactory.CreateChannel();
        bool           success = false;

        Exception mostRecentEx = null;

        for (int i = 0; i < 5; i++) // Attempt a maximum of 5 times
        {
            try
            {
                codeBlock((T)proxy);
                proxy.Close();
                success = true;
                break;
            }
            // The following is typically thrown on the client when a channel is terminated due to the server closing the connection.
            catch (ChannelTerminatedException cte)
            {
                mostRecentEx = cte;
                proxy.Abort();
                //  delay (backoff) and retry
                Thread.Sleep(1000 * (i + 1));
            }

            // The following is thrown when a remote endpoint could not be found or reached.  The endpoint may not be found or
            // reachable because the remote endpoint is down, the remote endpoint is unreachable, or because the remote network is unreachable.
            catch (EndpointNotFoundException enfe)
            {
                mostRecentEx = enfe;
                proxy.Abort();
                //  delay (backoff) and retry
                Thread.Sleep(1000 * (i + 1));
            }

            // The following exception that is thrown when a server is too busy to accept a message.
            catch (ServerTooBusyException stbe)
            {
                mostRecentEx = stbe;
                proxy.Abort();
                //  delay (backoff) and retry
                Thread.Sleep(1000 * (i + 1));
            }

            catch (Exception)
            {
                // rethrow any other exception not defined here
                // You may want to define a custom Exception class to pass information such as failure count, and failure type
                proxy.Abort();
                throw;
            }
        }
        if (mostRecentEx != null)
        {
            proxy.Abort();
            throw new Exception("WCF call failed after 5 retries.", mostRecentEx);
        }
    }
示例#7
0
        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);
        }
示例#8
0
        private void RemoveSellerIfExists(Uri request, string name)
        {
            if (FlightSearchLogic.Instance.delegates.ContainsKey(name))
            {
                ISellerService tsqs;
                bool           gotValue = FlightSearchLogic.Instance.delegates.TryGetValue(name, out tsqs);
                if (gotValue)
                {
                    IClientChannel currChannel = (IClientChannel)tsqs;
                    try
                    {
                        currChannel.Close();
                        currChannel.Abort();
                    }
                    catch (Exception)
                    {
                        currChannel.Abort();
                        Console.WriteLine("Closing of stale channel {0} failed, ignoring", currChannel.RemoteAddress.Uri.ToString());
                    }
                    ISellerService victimChannel;
                    FlightSearchLogic.Instance.delegates.TryRemove(name, out victimChannel);
                    Console.WriteLine("Successfully remove old seller {0} by name", name);
                }
            }


            foreach (var seller in FlightSearchLogic.Instance.delegates)
            {
                IClientChannel currChannel = ((IClientChannel)seller.Value);

                if (currChannel.RemoteAddress.Uri.Equals(request))
                {
                    Console.WriteLine("Detected connection retry by {0} from {1} ,removing old connection", name, request.ToString());
                    // Close this channel
                    try
                    {
                        currChannel.Close();
                        currChannel.Abort();
                    }
                    catch (Exception)
                    {
                        currChannel.Abort();
                        Console.WriteLine("Closing of stale channel {0} failed, ignoring", currChannel.RemoteAddress.Uri.ToString());
                    }
                    ISellerService victimChannel;
                    FlightSearchLogic.Instance.delegates.TryRemove(seller.Key, out victimChannel);
                }
            }
        }
 public T Function<T>(Func<T> func)
 {
     try
     {
         var result = func();
         _clientChannel.Close();
         return result;
     }
     catch (Exception ex)
     {
         KTrace.Error(ex);
         _clientChannel.Abort();
         throw;
     }
 }
示例#10
0
 /// <summary>
 /// Invoke method implementation
 /// </summary>
 private T Invoke <T>(string operationName, string uri, ExecuteOptions options, InvokeDelegate <T> operation)
 {
     using (new SPMonitoredScope("Invoke :" + operationName + "_" + uri))
     {
         string ep = FindLoadBalancerEndPoint(uri);
         if (!string.IsNullOrEmpty(ep))
         {
             Uri xu = new Uri(ep);
             IIdentityServiceContract identityapplication = GetChannel(xu, options);
             IClientChannel           clientChannel       = identityapplication as IClientChannel;
             try
             {
                 operation(identityapplication);
                 clientChannel.Close();
             }
             finally
             {
                 if (clientChannel.State != CommunicationState.Closed)
                 {
                     clientChannel.Abort();
                 }
             }
         }
     }
     return(default(T));
 }
示例#11
0
        public removeMerchantAliasResult RemoveMerchantAlias(removeMerchantAliasRequest request)
        {
            IClientChannel channel = (IClientChannel)_createMerchantAliasFactory.CreateChannel();

            bool success = false;
            removeMerchantAliasResult result = null;

            try
            {
                removeMerchantAliasResponse response = ((MerchantAliasWSRemove)channel).removeMerchantAlias(new removeMerchantAliasRequest1(request));
                channel.Close();

                result = response.@return;

                success = true;
            }
            finally
            {
                if (!success)
                {
                    channel.Abort();
                }
            }
            return(result);
        }
示例#12
0
        public createMerchantAliasResult CreateMerchantAlias(createMerchantAliasRequest request)
        {
            IClientChannel channel = (IClientChannel)_createMerchantAliasFactory.CreateChannel();

            bool success = false;
            createMerchantAliasResult result = null;

            try
            {
                createMerchantAliasResponse response = null;
                using (OperationContextScope scope = new OperationContextScope(channel))
                {
                    OperationContext.Current.OutgoingMessageHeaders.ReplyTo = new EndpointAddress(_config.AsyncServiceEndpoint);
                    response = ((MerchantAliasWSCreate)channel).createMerchantAlias(new createMerchantAliasRequest1(request));
                    channel.Close();
                }

                result = response.@return;

                success = true;
            }
            finally
            {
                if (!success)
                {
                    channel.Abort();
                }
            }
            return(result);
        }
示例#13
0
 public bool EndWhisper(IAsyncResult result)
 {
     if (this.proxy != null)
     {
         try
         {
             return(this.proxy.EndAsyncWhisper(result));
         }
         catch (Exception ex)
         {
             if (this.Logger != null)
             {
                 this.Logger.Fatal("Fail to EndWhisper", ex);
             }
             IClientChannel clientChannel = this.proxy as IClientChannel;
             if (clientChannel != null)
             {
                 CommunicationState state = clientChannel.State;
                 if (clientChannel.State == CommunicationState.Faulted)
                 {
                     clientChannel.Abort();
                 }
                 return(false);
             }
             return(false);
         }
     }
     if (this.Logger != null)
     {
         this.Logger.Fatal("No Service is connected");
     }
     return(false);
 }
示例#14
0
        void InvokeEnd(Task response, IClientChannel channel, MethodCallMessageWrapper methodCallWrapper)
        {
            Exception  exception = null;
            MethodInfo method    = methodCallWrapper.MethodBase as MethodInfo;

            try
            {
                FlowContextForTest(methodCallWrapper);

                if (response.IsFaulted)
                {
                    exception = EvaluateException(response.Exception);
                    throw exception;
                }
            }
            finally
            {
                OnPostInvoke(method, exception);
                if (channel.State != CommunicationState.Closed && channel.State != CommunicationState.Faulted)
                {
                    try
                    {
                        channel.Close();
                    }
                    catch
                    {
                        channel.Abort();
                    }
                }
                channel = null;
            }
        }
示例#15
0
        public static async Task <TResult> ExecuteAsync <TResult>(Func <T, Task <TResult> > action)
        {
            IClientChannel clientChannel = (IClientChannel)ChannelFactory.CreateChannel();

            bool success = false;
            TaskCompletionSource <TResult> taskCompletionSource = new TaskCompletionSource <TResult>();

            try
            {
                taskCompletionSource.TrySetResult(await action((T)clientChannel));
                clientChannel.Close();
                success = true;
            }
            catch (Exception ex)
            {
                taskCompletionSource.TrySetException(ex);
            }
            finally
            {
                if (!success)
                {
                    clientChannel.Abort();
                }
            }
            return(await taskCompletionSource.Task);
        }
示例#16
0
        // Token: 0x06000010 RID: 16 RVA: 0x00002588 File Offset: 0x00000788
        public static void Use(Action <T> codeBlock, string RemoteIP)
        {
            IClientChannel clientChannel = (IClientChannel)((object)new ChannelFactory <T>(GenericService <T> .binding).CreateChannel(new EndpointAddress(string.Format("http://{0}/{1}{2}{3}", new object[]
            {
                RemoteIP,
                "IRemo",
                "te",
                "Panel"
            }))));
            bool flag = false;

            try
            {
                codeBlock((T)((object)clientChannel));
                clientChannel.Close();
                flag = true;
            }
            finally
            {
                if (!flag)
                {
                    clientChannel.Abort();
                }
            }
        }
示例#17
0
    public static void Using <TServiceContract>(Action <TServiceContract> action, string endpointConfigurationName) where TServiceContract : class
    {
        ChannelFactoryCacheKey            cacheKey       = new ChannelFactoryCacheKey(typeof(TServiceContract), endpointConfigurationName);
        ChannelFactory <TServiceContract> channelFactory = (ChannelFactory <TServiceContract>)SvcHelper.ChannelFactories.GetOrAdd(
            cacheKey,
            cacheKey => new ChannelFactory <TServiceContract>(cacheKey.EndpointConfigurationName));
        TServiceContract typedChannel  = channelFactory.CreateChannel();
        IClientChannel   clientChannel = (IClientChannel)typedChannel;

        try
        {
            using (new OperationContextScope((IContextChannel)typedChannel))
            {
                action(typedChannel);
            }
        }
        finally
        {
            try
            {
                clientChannel.Close();
            }
            catch
            {
                clientChannel.Abort();
            }
        }
    }
示例#18
0
        public static void Using <T>(Action <T> action)
        {
            BasicHttpBinding   bindnig     = new BasicHttpBinding();
            string             endpointUri = GetServiceEndpoint(typeof(T));
            ChannelFactory <T> factory     = new ChannelFactory <T>(bindnig, new EndpointAddress(new Uri(endpointUri)));

            //ChannelFactory<T> factory = new ChannelFactory<T>("*");

            T client = factory.CreateChannel();

            try
            {
                action(client);
                ((IClientChannel)client).Close();
                factory.Close();
            }
            catch (Exception ex)
            {
                IClientChannel clientInstance = ((IClientChannel)client);
                if (clientInstance.State == System.ServiceModel.CommunicationState.Faulted)

                {
                    clientInstance.Abort();
                    factory.Abort();
                }
                else if (clientInstance.State != System.ServiceModel.CommunicationState.Closed)
                {
                    clientInstance.Close();
                    factory.Close();
                }
                throw (ex);
            }
        }
示例#19
0
        public async Task <byte[]> ExecuteWcfProcess(byte[] message)
        {
            IService       proxy = null;
            IClientChannel ch    = null;

            byte[] result;
            try
            {
                proxy = _factory.CreateChannel();
                ch    = (IClientChannel)proxy;
                ch.Open();
                result = await proxy.ExecuteWcfProcess(message);

                ch.Close();
            }
            catch (Exception)
            {
                if (ch != null)
                {
                    if (ch.State == CommunicationState.Faulted)
                    {
                        ch.Abort();
                    }
                    else
                    {
                        ch.Close();
                    }
                }

                throw;
            }

            return(result);
        }
        /// <summary>
        /// Callback function will be used when fault happened using the channel.
        /// </summary>
        /// <param name="sender">Specify the callback event sender.</param>
        /// <param name="e">Specify the event argument.</param>
        private void Channel_Faulted(object sender, EventArgs e)
        {
            IClientChannel channel = sender as IClientChannel;

            channel.Abort();
            ((IDisposable)channel).Dispose();
        }
示例#21
0
 public void SendMemberInfo(long guildKey, string sender, bool isOnline)
 {
     if (this.proxy != null)
     {
         try
         {
             this.proxy.MemberInfo(guildKey, sender, isOnline);
             return;
         }
         catch (Exception ex)
         {
             if (this.Logger != null)
             {
                 this.Logger.Error("Fail to close connection", ex);
             }
             IClientChannel clientChannel = this.proxy as IClientChannel;
             if (clientChannel.State == CommunicationState.Faulted)
             {
                 clientChannel.Abort();
             }
             return;
         }
     }
     if (this.Logger != null)
     {
         this.Logger.ErrorFormat("Fail to sendMemberInfo {0} {1} {2}", guildKey, sender, isOnline);
     }
 }
示例#22
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources
 /// </summary>
 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 protected override void Dispose(bool disposing)
 {
     if (disposing == true)
     {
         IClientChannel proxyChannel = ProxyInstance as IClientChannel;
         try
         {
             if (proxyChannel != null)
             {
                 if (proxyChannel.State != CommunicationState.Faulted)
                 {
                     proxyChannel.Close();
                 }
                 else
                 {
                     proxyChannel.Abort();
                 }
             }
         }
         catch (CommunicationException)
         {
             if (proxyChannel != null)
             {
                 proxyChannel.Abort();
             }
         }
         catch (TimeoutException)
         {
             if (proxyChannel != null)
             {
                 proxyChannel.Abort();
             }
         }
         catch (Exception)
         {
             if (proxyChannel != null)
             {
                 proxyChannel.Abort();
             }
             throw;
         }
         finally
         {
             ProxyInstance = default(T);
         }
     }
 }
示例#23
0
 public static void CloseClientChannel(IClientChannel c)
 {
     try {
         c.Close();
     } catch {
         c.Abort();
     } finally {
         c.Dispose();
     }
 }
示例#24
0
 public static void CleanUp(this IClientChannel proxy)
 {
     try
     {
         proxy.Close();
     }
     catch
     {
         proxy.Abort();
         throw;
     }
 }
示例#25
0
 public void UseAsyncWithReturnValue(UseServiceDelegateWithAsyncReturn <T> action, string endPoint, object obj)
 {
     try
     {
         _proxy = GetChannelFactory(endPoint).CreateChannel() as IClientChannel;
         if (_proxy != null)
         {
             _actionWithAsyncReturn = action;
             new Thread(() =>
             {
                 action((T)_proxy, obj);
                 _proxy.Close();
             }).Start();
         }
     }
     catch (CommunicationException communicationException)
     {
         if (_proxy != null)
         {
             _proxy.Abort();
         }
         throw communicationException;
     }
     catch (TimeoutException timeoutException)
     {
         if (_proxy != null)
         {
             _proxy.Abort();
         }
         throw timeoutException;
     }
     catch (Exception ex)
     {
         if (_proxy != null)
         {
             _proxy.Abort();
         }
         throw ex;
     }
 }
示例#26
0
        private void ExceptionOccured(string message, Exception e)
        {
            if (this.Logger != null)
            {
                this.Logger.Error(message, e);
            }
            IClientChannel clientChannel = this.proxy as IClientChannel;

            if (clientChannel.State == CommunicationState.Faulted)
            {
                clientChannel.Abort();
            }
        }
示例#27
0
        private void Close()
        {
            try
            {
                _channel.Faulted -= ChannelFaultedHandler;
                _channel.Closed -= ChannelClosedExternallyHandler;

                if (_channel.State == CommunicationState.Opened)
                    _channel.Close();
                else
                    _channel.Abort();
            }
            catch (Exception ex)
            {
                Logger.Logger.Instance.Debug(ex, "WCF channel close exception.");
                _channel.Abort();
            }
            finally
            {
                OnClosed();
            }
        }
示例#28
0
            public void Use(UseServiceDelegate <T> action, string endPoint)
            {
                try
                {
                    _proxy = GetChannelFactory(endPoint).CreateChannel() as IClientChannel;
                    if (_proxy != null)
                    {
                        _proxy.Open();
                        action((T)_proxy);

                        _proxy.Close();
                    }
                }
                catch (CommunicationException communicationException)
                {
                    if (_proxy != null)
                    {
                        _proxy.Abort();
                    }

                    throw communicationException;
                }
                catch (TimeoutException timeoutException)
                {
                    if (_proxy != null)
                    {
                        _proxy.Abort();
                    }
                    throw timeoutException;
                }
                catch (Exception ex)
                {
                    if (_proxy != null)
                    {
                        _proxy.Abort();
                    }
                    throw ex;
                }
            }
示例#29
0
 public void UseAsync(UseServiceDelegate <T> action, string endPoint, AsyncCallback callBack, object obj)
 {
     try
     {
         _proxy = GetChannelFactory(endPoint).CreateChannel() as IClientChannel;
         if (_proxy != null)
         {
             _proxy.Open();
             _callBack = callBack;
             _action   = action;
             IAsyncResult result = _action.BeginInvoke((T)_proxy, AsyncResult, obj);
         }
     }
     catch (CommunicationException communicationException)
     {
         if (_proxy != null)
         {
             _proxy.Abort();
         }
         throw communicationException;
     }
     catch (TimeoutException timeoutException)
     {
         if (_proxy != null)
         {
             _proxy.Abort();
         }
         throw timeoutException;
     }
     catch (Exception ex)
     {
         if (_proxy != null)
         {
             _proxy.Abort();
         }
         throw ex;
     }
 }
示例#30
0
        private void ChannelFaulted(object sender, EventArgs e)
        {
            IClientChannel channel = (IClientChannel)sender;

            try
            {
                channel.Close();
            }
            catch
            {
                channel.Abort();
            }
            throw new ApplicationException("Exc_ChannelFailure");
        }
示例#31
0
        static void Main(string[] args)
        {
            using (var channelFactory = new ChannelFactory <IHelloWorldService>(
                       new NetNamedPipeBinding(),
                       new EndpointAddress("net.pipe://localhost/my-services/hello-service")))
            {
                while (true)
                {
                    IClientChannel channel = null;
                    try
                    {
                        var helloWorldService = channelFactory.CreateChannel();
                        helloWorldService.HelloWorld();
                        channel = (IClientChannel)helloWorldService;
                        channel.Close();
                    }
                    catch (EndpointNotFoundException)
                    {
                    }
                    catch (CommunicationException)
                    {
                        channel.Abort();
                    }
                    catch (TimeoutException)
                    {
                        channel.Abort();
                    }
                    catch (Exception)
                    {
                        channel.Abort();
                        throw;
                    }

                    Console.ReadLine();
                }
            }
        }
示例#32
0
 private static void CloseChannel(IClientChannel channel)
 {
     try
     {
         channel.Close();
     }
     catch (TimeoutException)
     {
         channel.Abort();
     }
     catch (CommunicationException)
     {
         channel.Abort();
     }
     catch (Exception)
     {
         channel.Abort();
         throw;
     }
 }