Using Silverlight to upload more than 8KB of data from a WCF Service

Recently, I encountered an issue where one of my customer was trying to upload more than 8KB (8192 characters) of data from a WCF Service which was consumed by a Silverlight client. The Silverlight application would work fine, till the data upload was limited to 8192 characters, but just increasing it to 8193 characters would throw the following error-

"An exception occurred during the operation, making the result invalid. Check the InnerException for exception details."

If we step through the code the InnerException looks like the following-

System.ServiceModel.CommunicationException was unhandled by user code
Message=The remote server returned an error: NotFound.
StackTrace:
at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
at WCFTest.ServiceReference.TestServiceClient.TestServiceClientChannel.EndDoWork(IAsyncResult result)
at WCFTest.ServiceReference.TestServiceClient.WCFTest.ServiceReference.ITestService.EndDoWork(IAsyncResult result)
at WCFTest.ServiceReference.TestServiceClient.OnEndDoWork(IAsyncResult result)
at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)
InnerException: System.Net.WebException
Message=The remote server returned an error: NotFound.
StackTrace:
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
InnerException: System.Net.WebException
Message=The remote server returned an error: NotFound.
StackTrace:
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
InnerException:

I found many posts which gave solution to the issue and said following those would help one upload more than 8KB of data, but none seemed to help. One, thing was clear, the solution had to do with changing the WCF Service configuration.

On doing much of research, I got a the correct solution, which is as follows-

A) In the Visual Studio open the solution which exhibits the issue. For now let’s call it WCFTest.

B) Go to Tools -> WCF Service Configuration Editor.

C) Click File -> Open and browser to the Web.Config for the WCFTest.Web project.

D) Select Bindings -> New Binding Configuration.

E) Select basicHttpBinding and give a name for example - LargeBinding.

F) Select LargeBinding under Bindings and change MaxBufferPoolSize, MaxBufferSize, MaxReceivedMessageSize, MaxArrayLenght, MaxBytesPerRead to 2147483647.

G) Select Services -> WCFTest.Web.TestService -> Endpoints -> Select the binding which is used to bind the WCF Service, by default. In this case, let's assume it to be TestBinding. Change the BindingConfiguration to LargeBinding.

H) Save the Web.Config.

I) Open the ServiceReference.ClientConfig file in WCF Service Configuration Editor.

J) Go to Bindings -> TestBinding (basicHTTPBinding) and change the MaxBufferSize and MaxReceivedMessageSize to 2147483647.

K) Save the ServiceReference.ClientConfig file and exit out of WCF Service Configuration Editor.

L) Build the solution and now the WCF Service should be able to upload more than 8K of data.

Hope this helps!!!!

Content By – Sandeep Chalke