Exemple #1
0
        public static void Use(UseServiceDelegate <TServiceClient> codeBlock)
        {
            TServiceClient proxy   = null;
            bool           success = false;

            try
            {
                proxy = new TServiceClient();
                codeBlock(proxy);
                proxy.Close();
                success = true;
            }
            catch (Exception ex)
            {
                Common.Logger.Log.Fatal("Service error: " + ex);
                throw;
            }
            finally
            {
                if (!success && proxy != null)
                {
                    proxy.Abort();
                }
            }
        }
Exemple #2
0
    public static void Use(UseServiceDelegate <T> codeBlock)
    {
        T    proxy   = _channelFactory.CreateChannel();
        bool success = false;

        Exception mostRecentEx = null;

        for (int i = 0; i < 5; i++) // Attempt a maximum of 5 times
        {
            try
            {
                codeBlock(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);
        }
    }
 public static void Use(UseServiceDelegate <T> codeBlockDel)
 {
     try
     {
         if (codeBlockDel != null)
         {
             codeBlockDel(BusinessFactory());
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Exemple #4
0
        public static async Task <TResult> UseAsync <TResult>(UseServiceDelegate <TResult> codeBlock)
        {
            return(await Task.Run(() =>
            {
                IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
                bool success = false;
                bool getResultSuccess = false;
                TResult result = default(TResult);

                try
                {
                    result = codeBlock((IImoutoWCFService)proxy);
                    getResultSuccess = true;

                    proxy.Close();
                    success = true;
                }
                catch (Exception ex)
                {
                    try
                    {
                        proxy.Close();
                        success = true;
                    }
                    catch
                    {
                    }

                    if (!getResultSuccess)
                    {
                        throw ex;
                    }
                }
                finally
                {
                    if (!success)
                    {
                        proxy.Abort();
                    }
                }

                return result;
            }));
        }
Exemple #5
0
        public void CallRemoteProcedure(UseServiceDelegate <T> codeBlock)
        {
            var  proxy   = CreateProxy();
            bool success = false;

            try
            {
                codeBlock((T)proxy);
                proxy.Close();
                success = true;
            }
            finally
            {
                if (!success)
                {
                    proxy.Abort();
                }
            }
        }
        public static void Use(UseServiceDelegate <T> codeBlock)
        {
            IClientChannel proxy   = (IClientChannel)_channelFactory.CreateChannel();
            bool           success = false;

            try
            {
                codeBlock((T)proxy);
                proxy.Close();
                success = true;
            }
            finally
            {
                if (!success)
                {
                    proxy.Abort();
                }
            }
        }
Exemple #7
0
        public static async Task UseAsync(UseServiceDelegate codeBlock)
        {
            await Task.Run(() =>
            {
                IClientChannel proxy  = (IClientChannel)_channelFactory.CreateChannel();
                bool success          = false;
                bool getResultSuccess = false;

                try
                {
                    codeBlock((IImoutoWCFService)proxy);
                    getResultSuccess = true;

                    proxy.Close();
                    success = true;
                }
                catch (Exception ex)
                {
                    try
                    {
                        proxy.Close();
                        success = true;
                    }
                    catch
                    {
                    }

                    if (!getResultSuccess)
                    {
                        throw ex;
                    }
                }
                finally
                {
                    if (!success)
                    {
                        proxy.Abort();
                    }
                }
            });
        }
Exemple #8
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;
                }
            }
    /// <summary>
    /// Uses the specified code block.
    /// </summary>
    /// <param name="codeBlock">The code block.</param>
    internal static void Use(UseServiceDelegate <T> codeBlock)
    {
        var factory = GetChannelFactory();
        var proxy   = (IClientChannel)factory.CreateChannel();
        var success = false;

        try
        {
            using (proxy)
            {
                codeBlock((T)proxy);
            }
            success = true;
        }
        finally
        {
            if (!success)
            {
                proxy.Abort();
            }
        }
    }
Exemple #10
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;
     }
 }