private void EnsureServices(HttpContext context) { if (_hostingEnvironment == null) { _hostingEnvironment = context.RequestServices.GetService <IHostingEnvironment>(); } if (_apiDescriptionGroupModelCollectionProvider == null) { _apiDescriptionGroupModelCollectionProvider = context.RequestServices.GetService <IApiDescriptionGroupModelCollectionProvider>(); } }
/// <summary> /// Creates a new instance of <see cref="ApiDescriptionDetailModelProvider"/>. /// </summary> /// <param name="modelMetadataWrapperProvider">The <see cref="IModelMetadataWrapperProvider"/>.</param> /// <param name="apiDescriptionGroupModelCollectionProvider">The <see cref="IApiDescriptionGroupModelCollectionProvider"/>.</param> /// <param name="objectGenerator"></param> /// <param name="documentationProvider"></param> /// <param name="loggerFactory"></param> public ApiDescriptionDetailModelProvider( IModelMetadataWrapperProvider modelMetadataWrapperProvider, IApiDescriptionGroupModelCollectionProvider apiDescriptionGroupModelCollectionProvider, IObjectGenerator objectGenerator, IDocumentationProvider documentationProvider, ILoggerFactory loggerFactory) { _modelMetadataWrapperProvider = modelMetadataWrapperProvider; _objectGenerator = objectGenerator; _documentationProvider = documentationProvider; _logger = loggerFactory.CreateLogger <ApiDescriptionDetailModelProvider>(); var apiDescriptionGroupModels = apiDescriptionGroupModelCollectionProvider .ApiDescriptionGroups .Items .SelectMany(g => g.Items) .ToList(); foreach (var item in apiDescriptionGroupModels.GroupBy(o => o.Id).Where(p => p.Count() > 1)) { var controllerActionDescriptors = item.Select(o => o.ApiDescription.ActionDescriptor) .OfType <ControllerActionDescriptor>() .Select(p => new JObject { { "ControllerName", p.ControllerName }, { "ActionName", p.ActionName }, { "RouteTemplate", p.AttributeRouteInfo.Template }, }); _logger.LogWarning($"dumplicate web apis with the same route template.{Environment.NewLine}{JsonConvert.SerializeObject(controllerActionDescriptors, Formatting.Indented)}"); } var dictionary = apiDescriptionGroupModels .Distinct() .ToDictionary(o => o.Id, o => CreateApiDescriptionDetailModel(o)); //TODO Maybe exits dumpliate ids. _apiDescriptionDetailModelCache = new ReadOnlyDictionary <string, ApiDescriptionDetailModel>(dictionary); }