public static IVendorMediaTypesBuilder AddVendorMediaTypes(this IServiceCollection services,
                                                                   Action <VendorMediaTypeCollection> setupAction = null)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            // Setup media types and add them to the DI
            var collection = new VendorMediaTypeCollection();

            if (setupAction == null)
            {
                collection.AddAssembly(Assembly.GetCallingAssembly());
            }
            else
            {
                setupAction.Invoke(collection);
            }

            services.TryAddSingleton(collection);
            services.TryAddEnumerable(ServiceDescriptor
                                      .Singleton <IConfigureOptions <MvcOptions>, ConfigureVendorMediaTypesMvcOptions>());
            return(new VendorMediaTypesBuilder(services));
        }
Ejemplo n.º 2
0
        public static bool TryGetType(this VendorMediaTypeCollection collection, string value, out Type type)
        {
            type = null;
            var match = collection.SingleOrDefault(pair => pair.Value.Any(value.Contains));

            if (match.Equals(default(KeyValuePair <Type, string[]>)))
            {
                // we couldn't find a matching type
                return(false);
            }

            type = match.Key;
            return(true);
        }
Ejemplo n.º 3
0
        public static void AddAssembly(this VendorMediaTypeCollection collection, Assembly assembly)
        {
            var list = assembly.GetTypes()
                       .Where(type => Attribute.IsDefined(type, typeof(MediaTypeAttribute)))
                       .Select(type => new
            {
                Type       = type,
                Attributes = type.GetCustomAttributes <MediaTypeAttribute>()
                             .SelectMany(attribute => attribute.Types).ToArray()
            })
                       .ToList();

            foreach (var item in list)
            {
                collection.Add(item.Type, item.Attributes);
            }
        }
Ejemplo n.º 4
0
 public VendorMediaTypesOperationFilter(VendorMediaTypeCollection collection)
 {
     _collection = collection ?? throw new ArgumentNullException(nameof(collection));
 }
Ejemplo n.º 5
0
 public static void Add <T>(this VendorMediaTypeCollection collection, params string[] values)
 {
     collection.Add(typeof(T), values);
 }
 public ConsumesConstraintForVendorMediaTypesConvention(VendorMediaTypeCollection mediaTypeCollection)
 {
     _mediaTypeCollection = mediaTypeCollection ?? throw new ArgumentNullException(nameof(mediaTypeCollection));
 }