Esempio n. 1
0
 protected async Task InsteadInterceptorAsync(AsyncInterceptionContext context)
 {
     if (context.HasResult)
     {
         context.SetResult(await context.ExecuteBodyAsync());
         await Task.Run(() => OnInterceptorCalled(InterceptionMode.InsteadOfBody, context));
     }
 }
Esempio n. 2
0
        protected async virtual Task ProxyMethodInterceptor(AsyncInterceptionContext context)
        {
#if !SILVERLIGHT
            var oldCallback = CallContext.LogicalGetData(CallbackEnvironment.CallContextKey);
            var oldSession  = CallContext.LogicalGetData(CallbackEnvironment.CallContextSessionIdKey);
            CallContext.LogicalSetData(CallbackEnvironment.CallContextKey, this);
            CallContext.LogicalSetData(CallbackEnvironment.CallContextSessionIdKey, this.SessionId);
#endif
            int retrys = 0;

            try
            {
                while (GetRetryCount() == -1 || retrys <= GetRetryCount())
                {
                    try
                    {
                        if (await EnsureService(context))
                        {
                            var result = await context.ExecuteBodyAsync();

                            context.SetResult(result);
                            return;
                        }
                        else
                        {
                            retrys++;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex is TargetInvocationException)
                        {
                            ex = ex.InnerException;
                        }
                        if (RetryCondition != null && RetryCondition(ex))
                        {
                            retrys++;
                            this.State = ConnectionState.Disconnected;
                        }
                        else
                        {
#if (SILVERLIGHT || NET4)
                            throw ex;
#else
                            ExceptionDispatchInfo.Capture(ex).Throw();
#endif
                        }
                    }
                }
                if (context.HasResult && context.ReturnType.GetGenericArguments().First().IsValueType)
                {
                    context.SetResult(Activator.CreateInstance(context.ReturnType.GetGenericArguments().First()));
                }
            }
            finally
            {
#if !SILVERLIGHT
                CallContext.LogicalSetData(CallbackEnvironment.CallContextSessionIdKey, oldSession);
                CallContext.LogicalSetData(CallbackEnvironment.CallContextKey, oldCallback);
#endif
            }
        }