简体   繁体   中英

WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false

I created a simple WCF service in Visual Studio 2017, .NET 4.5. (Add New Item -> WCF Service). It changed the web.config and set the aspNetCompatibilityEnabled to true . So, the service is running in ASP.NET compatibility mode.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

The service has one OneWay method:

[OperationContract(IsOneWay = true)]
void DoWork();

The implementation looks like this:

public void DoWork()
{
    var threadId = Thread.CurrentThread.ManagedThreadId;
    Debug.WriteLine("Starting..." + threadId);
    Thread.Sleep(TimeSpan.FromSeconds(30));
    Debug.WriteLine("Finished..." + threadId);
}

This works like expected. One can send requests and then immediately return without and answer. I can send many requests and immediately get back.

However, if I change aspNetCompatibilityEnabled="false" and set

[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.NotAllowed)] 

on the service class, the service slows down. It can handle only 3 requests and the 4th one just waits. The service does not return without an answer immediately. It looks like, it can handle only 3 requests at a time.

What explains this behavior? Every setting is the default, nothing special is set in web.config .

If your application is hosted in IIS and if you set the aspNetCompatibilityEnabled='false' the requests will not flow through the ASP.NET http pipeline. One of the reason for the slow behavior could be, since the requests are not taking advantage of the pipeline, IIS is taking some time to run the scripts necessary to receive and pass the request to WCF. WCF by default only process one request at a time until you specify a different concurrency level. Other requests have to wait till the previous request is processed. It is the ASP.NET HTTP pipeline that is holding the request queue and processing it one at a time. I believe in this case since ASP.NET HTTP pipeline advantage is lost the the non ASP.NET pipeline is not handling the request queue properly. I hope this answers your questions. Some additional documentation on this are mentioned below

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet

https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/servicehostingenvironment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM