Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am getting this error after adding a document to a library that has a custom event receiver associated with it:

An SPRequest object was not disposed before the end of this thread. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. This object will now be disposed.

However I can't seem to figure out where the issue with the event reciever lies. As I have run the SPDisposeCheck tool and it seems to think my code is fine. Any ideas on how I can track this down to where it is coming from?

Here is a stripped down version of my code:

public override void ItemAdded(SPItemEventProperties properties)
        {
                SPWeb web = properties.Web;
                var item = properties.ListItem;                
                var id = String.Empty;
                try
                {
                    EventFiringEnabled = false;
                    web.AllowUnsafeUpdates = true;


                    switch (item.ContentType.Name.ToString())
                    {
                        case "ABC":
                            id = GetConfigurationKey(web, "PropertyID");
                            item["Property_x0020_ID"] = id;
                            item.Update();
                            break;
                        //Other case(s) here removed....
                        default:
                            logULS("ItemAdded Invalid Content Type, skipping...");
                            break;
                    }

                }
                catch (Exception ex)
                {
                    logULS("ItemAdded Error: " + ex.Message + "/" + ex.StackTrace);
                }
                finally
                {
                    web.AllowUnsafeUpdates = false;
                    EventFiringEnabled = true;                    
                }
            logULS("ItemAdded Ended.");
            }

Log ULS:

private void logULS(string message)
        {
            SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("EventRec", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, message);
        }

Get Config Key:

private string GetConfigurationKey(SPWeb web, string keyName)
        {
            logULS("GetConfigurationKey Started.");
            string keyValue = "*KEYNOTFOUND*";
            try
            {
                SPSecurity.RunWithElevatedPrivileges
                (
                    delegate()
                    {
                        using (var elevatedSite = new SPSite(web.Url))
                        {
                            logULS("::GetConfigurationKey Opened Elevated Site.");
                            using (var elevatedWeb = elevatedSite.OpenWeb())
                            {
                                logULS("::GetConfigurationKey Opened Elevated Web.");
                                foreach (SPList elevatedList in elevatedWeb.Lists)
                                {
                                    if (elevatedList.Title == "Configuration Keys")
                                    {
                                        var query = new SPQuery();
                                        query.Query = string.Format("<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">{0}</Value></Eq></Where>", keyName);
                                        SPListItemCollection foundItems = elevatedList.GetItems(query);
                                        if (foundItems.Count > 0)
                                        {
                                            SPListItem foundItem = foundItems[0];
                                            keyValue = foundItem["Value"].ToString();
                                            logULS("::GetConfigurationKey Found Key value: " + keyValue);
                                        }
                                    }
                                }
                            }
                        }
                    }
                );
            }
            catch (Exception ex)
            {
                logULS("GetConfigurationKey Error: " + ex.Message);
                return "*ERROR*";
            }
            logULS("GetConfigurationKey Ended.");
            return keyValue;
        }

The error message usually shows up the next line or two after the log messages I have coded in...

share|improve this question
    
Can you also post the code for the GetConfigurationKey and logULS functions? –  Vardhaman Deshpande Oct 16 '13 at 21:45
    
Updated with additional code.... –  BlueBird Oct 17 '13 at 12:57
    
I think the problem lays in where you are passing in the SPWeb object to GetConfigurationKey, try rewriting it to pass in SPWeb.ID or Url instead. Or dispose in this method. –  Robert Lindgren Oct 17 '13 at 13:04
    
What the current code leads to is a false positive, it actually disposes it later on, but it is still logged –  Robert Lindgren Oct 17 '13 at 13:07
    
I'm not sure I understand, but I believe the issue lies within the ItemAdded method... I changed the getconfigurationkey method as you suggested. –  BlueBird Oct 17 '13 at 13:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.