Disposing of SharePoint Variables

17 12 2008

Hi all,

I was recently struggling to figure out why I had the following error appear in my SharePoint error logs:

ERROR: request not found in the TrackedRequests. We might be creating and closing webs on different threads. ThreadId = 22, Free call stack =    at Microsoft.SharePoint.SPRequestManager.Release(SPRequest request)“

Well it turns out, in the particular piece of code in the workflow it was running at the time. I was disposing of the workflowproperties.site variable.

Having done a bit of research and testing, I have found that you do not need to dispose of workflowproperties.site, thought it would be useful to post it here in case anyone was having a similar problem.

Advertisements

Actions

Information

11 responses

15 01 2009
Alain

Hi,
Also i am struggling with the same problem and it is not fixed till now.I am not disposing any of the workflowproperties.site.
here is a sample of my code:

using (SPSite spSite = new SPSite(siteUrl))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
// My code here
}
}
Any help would be appreciated.

15 01 2009
alex350r

Hi Alian,

What I found is that you only need one using statement for the spsite and spweb, you will then no longer get the trackedrequests error. Try this code:

using (SPWeb web = workflowProperties.Site.OpenWeb(“Your Web URL”))
{
//Your code here
}

Cheers.

22 01 2009
Alain

Hi Alex,

i have applied the above on my application but the error is still generated :S.

Thanks for your help.

22 01 2009
alex350r

Hi Alain,

Sorry to hear that it is not working, please post your code inside the using statement please so I can have a look at it.

Do you have any other using statements elsewhere in the workflow?

Cheers,

Alex

22 01 2009
Alain

HI Alex,

Well yes i have many other using statements. it is a big workflow.
Here is one example:

SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPWeb spWeb = workflowProperties.Site.OpenWeb(“/”))
{
// Get the list from the site

Configuration config = WebConfigurationManager.OpenWebConfiguration(“/”, workflowProperties.Site.WebApplication.Name);
string myVal = config.AppSettings.Settings[ConfigurationKeys.WFConfigurationList].Value;
SPList spList = spWeb.Lists[myVal];
foreach (SPListItem item in spList.Items)
{
if (item[“Title”].ToString().Equals(key))
{
value = item[“Value”].ToString();
break;
}
}

}
});

22 01 2009
Alain

Here is another code:

public string GetDelegatedUser(Dictionary context, SPWorkflowActivationProperties workflowProperties)
{
string delegatedUser = “”;

SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPWeb spWeb = workflowProperties.Site.RootWeb)
{ // Get the list from the site
SPList spList = spWeb.Lists[context[ContextKeys.ListName]];

foreach (SPListItem spListItem in spList.Items)
{
if (spListItem[ContextKeys.Title].ToString().ToLower().Equals(context[ContextKeys.LoginName].ToString().ToLower()))
{
DateTime FromDate = DateTime.Parse(spListItem[ContextKeys.FromDate].ToString());
DateTime ToDate = DateTime.Parse(spListItem[ContextKeys.ToDate].ToString());

if (DateTime.Today.CompareTo(FromDate) >= 0 && DateTime.Today.CompareTo(ToDate) <= 0)
{
delegatedUser = spListItem[ContextKeys.DelegateTo].ToString();
}
break;
}
}
}
});
return delegatedUser;

}

22 01 2009
alex350r

Hi Alain,

Thanks for posting the code I will have a look into it. I can’t see anything blindingly obvious.

One interesting thing is that you are using SPSecurity.RunWithElevatedPrivileges, I was also using this and was getting the TrackedRequests error. I removed it as it wasn’t actually necessary for the piece of code I was using so this could be causing the problem. It is definitely a disposal problem somewhere.

Cheers,

Alex

23 01 2009
Alain

HI Alex,

Iremoved the SPSecurity.RunWithElevatedPrivileges but the error was also appearing.

Also, i was looking at the code and we are using the below but i dont know if it is getting disposed or not:
string sendEmailToApprover = workflowProperties.Site.RootWeb.AllUsers[assignedTo].Email;

Thanks,
Alain

23 01 2009
alex350r

Hi Alain,

Ok what I would be tempted to do is wrap that email string inside a using statement just in case, so:

Using (Spweb web = workflowproperties.site.rootweb)
{
string sendEmailToApprover = web.AllUsers[assignedto].Email;
}

Cheers

Alex

15 10 2010
Jonas Durston

When this problem plagues SPD workflows with no custom code whatsoever, what should we do?

19 10 2010
alex350r

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: