Exemple #1
0
        public async Task <ActorResponse> Execute(ActorRequest message, TimeSpan?timeout = null)
        {
            _reqSecondMeter.Tick();

            var actorInstanceKey = string.Join(",", message.ActorInterface, message.ActorId);

            var actorInstance = await ActivateInstance(message.ActorInterface, message.ActorId);

            actorInstance.LastAccess = DateTimeOffset.UtcNow;


            var track = _serviceOptions.TrackActorExecutionDependencyCalls ?
                        _telemetry.Dependency($"actor:{message.ActorInterface}", message.ActorMethodName) : null;

            try
            {
                var result = await actorInstance.Instance.Post(_serializer, message, timeout);

                track?.End(true);

                var response = new ActorResponse()
                {
                    Success  = true,
                    Response = _serializer.Serialize(result),
                    Id       = message.Id
                };

                return(response);
            }
            catch (Exception ex)
            {
                track?.End(false);

                var response = new ActorResponse()
                {
                    Success = false,
                    //Exception = (Exception)ex,
                    Id = message.Id
                };

                _actorInstances.TryRemove(actorInstanceKey, out var instance);
                instance.Instance.Dispose();

                await _actorDirectory.UnregisterActor(instance.ActorTypeName, instance.ActorId, this.StageGuid);

                _telemetry.Exception(ex, "ActorException");

                return(response);
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var act = new Activity("Complex process");

            act.Start();
            telemetry.SetUserContext("testId");
            telemetry.SetOperationContext(act.Id);
            telemetry.Event(new Event
            {
                Name       = "Get data: Inside FirstFunction",
                Context    = req.HttpContext,
                Additional = new Dictionary <string, string>
                {
                    ["ts"] = DateTime.Now.ToString()
                }
            });
            telemetry.StartOperation(act.Id, "Get data");

            HttpClient client = new HttpClient();

            headerService.SetCorellationId(client.DefaultRequestHeaders, act.Id);
            DateTime start    = DateTime.Now;
            var      response = await client.GetAsync("http://localhost:7071/api/SecondFunction");

            var stop = (DateTime.Now - start);

            var dependency = await telemetry.Dependency(new Dependency()
            {
                Duration = stop,
                Name     = "Calling SecondFunction",
                Request  = response.RequestMessage,
                Response = response,
                Data     = await response.Content.ReadAsStringAsync()
            });

            telemetry.StopOperation();
            var data = JsonConvert.DeserializeObject <Data>(dependency.Data);

            data.Content += " Added data from first function.";
            string output = JsonConvert.SerializeObject(data);

            return(new OkObjectResult(output));
        }
        public void InterceptAsynchronous(IInvocation invocation)
        {
            var actor = (IActor)invocation.Proxy;


            //List<byte[]> arguments = new List<byte[]>();
            //foreach (var argument in invocation.Arguments)
            //{
            //    arguments.Add(_serializer.Serialize(argument));
            //}


            //var message = new ActorRequest()
            //{
            //    ActorId = actor.Id,
            //    ActorInterface = $"{invocation.Method.DeclaringType.Namespace}.{invocation.Method.DeclaringType.Name}",
            //    ActorMethodName = invocation.Method.Name,
            //    Arguments = arguments,
            //    FireAndForget=_fireAndForget
            //};

            var message = new LocalActorRequest()
            {
                ActorId         = actor.Id,
                ActorMethod     = invocation.Method,
                ArgumentObjects = invocation.Arguments,
                FireAndForget   = _fireAndForget
            };


            var task = Task.Run(() => {
                var tracker = _serviceOptions.TrackProxyDependencyCalls ?
                              _telemetry.Dependency($"proxy.actor:{message.ActorInterface}", message.ActorMethodName) : null;

                return(_remoteClient.SendActorRequest(message, _timeout)
                       .ContinueWith((t) =>
                {
                    if (t.IsFaulted)
                    {
                        tracker?.End(false);


                        if (t.Exception != null)
                        {
                            throw t.Exception;
                        }
                    }

                    var result = t.Result;
                    if (result != null && !result.Success)
                    {
                        tracker?.End(false);

                        if (result.Exception != null)
                        {
                            throw result.Exception;
                        }
                    }

                    tracker?.End(true);
                }));
            });


            if (_fireAndForget)
            {
                invocation.ReturnValue = Task.CompletedTask;
            }
            else
            {
                invocation.ReturnValue = task;
            }
        }