using System; using ApiBasicAuth.Security; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; namespace Win_in.Sfs.Scp.WebApi.XmlHost { public class Startup { public void ConfigureServices(IServiceCollection services) { // services.AddControllers() // .AddXmlDataContractSerializerFormatters(); var configuration = services.GetConfiguration(); services.AddControllers() .AddMvcOptions(options => { // options.ModelMetadataDetailsProviders.Add( // new ExcludeBindingMetadataProvider(typeof(System.Version))); // options.ModelMetadataDetailsProviders.Add( // new SuppressChildValidationMetadataProvider(typeof(System.Guid))); options.InputFormatters.Insert(0, new XDocumentInputFormatter()); // options.OutputFormatters.Insert(0,new XDocumentOutputFormatter()); }) .AddXmlDataContractSerializerFormatters() .AddXmlSerializerFormatters(); // services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Win_in.Sfs.Scp.WebApi.Xml.Host", Version = "v1" }); c.SchemaFilter(); }); services.AddAuthentication("BasicAuthentication") .AddScheme("BasicAuthentication", null); services.Configure(configuration.GetSection("BasicAuthentication")); services.Configure(configuration.GetSection("RemoteAuthentication")); services.AddSingleton(typeof(IHttpClientInvoker<,>),typeof(HttpClientInvoker<,>)); services.AddSingleton(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Win_in.Sfs.Scp.WebApi.Xml.Host v1")); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } } internal class SwaggerFixArraysInXmlFilter : ISchemaFilter { public void Apply(OpenApiSchema schema, SchemaFilterContext context) { var type = context.Type; // Fix issues with xml array examples not generating correctly if (type.IsValueType) { return; } if (type.Name == "String") { return; } schema.Xml = new OpenApiXml { Name = type.Name }; if (schema.Properties == null) { return; } foreach (var property in schema.Properties) { //Array property, which wraps its elements if (property.Value.Type != "array") { continue; } property.Value.Xml = new OpenApiXml { Name = $"{property.Key}", Wrapped = true }; property.Value.Items.Xml = new OpenApiXml { Name = $"{property.Value.Items.Type}", Wrapped = true }; } } }