CreateTracer() публичный статический Метод

public static CreateTracer ( System.Net.Http.Formatting.MediaTypeFormatter formatter, ITraceWriter traceWriter, HttpRequestMessage request ) : System.Net.Http.Formatting.MediaTypeFormatter
formatter System.Net.Http.Formatting.MediaTypeFormatter
traceWriter ITraceWriter
request System.Net.Http.HttpRequestMessage
Результат System.Net.Http.Formatting.MediaTypeFormatter
        public override MediaTypeFormatter GetPerRequestFormatterInstance(
            Type type,
            HttpRequestMessage request,
            MediaTypeHeaderValue mediaType
            )
        {
            MediaTypeFormatter formatter = null;

            TraceWriter.TraceBeginEnd(
                request,
                TraceCategories.FormattingCategory,
                TraceLevel.Info,
                InnerFormatter.GetType().Name,
                GetPerRequestFormatterInstanceMethodName,
                beginTrace: (tr) =>
            {
                tr.Message = Error.Format(
                    SRResources.TraceGetPerRequestFormatterMessage,
                    InnerFormatter.GetType().Name,
                    type.Name,
                    mediaType
                    );
            },
                execute: () =>
            {
                formatter = InnerFormatter.GetPerRequestFormatterInstance(
                    type,
                    request,
                    mediaType
                    );
            },
                endTrace: (tr) =>
            {
                if (formatter == null)
                {
                    tr.Message = SRResources.TraceGetPerRequestNullFormatterEndMessage;
                }
                else
                {
                    string formatMessage = Object.ReferenceEquals(
                        MediaTypeFormatterTracer.ActualMediaTypeFormatter(formatter),
                        InnerFormatter
                        )
                          ? SRResources.TraceGetPerRequestFormatterEndMessage
                          : SRResources.TraceGetPerRequestFormatterEndMessageNew;

                    tr.Message = Error.Format(formatMessage, formatter.GetType().Name);
                }
            },
                errorTrace: null
                );

            if (formatter != null && !(formatter is IFormatterTracer))
            {
                formatter = MediaTypeFormatterTracer.CreateTracer(formatter, TraceWriter, request);
            }

            return(formatter);
        }
        private IEnumerable <MediaTypeFormatter> CreateFormatterTracers(HttpRequestMessage request, IEnumerable <MediaTypeFormatter> formatters)
        {
            List <MediaTypeFormatter> formatterTracers = new List <MediaTypeFormatter>();

            foreach (MediaTypeFormatter formatter in formatters)
            {
                formatterTracers.Add(MediaTypeFormatterTracer.CreateTracer(formatter, _traceWriter, request));
            }

            return(formatterTracers);
        }
