Thursday, January 17, 2013

You cannot invalidate the SPRequest object while it’s in use in SharePoint 2010

Cause:

If you are passing SPWeb object in the thread method, you will get this exception.

Suppose in the main thread , you have created the SPWeb object and pass this object to a thread method, like this,



 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {

                            activateThread = new Thread(delegate()
                            {
                                this.Activate(web);
                            });
                            activateThread.Start();
                       }
                  }

Here the SPWeb object 'web' is passing to the thread method 'Activate'. The issue is because of this only.


Remedy:


Don't pass the SPWeb object from main thread (Application Thread) to a thread method. Instead create the SPWeb object inside the thread method (Here Activate method) and dispose the web object there.



1 comment: