Example #1
0
 public void Advise(MethodAdviceContext context)
 {
     if (KillExisting && _thread != null && _thread.IsAlive)
         _thread.Abort();
     _thread = new Thread(context.Proceed) { IsBackground = true, Name = context.TargetMethod.Name };
     _thread.Start();
 }
Example #2
0
 public void Advise(MethodAdviceContext call)
 {
     if (_newParameter.HasValue)
         call.Parameters[0] = _newParameter.Value;
     call.Proceed();
     if (_newReturnValue.HasValue)
         call.ReturnValue = _newReturnValue.Value;
 }
Example #3
0
 public void Advise(MethodAdviceContext context)
 {
     LastAdvicesCount = ++AdvicesCount[context];
     if (RandomString[context] == null)
         RandomString[context] = "1";
     else
         RandomString[context] += "0";
     LastRandomString = RandomString[context];
     context.Proceed();
 }
 private async Task<ActivationContext> DoAfter(MethodAdviceContext context, ActivationContext ctx)
 {
     if (context.IsAwaitable())
     {
         ctx = await AfterProcesssingAsync(ctx);
     }
     else
     {
         ctx = AfterProcesssing(ctx);
     }
     return ctx;
 }
Example #5
0
        /// <summary>
        /// Implements advice logic.
        /// Usually, advice must invoke context.Proceed()
        /// </summary>
        /// <param name="context">The method advice context.</param>
        /// <exception cref="System.InvalidOperationException"></exception>
        public void Advise(MethodAdviceContext context)
        {
            var invokedMethod = (MethodInfo)context.TargetMethod;
            var restCall = RestCall.FromMethod(invokedMethod);
            if (restCall == null)
                throw new NotImplementedException();

            // first, replace the parameters
            // (not elegant way)
            var path = restCall.Path;
            var parameters = invokedMethod.GetParameters();
            for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
            {
                var parameter = context.Parameters[parameterIndex];
                var literalParameter = parameter != null ? parameter.ToString() : "";
                var literalParameterPlaceholder = "{" + parameters[parameterIndex].Name + "}";
                path = path.Replace(literalParameterPlaceholder, literalParameter);
            }

            // then create the route and send the request
            var hostAddress = _hostAddress;
            if (hostAddress == null)
            {
                var serviceContractAttribute = context.TargetType.GetCustomAttribute<ServiceContractAttribute>();
                hostAddress = new Uri(serviceContractAttribute.Namespace);
            }
            var target = DnsNameResolver.LocalResolve(hostAddress.Host);
            using (var stream = _route.Connect(target, hostAddress.Port).ToNetworkStream())
            {
                var request = new HttpRequest(restCall.Verb, path)
                    .AddHeader("Host", hostAddress.GetHostAndPort())
                    .AddHeader("Connection", "Close")
                    .AddHeader("Proxy-Connection", "Keep-Alive");
                request.Write(stream);
                var response = HttpResponse.FromStream(stream);
                if (response.StatusCode >= 400)
                {
                    var errorText = response.ReadContentString(stream);
                    throw new InvalidOperationException(errorText);
                }
                // handle return value
                if (invokedMethod.ReturnType != typeof(void))
                {
                    var contentString = response.ReadContentString(stream);
                    using (var contentStream = new MemoryStream(Encoding.UTF8.GetBytes(contentString)))
                    {
                        var serializer = new DataContractJsonSerializer(invokedMethod.ReturnType);
                        var result = serializer.ReadObject(contentStream);
                        context.ReturnValue = result;
                    }
                }
            }
        }
 protected override async Task AdviseAsync(MethodAdviceContext context, Func<Task<object>> proceed)
 {
     try
     {
         await proceed();
     }
     catch (Exception ex)
     {
         ex.Log();
         throw;
     }
 }
 private async Task<ActivationContext> DoBefore(MethodAdviceContext context)
 {
     ActivationContext ctx;
     if (context.IsAwaitable())
     {
         ctx = await BeforeProcessingAsync(new ActivationContext(context));
     }
     else
     {
         ctx = BeforeProcessing(new ActivationContext(context));
     }
     return ctx;
 }
 void IMethodAdvice.Advise(MethodAdviceContext context)
 {
     if (context.IsAwaitable() && context.IsAsync())
         AdviseAsync(context, async () => await context.ProceedAsync1());
     else if (context.IsAwaitable())
         AdviseAsync(context, context.ProceedAsync1);
     else
         AdviseSync(context, () =>
             {
                 context.Proceed();
                 return 1;
             });
 }
        public void Advise(MethodAdviceContext context)
        {
            // some non-sense mocking
            var methodInfo = (MethodInfo)context.TargetMethod;
            var returnType = methodInfo.ReturnType;
            if (returnType != typeof(void))
                context.ReturnValue = Activator.CreateInstance(returnType);

            // now, some advice
            if (NewFirstParameter.HasValue)
                context.Parameters[0] = NewFirstParameter.Value;
            if (NewReturnValue.HasValue)
                context.ReturnValue = NewReturnValue.Value;
        }
 public void Advise(MethodAdviceContext context)
 {
     var locked = Locked[context];
     if (locked)
         return;
     try
     {
         Locked[context] = true;
         context.Proceed();
     }
     finally
     {
         Locked[context] = false;
     }
 }
 protected override async Task AdviseAsync(MethodAdviceContext context, Func<Task<object>> proceed)
 {
     var ctx = await DoBefore(context);
     if (ctx.ExecuteAlternate)
     {
         if (context.HasReturnValue && ctx.SkipTargetProcess)
             context.ReturnValue = await AlternateProceedAsync(ctx);
         else await AlternateProceedAsync(ctx);
     }
     if (!ctx.SkipTargetProcess)
     {
         try
         {
             await Proceed(proceed, ctx);
         }
         catch (Exception ex)
         {
             HandleException(ctx, ex);
             if (!ctx.SwallowException) throw;
         }
     }
     ctx = await DoAfter(context, ctx);
     ctx.Clean();
 }
