[Solution] ASP. NET Core WebAPI: API access is blocked, and corewebapi
Suppose there are three projects: ProjectA, ProjectB, and ProjectC.
The ProjectA project has two interfaces: Get () and GetTest (string code ).
An interface in the project, GetHelloWorld ().
ProjectC is a background task.
The problem is:
The Get interface of the ProjectA project accesses the HelloWorld of the ProjectB project. In this case, the PojectC project requests GetTest in ProjectA,In this case, the GET request in the project is blocked.,You must wait until the PojectC access ends and the ProjectA access is resumed....
I wrote a demo.
ProjectA code:
[HttpGet] public async Task<IEnumerable<string>> Get() { while ( true ) { try { Trace.WriteLine( $"{DateTime.Now} start web request" ); var apiUrl = $"http://localhost:2615/"; var res = await GetAsync( apiUrl ); Trace.WriteLine( $"{DateTime.Now} end web request" ); } catch { } } } [HttpGet( "{id}" )] public string Gettest( string code ) { return code; }
Project code:
public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { Trace.WriteLine(DateTime.Now.ToString()); await context.Response.WriteAsync("Hello World!"); }); } }
Project code:
int threadCount = 100; Semaphore semaphore = new Semaphore( 0, 1000 ); for ( int i = 0; i < threadCount; i++ ) { new Thread( threadId => { for ( int j = 0; j < 100; j++ ) { try { using ( var client = new WebClient { Proxy = null } ) { Console.WriteLine( "thread{0}round{1}->{2}", threadId, j, client.DownloadString( $"http://localhost:40895/api/values/Gettest?code={Guid.NewGuid()}" ) ); } } catch ( Exception err ) { Console.WriteLine( "thread{0}round{1}->error", threadId, j ); } } semaphore.Release(); } ).Start( i ); }
I don't know if Microsoft's Bug is still a problem I used at this time, so please ask bloggers to help me see what the problem is...