IWebHostBuilderExtensions_VitConfig.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Logging;
  4. using Newtonsoft.Json.Linq;
  5. namespace Vit.Extensions
  6. {
  7. public static class IWebHostBuilderExtensions_VitConfig
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. /// <param name="data"></param>
  13. /// <returns></returns>
  14. public static IWebHostBuilder UseVitConfig(this IWebHostBuilder data)
  15. {
  16. data.ConfigureServices(delegate (Microsoft.AspNetCore.Hosting.WebHostBuilderContext context, Microsoft.Extensions.DependencyInjection.IServiceCollection services)
  17. {
  18. services.Configure(delegate (Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions options)
  19. {
  20. //限制body的大小
  21. options.Limits.MaxRequestBodySize = Vit.Core.Util.ConfigurationManager.ConfigurationManager
  22. .Instance.GetByPath<long?>("Vit.Kestrel.MaxRequestBodySize");
  23. });
  24. //解决Multipart body length limit 134217728 exceeded
  25. services.Configure<Microsoft.AspNetCore.Http.Features.FormOptions>(x =>
  26. {
  27. var ValueLengthLimit = Vit.Core.Util.ConfigurationManager.ConfigurationManager.Instance.GetByPath<int?>("Vit.Kestrel.ValueLengthLimit");
  28. if (ValueLengthLimit.HasValue)
  29. {
  30. x.ValueLengthLimit = ValueLengthLimit.Value;
  31. }
  32. var MultipartBodyLengthLimit = Vit.Core.Util.ConfigurationManager.ConfigurationManager.Instance.GetByPath<long?>("Vit.Kestrel.MultipartBodyLengthLimit");
  33. if (MultipartBodyLengthLimit.HasValue)
  34. {
  35. x.MultipartBodyLengthLimit = MultipartBodyLengthLimit.Value;// In case of multipart
  36. }
  37. });
  38. });
  39. if (null == Vit.Core.Util.ConfigurationManager.ConfigurationManager.Instance.Get<JToken>("Logging"))
  40. {
  41. data.ConfigureLogging((Microsoft.Extensions.Logging.ILoggingBuilder logging) =>
  42. {
  43. logging.ClearProviders();
  44. });
  45. }
  46. return data;
  47. }
  48. }
  49. }