I have a very strange issue which I cannot explain. We have and event receiver and within this we instantiate our own class that does all the work we need it to do. As part of this we pass in an SPWEB that we create. The code runs fine but when the code tries to get a list from the passed in SPWEB we get the "Detected use of SPRequest for previously closed SPWeb object" error being shown in the ULS logs.

The SPWeb is only being closed within the event receiver code after our class has done the work.

What is really strange is that if i run the same code from a Unit test no such error occurs so i am wondering if it is either an issue with the W3P process of the way sharepoint handles the threads of event receivers.

Here is the event receiver code


    SPSite elevatedSite = new SPSite(properties.SiteId);
    SPWeb elevatedWeb = elevatedSite.OpenWeb(properties.RelativeWebUrl);
                  MYClass myCLass = new MyCLass();
                  myClass.DoWork(elevatedWeb); // Cause error in ULS log
                if (elevatedWeb != null)
                if (elevatedSite != null)

If I run the same code in a unit test where I create a SPWeb object then the code run as I expect without any issues in the ULS logs.

nothing within MYClass calls dispose on the web object

I know I could but the Site and Web in a using, this does not make a difference to the error appearing or not

I have already used SPDisposeChecker and this shows no issues. I have gone through a number of posts around correctly disposing of SPWeb objects and believe that my code is doing this correctly

My code is working as expected but I have no idea how to solve this issue or if an issue actually exists. I do not want to release code that may cause a problem on production systems.

  • What work is MyClass actually doing? My concern is that it is doing some operation that executes asynchronously. – Jon Lester Oct 2 '12 at 13:39

It looks OK to me, but you could try this:

using (var elevatedSite = new SPSite(properties.SiteId))
    using (var elevatedWeb = elevatedSite.OpenWeb(properties.RelativeWebUrl))
        var myCLass = new MyCLass();
| improve this answer | |
  • Sorry for refactoring to var, I blame R# cleanup code :) – eirikb Sep 27 '12 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.