示例#1
0
        /// <summary>
        ///     Called after an inbound message has been received but before the message is dispatched to the intended operation.
        /// </summary>
        /// <returns>
        ///     The object used to correlate state. This object is passed back in the
        ///     <see
        ///         cref="M:System.ServiceModel.Dispatcher.IDispatchMessageInspector.BeforeSendReply(System.ServiceModel.Channels.Message@,System.Object)" />
        ///     method.
        /// </returns>
        /// <param name="request">The message message.</param>
        /// <param name="channel">The incoming channel.</param>
        /// <param name="instanceContext">The current service instance.</param>
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            if (request == null || _tracer == null)
            {
                return(null);
            }

            ExtractNames(request, channel, out _webServiceName, out _operationName);

            Guid   correlationGuid;
            string correlationId = GetMessageHeader(request, CorrelationIdHeader);

            if (!string.IsNullOrWhiteSpace(correlationId) && Guid.TryParse(correlationId, out correlationGuid) && correlationGuid != Guid.Empty)
            {
                _tracer.SetCorrelationId(correlationGuid);
            }

            string userName = GetMessageHeader(request, UserNameHeader);

            if (!string.IsNullOrWhiteSpace(userName))
            {
                _tracer.SetPrincipalId(userName);
            }

            string sessionId = GetMessageHeader(request, SessionIdHeader);

            if (!string.IsNullOrWhiteSpace(sessionId))
            {
                _tracer.SetSessionId(sessionId);
            }

            _serverSideStartTime = _tracer.TraceStartPerformance();
            return(null);
        }
示例#2
0
        /// <summary>
        /// Exécute le code du <see cref="MonitoringMiddleware"/>.
        /// </summary>
        /// <param name="context">Le contexte Http.</param>
        /// <returns>Le code d'appel au middleware suivant.</returns>
        public async Task InvokeAsync(HttpContext context)
        {
            HttpRequest request = context.Request;
            // définit le session id au niveau du monitoring s'il a été fourni en tant que header http
            string sessionId = request?.Headers?[SessionIdHeader].FirstOrDefault();

            if (!string.IsNullOrWhiteSpace(sessionId))
            {
                _tracer.SetSessionId(sessionId);
            }
            // définit le principal id au niveau du monitoring
            string principalId = context.User?.GetPreferredUserName();

            if (!string.IsNullOrWhiteSpace(principalId))
            {
                _tracer.SetPrincipalId(principalId);
            }
            // débute une corrélation au niveau du monitoring
            _tracer.SetCorrelationId(Guid.NewGuid());
            try
            {
                using (_tracer.TracePerformance(traceEventType: TraceEventType.HttpResponse,
                                                traceEventName: request.GetEncodedPathAndQuery(),
                                                context: CreateContextForApiRequest(request)))
                {
                    await _next(context);
                }
            }
            catch (SqlException sqlException) when(sqlException.Number == 60000)
            {
                _tracer.TraceWarning(InternalMessages.UnexpectedUserAction, ErrorCodes.Application.UnexpectedUserAction, context: CreateContextForApiRequest(request));
                await context.Response.WriteProblemAsync(HttpStatusCode.NotFound, InternalMessages.NotFoundResource);
            }
            catch (Exception exception)
            {
                _tracer.TraceException(exception, context: CreateContextForApiRequest(request));
                await context.Response.WriteProblemAsync(HttpStatusCode.InternalServerError, exception.Message);
            }
        }