Пример #1
0
        /// <summary>
        /// Gets the first content-type match for the handler from a list of accepted types
        /// </summary>
        /// <param name="mediaTypeHandler">The content type handler.</param>
        /// <param name="acceptedTypes">The accepted types.</param>
        /// <returns>The MIME content type, or <c>null</c> if no match is found.</returns>
        public static string GetContentType(this IMediaTypeHandler mediaTypeHandler, IList <string> acceptedTypes)
        {
            HashSet <string> contentTypes;

            if (!Cache.TryGetValue(mediaTypeHandler.GetType(), out contentTypes))
            {
                lock (Sync)
                {
                    if (!Cache.TryGetValue(mediaTypeHandler.GetType(), out contentTypes))
                    {
                        contentTypes =
                            new HashSet <string>(
                                MediaTypesAttribute.Get(mediaTypeHandler.GetType()).SelectMany(cta => cta.ContentTypes),
                                StringComparer.OrdinalIgnoreCase);
                        Cache[mediaTypeHandler.GetType()] = contentTypes;
                    }
                }
            }
            return(acceptedTypes.FirstOrDefault(x => ContainsMatchingContentType(contentTypes, x)));
        }
Пример #2
0
 /// <summary>
 /// Gets the first content-type match for the handler from a list of accepted types
 /// </summary>
 /// <param name="mediaTypeHandler">The content type handler.</param>
 /// <param name="acceptedTypes">The accepted types.</param>
 /// <returns>The MIME content type, or <c>null</c> if no match is found.</returns>
 public static string GetContentType(this IMediaTypeHandler mediaTypeHandler, IList <string> acceptedTypes)
 {
     return(MediaTypesAttribute.Get(mediaTypeHandler.GetType()).SelectMany(cta => cta.ContentTypes)
            .FirstOrDefault(acceptedTypes.Contains));
 }
Пример #3
0
        public async ValueTask <bool> Serialize(string mediaType, object obj, PipeWriter writer)
        {
            IMediaTypeHandler handler = default;

            try
            {
                if (this._handlers.TryGetValue(mediaType, out handler))
                {
                    await handler.Serialize(obj, writer);

                    return(true);
                }
            }
            catch (Exception exc)
            {
                this._logger.LogWarning(exc, $"Failure to serialize body into '{mediaType}' using {handler.GetType().FullName}: {exc.Message}.");
            }

            return(false);
        }