コード例 #1
0
 private static object[] GetParametersByName(ServiceMethod serviceMethod, IDictionary args)
 {
     return(serviceMethod.ServiceInterfaceMethod
            .GetParameters()
            .Select(p => JsonHelper.ConvertWeaklyTypedValue(args[p.Name], p.ParameterType))
            .ToArray());
 }
コード例 #2
0
        private async Task <string> GetResponse(HttpListenerContext context, ServiceMethod serviceMethod, HttpServiceRequest requestData)
        {
            var taskType   = serviceMethod.ServiceInterfaceMethod.ReturnType;
            var resultType = taskType.IsGenericType ? taskType.GetGenericArguments().First() : null;
            var arguments  = requestData.Target.IsWeaklyTyped ? GetParametersByName(serviceMethod, requestData.Arguments) : requestData.Arguments.Values.Cast <object>().ToArray();
            var settings   = requestData.Target.IsWeaklyTyped ? JsonSettingsWeak : JsonSettings;

            var invocationResult = await Activator.Invoke(serviceMethod, arguments);

            string response = JsonConvert.SerializeObject(invocationResult.Result, resultType, settings);

            context.Response.Headers.Add(HSHttpHeaders.ExecutionTime, invocationResult.ExecutionTime.ToString());

            return(response);
        }
コード例 #3
0
        public async Task <InvocationResult> Invoke(ServiceMethod serviceMethod, object[] arguments)
        {
            var invokeTarget = GetInvokeTarget(serviceMethod);

            object[] argumentsWithDefaults = GetConvertedAndDefaultArguments(serviceMethod.ServiceInterfaceMethod, arguments);

            var sw   = Stopwatch.StartNew();
            var task = (Task)serviceMethod.ServiceInterfaceMethod.Invoke(invokeTarget, argumentsWithDefaults);
            await task.ConfigureAwait(false);

            var executionTime = sw.Elapsed;
            var result        = task.GetType().GetProperty("Result").GetValue(task);

            return(new InvocationResult {
                Result = result, ExecutionTime = executionTime
            });
        }
コード例 #4
0
        public void TryPublish(HttpServiceRequest requestData, Exception ex, ServiceMethod serviceMethod,
                               double requestTime)
        {
            var callEvent = _eventPublisher.CreateEvent();

            callEvent.CalledServiceName = serviceMethod?.GrainInterfaceType.Name;
            callEvent.ClientMetadata    = requestData.TracingData;
            callEvent.ServiceMethod     = requestData.Target?.MethodName;

            var metaData  = _serviceEndPointDefinition.GetMetaData(serviceMethod);
            var arguments = (requestData.Arguments ?? new OrderedDictionary()).Cast <DictionaryEntry>();

            callEvent.Params = arguments.SelectMany(_ => ExtractParamValues(_, metaData)).Select(_ => new Param {
                Name        = _.name,
                Value       = _.value,
                Sensitivity = _.sensitivity,
            });
            callEvent.Exception       = ex;
            callEvent.ActualTotalTime = requestTime;
            callEvent.ErrCode         = ex != null ? null : (int?)0;

            _eventPublisher.TryPublish(callEvent);
        }
コード例 #5
0
 public EndPointMetadata(ServiceMethod method)
 {
     Initialize(method);
 }
コード例 #6
0
        private async Task HandleRequest(HttpListenerContext context)
        {
            RequestTimings.ClearCurrentTimings();
            using (context.Response)
            {
                var       sw = Stopwatch.StartNew();
                Exception ex;

                // Special endpoints should not be logged/measured/traced like regular endpoints
                try
                {
                    foreach (var customEndpoint in CustomEndpoints)
                    {
                        if (await customEndpoint.TryHandle(context, (data, status, type) => TryWriteResponse(context, data, status, type)))
                        {
                            return;
                        }
                    }
                }
                catch (Exception e)
                {
                    ex = GetRelevantException(e);
                    await TryWriteResponse(context, ExceptionSerializer.Serialize(ex), GetExceptionStatusCode(ex));

                    return;
                }

                // Regular endpoint handling
                TracingContext.SetUpStorage();
                RequestTimings.GetOrCreate(); // initialize request timing context

                Exception actualException = null;
                string    methodName      = null;
                // Initialize with empty object for protocol backwards-compatibility.

                var requestData = new HttpServiceRequest {
                    TracingData = new TracingData()
                };
                ServiceMethod serviceMethod = null;
                try
                {
                    try
                    {
                        ValidateRequest(context);
                        await CheckSecureConnection(context);

                        requestData = await ParseRequest(context);

                        TracingContext.SetOverrides(requestData.Overrides);

                        serviceMethod = ServiceEndPointDefinition.Resolve(requestData.Target);
                        methodName    = serviceMethod.ServiceInterfaceMethod.Name;
                    }
                    catch (Exception e)
                    {
                        actualException = e;
                        if (e is RequestException)
                        {
                            throw;
                        }

                        throw new RequestException("Invalid request", e);
                    }

                    RejectRequestIfLateOrOverloaded();

                    var responseJson = await GetResponse(context, serviceMethod, requestData);
                    await TryWriteResponse(context, responseJson);
                }
                catch (Exception e)
                {
                    actualException = actualException ?? e;
                    ex = GetRelevantException(e);

                    string json = ExceptionSerializer.Serialize(ex);
                    await TryWriteResponse(context, json, GetExceptionStatusCode(ex));
                }
                finally
                {
                    sw.Stop();
                    _serverRequestPublisher.TryPublish(requestData, actualException, serviceMethod, sw.Elapsed.TotalMilliseconds);
                }
            }
        }
コード例 #7
0
 public EndPointMetadata GetMetaData(ServiceMethod method)
 {
     return(_metadata.GetOrAdd(method, m => new EndPointMetadata(m)));
 }
コード例 #8
0
 protected abstract object GetInvokeTarget(ServiceMethod serviceMethod);