问题由来:.net core api之前是用 .net core 2.0开发的,测试过都是正常的,近期升级到了3.0,发现api get正常,post提示400,405 Method Not Allowed
查找没有找到原因,就在本地调试,提示错误信息:
System.InvalidOperationException: Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
错误原因:.net core 3.0不允许同步操作,必须改成异步方式
解决方法:将原来的ReadToEnd(),修改为ReadToEndAsync(), 问题得到解决。
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8)) { result = await reader.ReadToEndAsync(); //升级3.0不允许同步操作,必须异步 Logger.Info("FactoryBarCode:" + result); }
另一种办法:
It's worth noting that if you host on kestrel directly then your Program.cs should have appropriate ConfigureKestrel call public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .ConfigureKestrel((context, options) => { options.AllowSynchronousIO = true; })