Example #12
0
 public void Advise(MethodAdviceContext context)
 {
     context.Parameters[0] = (string)context.Parameters[0] + "E";
     context.Proceed();
 }
 public void Advise(MethodAdviceContext context)
 {
     LastStaticAdvicesCount = ++StaticAdvicesCount[context];
     context.Proceed();
 }
Example #14
0
 public void Advise(MethodAdviceContext context)
 {
     // do things you want here
     context.Proceed(); // this calls the original method
     // do other things here
 }
Example #15
0
 /// <summary>
 /// Implements advice logic.
 ///             Usually, advice must invoke context.Proceed()
 /// </summary>
 /// <param name="context">The method advice context.</param>
 public void Advise(MethodAdviceContext context)
 {
     using (var t = TracerFactory.StartTracer(context.TargetType, context.TargetMethod.Name))
     {
         try
         {
             context.Proceed();
         }
         catch (Exception ex)
         {
             if (TreatExceptionAsInformational)
             {
                 t.SetException(ex);
             }
             else
                 t.SetErrorState(ex);
         }
     }
 }
Example #16
0
 public void Advise(MethodAdviceContext context)
 {
     context.Proceed();
 }
Example #17
0
 public void Advise(MethodAdviceContext call)
 {
     Count++;
     call.Proceed();
 }
 protected virtual void AdviseSync(MethodAdviceContext context, Func<object> proceed)
 {
     AdviseAsync(context, () => Task.FromResult(proceed()));
 }
 protected abstract Task AdviseAsync(MethodAdviceContext context, Func<Task<object>> proceed);
Example #20
0
 public void Advise(MethodAdviceContext context)
 {
     var targetType = context.TargetMethod.DeclaringType;
     Assert.IsTrue(targetType.IsInterface);
 }