We have a custom WCF based service for automating some common tasks within our SharePoint 2007 environment. There is an issue with a piece of code that modifies the title of a SharePoint web. The code is very simple and works fine from a console app:
public void ChangeTitle(SPWeb web, string title)
{
web.Title = title;
web.Description = “Description for “ + title;
web.Update();
}
When the diagnostic logging for the SharePoint farm has the trace log settings set to “verbose”, the call to the web.Update() method throws a NullReferenceException.
Using .NET Reflector, I have tracked this down to the SPUtility.StackTraceString() method, which is only called if the trace logging is set to verbose. This method builds up a call stack string which is then written to the ULS logs. It only succeeds in building some of the string as it seems to expect more method calls in the stack than there actually are. Once it gets to the method exposed as the WCF operation, there are no methods left in the stack and so the null reference exception is thrown.
The stack trace of the error finishes as follows...
System.NullReferenceException was caught Message="Object reference not set to an instance of an object." Source="Microsoft.SharePoint" StackTrace: at Microsoft.SharePoint.Utilities.SPUtility.StackTraceString(Int32 numLevelsToSkip) at Microsoft.SharePoint.SPWeb.TraceWebOperation(String operation, Boolean recordStack) at Microsoft.SharePoint.SPWeb.Update() ...
The StackTraceString method that throws the exception looks like this:
The exception is thrown when trying to access "DeclaringType" after a few iterations (once the WCF boundary has been reached).
The error disappears when trace logging is set to something other than verbose as the code that throws the exception is contained within an "if(verbose){}" style block within the SPWeb.TraceWebOperation() call.
Aside from never setting verbose tracing (as we won't be able to control this in production), is there something else that we can try to get this to work?