/// <summary> /// Adds gRPC HTTP API services to the specified <see cref="IServiceCollection" />. /// </summary> /// <param name="services">The <see cref="IServiceCollection" /> for adding services.</param> /// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns> public static IServiceCollection AddGRpcSwagger(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddGRpcHttpApi(); services.TryAddEnumerable(ServiceDescriptor .Transient <IApiDescriptionProvider, GRpcHttpApiDescriptionProvider>()); // Register default description provider in case MVC is not registered services.TryAddSingleton <IApiDescriptionGroupCollectionProvider>(serviceProvider => { var actionDescriptorCollectionProvider = serviceProvider.GetService <IActionDescriptorCollectionProvider>(); var apiDescriptionProvider = serviceProvider.GetServices <IApiDescriptionProvider>(); return(new ApiDescriptionGroupCollectionProvider( actionDescriptorCollectionProvider ?? new EmptyActionDescriptorCollectionProvider(), apiDescriptionProvider)); }); // Add or replace contract resolver. services.Replace(ServiceDescriptor.Transient <IDataContractResolver>(s => { var serializerOptions = s.GetService <IOptions <JsonOptions> >()?.Value?.JsonSerializerOptions ?? new JsonSerializerOptions(); var innerContractResolver = new JsonSerializerDataContractResolver(serializerOptions); return(new GRpcDataContractResolver(innerContractResolver)); })); return(services); }
private static DocumentFilterContext GetDocumentFilterContext(ApiVersionModel versionModel) { var actionDescriptor = new ActionDescriptor(); actionDescriptor.SetProperty(versionModel); var apiDescription = new ApiDescription { ActionDescriptor = actionDescriptor }; var apiDescriptions = new List <ApiDescription> { apiDescription }; var schemaGeneratorOptions = new SchemaGeneratorOptions(); var jsonSerializerOptions = new JsonSerializerOptions(); var jsonSerializerDataContractResolver = new JsonSerializerDataContractResolver(jsonSerializerOptions); var schemaGenerator = new SchemaGenerator(schemaGeneratorOptions, jsonSerializerDataContractResolver); var schemaRepository = new SchemaRepository(); return(new DocumentFilterContext(apiDescriptions, schemaGenerator, schemaRepository)); }
/// <summary> /// Adds gRPC HTTP API services to the specified <see cref="IServiceCollection" />. /// </summary> /// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param> /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns> public static IServiceCollection AddGrpcSwagger(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } services.AddGrpcHttpApi(); services.TryAddSingleton <IApiDescriptionGroupCollectionProvider, GrpcHttpApiDescriptionProvider>(); // Add or replace contract resolver. services.Replace(ServiceDescriptor.Transient <IDataContractResolver>(s => { var serializerOptions = s.GetService <IOptions <JsonOptions> >()?.Value?.JsonSerializerOptions ?? new JsonSerializerOptions(); var innerContractResolver = new JsonSerializerDataContractResolver(serializerOptions); return(new GrpcDataContractResolver(innerContractResolver)); })); return(services); }