2010-12-10

ViewState problems with Sandbox Web Parts in SharePoint 2010

I have been playing around with developing sandbox web parts for SharePoint 2010 Foundation. With one particular web part, i had a form that was pretty large, over 300 fields. Initially i had no problems with the form loading and submitting. Gradually i started to convert some of the fields to dropdownlists, and databinding not insignificant datasets. At some point the form started to still display, but would not submit. I kept getting the generic error.

Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred.

My first attempt to solve this, was to locate the error. I try/catched all of the UI events. Strangely the exception was not being caught. Odd. Next i tried disabling sections of logic in web part and found that if i disabled the databind logic, i got no error. I figured it must have been one of the datasets causing an issue (but really that was a wrong assumption now that i know the true cause). So I continued on. I had to spend a while finding which dropdown sets were causing the problem, but i did narrow it down to one dataset, which also happened to be the largest. Hmm the largest, i had a feeling it might have something to do with a httppost max size at this point. So i reduced the dataset to only 50 records. Sure enough the error went away. With a few tests and page view sources, i found that the error would happen when the form size went over 128KB no matter what dataset had the largest amount of records.

I then decided to look at the ULS logs (which i probably should have done first) and had this error trace


SPUCWorkerProcess.exe (0x165C) 0x1744 SharePoint Foundation Sandboxed Code Service fe8s Medium - - Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred. - userCodeWrapperType = "Microsoft.SharePoint.UserCode.SPUserCodeWebPartWrapper", userAssemblyGroupId = "E8C0D7A9294B4BCCB63757BF4AB4BC47-UCftMMyPOXDR6ibqgsTcXIE7gh71nqlnnuite7AdBB8=", siteCollectionId = "c02189c4-1dea-4969-b2b8-8240dfe4ef65" - Inner Exception: Microsoft.SharePoint.UserCode.SPUserCodeSolutionProxiedException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> Microsoft.SharePoint.UserCode.SPUserCodeSolutionProxiedException: Value cannot be null. Parameter name: path1 at System.IO.Path.Combine(String path1, String path2) at System.Web.HttpRawUploadedContent.TempFile..ctor() at System.Web.HttpR...
SPUCWorkerProcess.exe (0x165C) 0x1744 SharePoint Foundation Sandboxed Code Service fe8s Medium ...awUploadedContent.AddBytes(Byte[] data, Int32 offset, Int32 length) at System.Web.HttpRequest.GetEntireRawContent() at System.Web.HttpRequest.FillInFormCollection() at System.Web.HttpRequest.get_Form() at System.Web.HttpRequest.get_HasForm() at System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) at System.Web.UI.Page.DeterminePostBackMode() at Microsoft.SharePoint.UserCode.SPUserCodePage.DeterminePostBackMode() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- Server stack trace: at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean inc...
SPUCWorkerProcess.exe (0x165C) 0x1744 SharePoint Foundation Sandboxed Code Service fe8s Medium ...ludeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at Microsoft.SharePoint.UserCode.SPUserCodeWebPartWrapper.ExecuteHttpRequest(SPUserCodeWebPartHttpRequestContext webPartExecutionContext, SPUserCodeWebPartHttpResponse httpRequestResponse) at Microsoft.SharePoint.UserCode.SPUserCodeWebPartWrapper.Execute(SPUserCodeExecutionContext executionContext) at Microsoft.SharePoint.UserCode.SPUserCodeApplicationHostAppDomainRef.ExecuteWrapper(SPUserCodeWrapper wrapper, SPUserCodeExecutionContext executionContext) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage...
SPUCWorkerProcess.exe (0x165C) 0x1744 SharePoint Foundation Sandboxed Code Service fe8s Medium ...(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.EndInvokeHelper(Message reqMsg, Boolean bProxyCase) at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(Object NotUsed, MessageData& msgData) at Microsoft.SharePoint.UserCode.SPUserCodeApplicationHostAppDomainRef.CodeToExecuteWrapper.EndInvoke(IAsyncResult result) at Microsoft.SharePoint.UserCode.SPUserCodeApplicationHostAppDomainRef.Execute(Type userCodeWrapperType, SPUserCodeCachedAssemblyGroup userAssemblyGroup, Guid siteCollectionId, Byte[] binaryUserCodeToken, Byte[] proxyO...
SPUCWorkerProcess.exe (0x165C) 0x1744 SharePoint Foundation Sandboxed Code Service fe8s Medium ...perationToken, SPUserCodeExecutionContext executionContext)
SPUCHostService.exe (0x12E4) 0x14CC SharePoint Foundation Sandboxed Code Service fe3r Medium - - Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred. - Monitored process "ipc://f28fa0e5-d648-4c74-a67f-4ae8dc6f1856:7000" has encountered an unhandled exception while executing user code. - Inner Exception: Microsoft.SharePoint.UserCode.SPUserCodeSolutionProxiedException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> Microsoft.SharePoint.UserCode.SPUserCodeSolutionProxiedException: Value cannot be null. Parameter name: path1 at System.IO.Path.Combine(String path1, String path2) at System.Web.HttpRawUploadedContent.TempFile..ctor() at System.Web.HttpRawUploadedContent.AddBytes(Byte[] data, Int32 offset, Int32 length) at System.Web.HttpRequest.GetEntire...
SPUCHostService.exe (0x12E4) 0x14CC SharePoint Foundation Sandboxed Code Service fe3r Medium ...RawContent() at System.Web.HttpRequest.FillInFormCollection() at System.Web.HttpRequest.get_Form() at System.Web.HttpRequest.get_HasForm() at System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull) at System.Web.UI.Page.DeterminePostBackMode() at Microsoft.SharePoint.UserCode.SPUserCodePage.DeterminePostBackMode() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- Server stack trace: at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, ...
SPUCHostService.exe (0x12E4) 0x14CC SharePoint Foundation Sandboxed Code Service fe3r Medium ...Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at Microsoft.SharePoint.UserCode.SPUserCodeWebPartWrapper.ExecuteHttpRequest(SPUserCodeWebPartHttpRequestContext webPartExecutionContext, SPUserCodeWebPartHttpResponse httpRequestResponse) at Microsoft.SharePoint.UserCode.SPUserCodeWebPartWrapper.Execute(SPUserCodeExecutionContext executionContext) at Microsoft.SharePoint.UserCode.SPUserCodeApplicationHostAppDomainRef.ExecuteWrapper(SPUserCodeWrapper wrapper, SPUserCodeExecutionContext executionContext) at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) ...
SPUCHostService.exe (0x12E4) 0x14CC SharePoint Foundation Sandboxed Code Service fe3r Medium ... at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.EndInvokeHelper(Message reqMsg, Boolean bProxyCase) at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(Object NotUsed, MessageData& msgData) at Microsoft.SharePoint.UserCode.SPUserCodeApplicationHostAppDomainRef.CodeToExecuteWrapper.EndInvoke(IAsyncResult result) at Microsoft.SharePoint.UserCode.SPUserCodeApplicationHostAppDomainRef.Execute(Type userCodeWrapperType, SPUserCodeCachedAssemblyGroup userAssemblyGroup, Guid siteCollectionId, Byte[] binaryUserCodeToken, Byte[] proxyOperationToken, SPUserCodeExecutionContext executionContext)


The key text being

Value cannot be null.
Parameter name: path1
at System.IO.Path.Combine(String path1, String path2)
at System.Web.HttpRawUploadedContent.TempFile..ctor()

I have seen something like this before, when trying to use file uploads in a sandbox web part. Basically the HTTP object in the SPUCWorkerProcess is not fully functional. You can see how the process works from here http://msdn.microsoft.com/en-us/library/ff798382.aspx. So what was this error telling me, and how did it relate to the 128KB limit i was seeing? I had a guess that at this limit, the http object stopped buffering the request in memory and started writing it to a temporary file on disk. Took a few google searchs to find the appropriate key to set to increase this limit http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.requestlengthdiskthreshold.aspx. I set this on the sharepoint vss web.config. But this didnt fix the problem.

Both of the sandbox processes are well ... processes. SPUCHostService.exe/SPUCHostService.exe. I doubted that they would have httpRuntime settings i could change. But i went to the folder to see. Inside the folder i found an extra web.config (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\UserCode\web.config), maybe i could use that. I added and tried again. To my surprise it fixed the problem.

Well it fixed the problem for my local machine, too bad that solution would not work on a hosting provider. There would be little chance they would change their UserCode settings for me. My next question was, why was so much data being sent the to sandbox process in the first place. Remember this only happens when the form is submitted. I took a guess that it was related to the viewstate, so tried disabling that, and bingo the problem went away. It does mean though that i'm going to have to manually deal with the postback binds, but at least the form is not crashing now.