[Solution] ASP. NET Core WebAPI: API access is blocked, and corewebapi

Source: Internet
Author: User

[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...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.