Exemplo n.º 1
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);
        }