find and solve || findandsolve.com
Please wait.....

Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead.


System.InvalidOperationException: ‘Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true

This exception was originally thrown at this call stack:

    [External Code]

    HomeController.SitemapXml() in HomeController.cs

This is  my code in HmeController

       [Route("/sitemap.xml")]
        public async void SitemapXml()
        {
            string host = Request.Scheme + "://" + Request.Host;
            Response.ContentType = "application/xml";   
            var data = _iBasicAritcalProvider.GetlistForSitetMap();
            using (var xml = XmlWriter.Create(Response.Body, new XmlWriterSettings { Indent = true }))
            {
                xml.WriteStartDocument();                 xml.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
                foreach (var item in data.Data.ToList())                 {                     xml.WriteStartElement("url");                     xml.WriteElementString("loc", host + item.TitleURL);                     xml.WriteElementString("priority", "0.8");                     xml.WriteElementString("changefreq", "monthly");                     xml.WriteElementString("lastmod", item.PublishDate);                     xml.WriteEndElement();                 }                 xml.WriteEndElement();
            }         }


Answer


Option 1.

If you want ot a short-term solution to this problem in your project , then you can turn the setting back on for your server.

You turn it on by enabling it in the server options in Startup.ConfigureServices for your specific server.

For example, you can use  following sets AllowSynchronousIO=true for IIS:

public class Startup
{
 public void ConfigureServices(IServiceCollection services)
 {
  //another service methods
  services.Configure<IISServerOptions>(options =>
  {
   options.AllowSynchronousIO = true;
  });
 }
 //another methods in Startup
}


Option 2.

Put this in your Startup.cs class for either Kestrel or IIS:

If you wan to use in IIS then you can do that 

   services.Configure<IISServerOptions>(options =>
    {
        options.AllowSynchronousIO = true;
    });

If use Kestrel, you can  do that

    services.Configure<KestrelServerOptions>(options =>
    {
        options.AllowSynchronousIO = true;
    });


Option 3.

When the exception is thrown in code you cannot control, and you have no other choice than to enable AllowSynchronousIO, it is best to enable it for specific requests, instead of globally.

You can also create a simple middleware and that function apply this to specific requests:

app.Use(app.Use(async (context, next) =>
{
    if (context.Request.Path.StartsWithSegments("/some-your-link-that-needs-sync-io"))
    {
        var syncIoFeature = context.Features.Get<IHttpBodyControlFeature>();
        if (syncIoFeature != null)
        {
            syncIoFeature.AllowSynchronousIO = true;
        }
    }
    await next();
})


Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response