public async Task <object> Handler(string name, object[] args, Context context, NextInvokeHandler next)
        {
            await Acquire().ConfigureAwait(false);

            var result = next(name, args, context);

#if !NET35_CF
            await result.ContinueWith((task) => Release(), TaskScheduler.Current).ConfigureAwait(false);
#else
            await result.ContinueWith((task) => Release()).ConfigureAwait(false);
#endif
            return(await result.ConfigureAwait(false));
        }
Beispiel #2
0
        public static async Task <object> Broadcast(string name, object[] args, Context context, NextInvokeHandler next)
        {
            var clientContext = context as ClientContext;
            var uris          = clientContext.Client.Uris;
            var n             = uris.Count;
            var results       = new Task <object> [n];

            for (int i = 0; i < n; ++i)
            {
                var forkingContext = clientContext.Clone() as ClientContext;
                forkingContext.Uri = uris[i];
                results[i]         = next(name, args, forkingContext);
            }
#if NET40
            return(await TaskEx.WhenAll(results).ConfigureAwait(false));
#else
            return(await Task.WhenAll(results).ConfigureAwait(false));
#endif
        }
Beispiel #3
0
        protected async Task <object> Handler(string name, object[] args, Context context, NextInvokeHandler next)
        {
            var serviceContext = context as ServiceContext;
            var from           = "";

            if (serviceContext.RequestHeaders.TryGetValue("id", out var id))
            {
                from = id.ToString();
            }
            switch (name)
            {
            case ">":
            case ">?":
                args[3] = from;
                break;

            case ">*":
                args[2] = from;
                break;
            }
            return(await next(name, args, new BrokerContext(new Producer(this, from), serviceContext)).ConfigureAwait(false));
        }
Beispiel #4
0
        public static async Task <object> Forking(string name, object[] args, Context context, NextInvokeHandler next)
        {
            var deferred      = new TaskCompletionSource <object>();
            var clientContext = context as ClientContext;
            var uris          = clientContext.Client.Uris;
            var n             = uris.Count;
            var count         = n;

            for (int i = 0; i < n; ++i)
            {
                var forkingContext = clientContext.Clone() as ClientContext;
                forkingContext.Uri = uris[i];
                Task result = next(name, args, forkingContext).ContinueWith((task) => {
                    if (task.Exception != null)
                    {
                        if (Interlocked.Decrement(ref count) == 0)
                        {
                            deferred.TrySetException(task.Exception);
                        }
                    }
                    else
                    {
                        deferred.TrySetResult(task.Result);
                    }
                }, TaskScheduler.Current);
            }
            return(await deferred.Task.ConfigureAwait(false));
        }
Beispiel #5
0
        private static async Task <object> TimeoutHandler(string fullname, object[] args, Context context, NextInvokeHandler next)
        {
            var resultTask     = next(fullname, args, context);
            var serviceContext = context as ServiceContext;
            var timeout        = serviceContext.Method.Timeout > TimeSpan.Zero ? serviceContext.Method.Timeout : serviceContext.Service.Timeout;

            if (timeout > TimeSpan.Zero)
            {
                using (CancellationTokenSource source = new CancellationTokenSource()) {
#if NET40
                    var timer = TaskEx.Delay(timeout, source.Token);
                    var task  = await TaskEx.WhenAny(resultTask, timer).ConfigureAwait(false);
#else
                    var timer = Task.Delay(timeout, source.Token);
                    var task  = await Task.WhenAny(resultTask, timer).ConfigureAwait(false);
#endif
                    source.Cancel();
                    if (task == timer)
                    {
                        throw new TimeoutException();
                    }
                }
            }
            return(await resultTask.ConfigureAwait(false));
        }
Beispiel #6
0
        public static async Task <object> InvokeHandler(this Log log, string name, object[] args, Context context, NextInvokeHandler next)
        {
            bool enabled = context.Contains("Log") ? (context as dynamic).Log : log.Enabled;

            if (!enabled)
            {
                return(await next(name, args, context).ConfigureAwait(false));
            }
            string a = "";

            try {
                a = (args.Length > 0) && typeof(Context).IsAssignableFrom(args.Last().GetType()) ? Stringify(new List <object>(args.Take(args.Length - 1))) : Stringify(args);
            }
            catch (Exception e) {
                Trace.TraceError(e.StackTrace);
            }
            try {
                var result = await next(name, args, context).ConfigureAwait(false);

                try {
                    Trace.TraceInformation(name + "(" + a.Substring(1, a.Length - 2) + ") = " + Stringify(result));
                }
                catch (Exception e) {
                    Trace.TraceError(e.StackTrace);
                }
                return(result);
            }
            catch (Exception e) {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
Beispiel #7
0
        protected async Task <object> Handler(string name, object[] args, Context context, NextInvokeHandler next)
        {
            var from = "";

            if (((context as ServiceContext).RequestHeaders).TryGetValue("id", out var id))
            {
                from = id.ToString();
            }
            switch (name)
            {
            case ">":
            case ">?":
                args[3] = from;
                break;

            case ">*":
                args[2] = from;
                break;
            }
            IProducer producer = new Producer(this, from);

            context["producer"] = producer;
            return(await next(name, args, context).ConfigureAwait(false));
        }
Beispiel #8
0
        public async Task <object> InvokeHandler(string name, object[] args, Context context, NextInvokeHandler next)
        {
            var clientContext = new ClientContext {
                Timeout = Timeout
            };

            clientContext.RequestHeaders = context.RequestHeaders;
            var result = await client.InvokeAsync <object>(name, args, clientContext).ConfigureAwait(false);

            context.ResponseHeaders = clientContext.ResponseHeaders;
            return(result);
        }
Beispiel #9
0
 public static Task <object> InvokeHandler(string name, object[] args, Context context, NextInvokeHandler next)
 {
     return(LogExtensions.InvokeHandler(instance, name, args, context, next));
 }
Beispiel #10
0
 public async Task <object> InvokeHandler(string name, object[] args, Context context, NextInvokeHandler next)
 {
     if (MockService == null)
     {
         return(await next(name, args, context).ConfigureAwait(false));
     }
     try {
         return(await next(name, args, context).ConfigureAwait(false));
     }
     catch (BreakerException) {
         return(await MockService.Invoke(name, args, context).ConfigureAwait(false));
     }
 }
        public async Task <object> Handler(string name, object[] args, Context context, NextInvokeHandler next)
        {
            var resultTask     = next(name, args, context);
            var serviceContext = context as ServiceContext;
            var timeout        = Timeout;

            if (serviceContext.Method.Options.ContainsKey("timeout"))
            {
                timeout = (TimeSpan)(serviceContext.Method.Options["timeout"]);
            }
            if (timeout > TimeSpan.Zero)
            {
                using CancellationTokenSource source = new CancellationTokenSource();
#if NET40
                var timer = TaskEx.Delay(timeout, source.Token);
                var task  = await TaskEx.WhenAny(resultTask, timer).ConfigureAwait(false);
#else
                var timer = Task.Delay(timeout, source.Token);
                var task  = await Task.WhenAny(resultTask, timer).ConfigureAwait(false);
#endif
                source.Cancel();
                if (task == timer)
                {
                    throw new TimeoutException();
                }
            }
            return(await resultTask.ConfigureAwait(false));
        }
Beispiel #12
0
        public async Task <object> InvokeHandler(string name, object[] args, Context context, NextInvokeHandler next)
        {
            await Acquire().ConfigureAwait(false);

            return(await next(name, args, context).ConfigureAwait(false));
        }
Beispiel #13
0
        public static async Task <object> InvokeHandler(this Log log, string name, object[] args, Context context, NextInvokeHandler next)
        {
            bool enabled = context.Contains("log") ? (bool)context["log"] : log.Enabled;

            if (!enabled)
            {
                return(await next(name, args, context).ConfigureAwait(false));
            }
            string a = "";

            try {
                a = Stringify(args);
            }
            catch (Exception e) {
#if !NET35_CF
                Trace.TraceError(e.StackTrace);
#else
                Trace.WriteLine(e.StackTrace);
#endif
            }
            try {
                var result = await next(name, args, context).ConfigureAwait(false);

                try {
#if !NET35_CF
                    Trace.TraceInformation(name + "(" + a.Substring(1, a.Length - 2) + ") = " + Stringify(result));
#else
                    Trace.WriteLine(name + "(" + a.Substring(1, a.Length - 2) + ") = " + Stringify(result));
#endif
                }
                catch (Exception e) {
#if !NET35_CF
                    Trace.TraceError(e.StackTrace);
#else
                    Trace.WriteLine(e.StackTrace);
#endif
                }
                return(result);
            }
            catch (Exception e) {
#if !NET35_CF
                Trace.TraceError(e.StackTrace);
#else
                Trace.WriteLine(e.StackTrace);
#endif
                throw;
            }
        }
Beispiel #14
0
        public static async Task <object> Handler(string name, object[] args, Context context, NextInvokeHandler next)
        {
            var result = next(name, args, context);

            if (context.Contains("Oneway"))
            {
                return(null);
            }
            return(await result.ConfigureAwait(false));
        }