Server cannot modify cookies after HTTP headers have been sent

[亡魂溺海] 提交于 2019-12-05 20:49:50

Unless you have a very good reason to, you shouldn't be spinning up background worker threads in an ASP.NET request. Ultimately you still have to wait for this thread to finish its work before you send the response back to the browser.

It sounds like the response stream has already been partially written to and then your thread is trying to add the cookie.

I'd rethink your strategy here and take a read of the following guidelines:

Chapter 6 — Improving ASP.NET Performance - Threading Guidelines

It looks like a dated document but the principles still stand. If the reason for making your call to the data processor is to prevent the ASP.NET worker thread from blocking and using up resources because the data processor is long running, then consider making the page an Asynchronous page instead.

Yes, Cookies are part of the http response and in a async operation you cannot change anything after response is generated and sent to browser.

To workaround this i recommend to build a ajax loop on browser to get async operation result. When operation completed you can return a cookie with ajax response.

What if it is in preinit or init? not sure if this will help though. http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!