Пример #3
0
        public ContentNegotiationResult Negotiate(
            Type type,
            HttpRequestMessage request,
            IEnumerable <MediaTypeFormatter> formatters
            )
        {
            ContentNegotiationResult result = null;

            _traceWriter.TraceBeginEnd(
                request,
                TraceCategories.FormattingCategory,
                TraceLevel.Info,
                _innerNegotiator.GetType().Name,
                NegotiateMethodName,
                beginTrace: (tr) =>
            {
                tr.Message = Error.Format(
                    SRResources.TraceNegotiateFormatter,
                    type.Name,
                    FormattingUtilities.FormattersToString(formatters)
                    );
            },
                execute: () =>
            {
                result = _innerNegotiator.Negotiate(type, request, formatters);
            },
                endTrace: (tr) =>
            {
                tr.Message = Error.Format(
                    SRResources.TraceSelectedFormatter,
                    result == null
                          ? SRResources.TraceNoneObjectMessage
                          : MediaTypeFormatterTracer
                    .ActualMediaTypeFormatter(result.Formatter)
                    .GetType().Name,
                    result == null || result.MediaType == null
                          ? SRResources.TraceNoneObjectMessage
                          : result.MediaType.ToString()
                    );
            },
                errorTrace: null
                );

            if (result != null)
            {
                result.Formatter = MediaTypeFormatterTracer.CreateTracer(
                    result.Formatter,
                    _traceWriter,
                    request
                    );
            }

            return(result);
        }
        public void Decorator_GetInner_On_All_MediaTypeFormatterTracers_Returns_Object_Of_Type_MediaTypeFormatter(MediaTypeFormatter formatter)
        {
            // Arrange
            HttpRequestMessage request         = new HttpRequestMessage();
            MediaTypeFormatter formatterTracer = MediaTypeFormatterTracer.CreateTracer(formatter, new TestTraceWriter(), request);

            // Act
            MediaTypeFormatter innerFormatter = Decorator.GetInner(formatterTracer);

            // Assert
            Assert.Same(formatter, innerFormatter);
        }
        public void CreateTracer_Returns_Tracing_Formatter(MediaTypeFormatter formatter)
        {
            // Arrange
            HttpRequestMessage request = new HttpRequestMessage();

            // Act
            MediaTypeFormatter tracingFormatter = MediaTypeFormatterTracer.CreateTracer(formatter, new TestTraceWriter(), request);

            // Assert
            IFormatterTracer tracer = Assert.IsAssignableFrom <IFormatterTracer>(tracingFormatter);

            Assert.Same(formatter, tracer.InnerFormatter);
        }
        public void CreateTracer_Returns_Tracing_Formatter(Type formatterType)
        {
            // Arrange
            HttpRequestMessage request   = new HttpRequestMessage();
            MediaTypeFormatter formatter = (MediaTypeFormatter)Activator.CreateInstance(formatterType);

            // Act
            MediaTypeFormatter tracingFormatter = MediaTypeFormatterTracer.CreateTracer(formatter, new TestTraceWriter(), request);

            // Assert
            Assert.IsAssignableFrom <IFormatterTracer>(tracingFormatter);
            Assert.IsAssignableFrom(formatterType, tracingFormatter);
        }
        public void CreateTracer_Loads_RequiredMemberSelector_From_InnerFormatter(Type formatterType)
        {
            // Arrange
            HttpRequestMessage request   = new HttpRequestMessage();
            MediaTypeFormatter formatter = (MediaTypeFormatter)Activator.CreateInstance(formatterType);

            Mock <IRequiredMemberSelector> requiredMemberSelector = new Mock <IRequiredMemberSelector>();

            formatter.RequiredMemberSelector = requiredMemberSelector.Object;

            // Act
            MediaTypeFormatter tracingFormatter = MediaTypeFormatterTracer.CreateTracer(formatter, new TestTraceWriter(), request);

            // Assert
            Assert.Equal(formatter.RequiredMemberSelector, tracingFormatter.RequiredMemberSelector);
        }
        public void CreateTracer_Loads_SupportedEncodings_From_InnerFormatter(Type formatterType)
        {
            // Arrange
            HttpRequestMessage request   = new HttpRequestMessage();
            MediaTypeFormatter formatter = (MediaTypeFormatter)Activator.CreateInstance(formatterType);
            Mock <Encoding>    encoding  = new Mock <Encoding>();

            formatter.SupportedEncodings.Clear();
            formatter.SupportedEncodings.Add(encoding.Object);

            // Act
            MediaTypeFormatter tracingFormatter = MediaTypeFormatterTracer.CreateTracer(formatter, new TestTraceWriter(), request);

            // Assert
            Assert.Equal(formatter.SupportedEncodings, tracingFormatter.SupportedEncodings);
        }
        public void CreateTracer_Returns_Tracing_BufferedFormatter()
        {
            // Arrange
            HttpRequestMessage request   = new HttpRequestMessage();
            MediaTypeFormatter formatter = new Mock <BufferedMediaTypeFormatter>()
            {
                CallBase = true
            }.Object;

            // Act
            MediaTypeFormatter tracingFormatter = MediaTypeFormatterTracer.CreateTracer(formatter, new TestTraceWriter(), request);

            // Assert
            Assert.IsAssignableFrom <IFormatterTracer>(tracingFormatter);
            Assert.IsAssignableFrom <BufferedMediaTypeFormatter>(tracingFormatter);
        }
        public void CreateTracer_Loads_MediaTypeMappings_From_InnerFormatter(Type formatterType)
        {
            // Arrange
            HttpRequestMessage request   = new HttpRequestMessage();
            MediaTypeFormatter formatter = (MediaTypeFormatter)Activator.CreateInstance(formatterType);

            Mock <MediaTypeMapping> mediaTypeMapping = new Mock <MediaTypeMapping>(new MediaTypeHeaderValue("application/dummy"));

            formatter.MediaTypeMappings.Clear();
            formatter.MediaTypeMappings.Add(mediaTypeMapping.Object);

            // Act
            MediaTypeFormatter tracingFormatter = MediaTypeFormatterTracer.CreateTracer(formatter, new TestTraceWriter(), request);

            // Assert
            Assert.Equal(formatter.MediaTypeMappings, tracingFormatter.MediaTypeMappings);
        }
        public void CreateTracer_Loads_SupportedMediaTypes_From_InnerFormatter(Type formatterType)
        {
            // Arrange
            HttpRequestMessage request   = new HttpRequestMessage();
            MediaTypeFormatter formatter = (MediaTypeFormatter)Activator.CreateInstance(formatterType);

            MediaTypeHeaderValue mediaTypeHeaderValue = new MediaTypeHeaderValue("application/dummy");

            formatter.SupportedMediaTypes.Clear();
            formatter.SupportedMediaTypes.Add(mediaTypeHeaderValue);

            // Act
            MediaTypeFormatter tracingFormatter = MediaTypeFormatterTracer.CreateTracer(formatter, new TestTraceWriter(), request);

            // Assert
            Assert.Equal(formatter.SupportedMediaTypes, tracingFormatter.SupportedMediaTypes);
        }