Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am facing an issue while uploading a file using WCF service.
Below is the error message:

"The remote server returned an unexpected response: (400) Bad Request."

<br />
 protected void btnUploadDocument_Click(object sender, EventArgs e)<br />
        {<br />
            DRServiceReference.IDocumentService proxy = new DRServiceReference.DocumentServiceClient();<br />
            UploadDocumentRequest uploadDocumentRequest = new UploadDocumentRequest();<br />
            uploadDocumentRequest.DocumentName = txtFileName.Text;<br />
            uploadDocumentRequest.RepositoryId = 1;<br />
            uploadDocumentRequest.DocumentBytes = StreamFile(txtFileName.Text);<br />
            UploadDocumentResponse uploadDocumentResponse = null;<br />
            uploadDocumentResponse = proxy.UploadDocument(uploadDocumentRequest);<br />
            int documentId = uploadDocumentResponse.DocumentId;<br />
            lbldocumentId.Text = Convert.ToInt32(documentId).ToString();<br />
            if (uploadDocumentResponse.Errors != null)<br />
            {<br />
                string errorMessage = uploadDocumentResponse.Errors[0].Description;<br />
                lblError.Text = "Error : " + errorMessage;<br />
                lbldocumentId.Text = string.Empty;<br />
            }<br />
            if (uploadDocumentResponse.Warnings != null)<br />
            {<br />
                string warningDescription = uploadDocumentResponse.Warnings[0].Description;<br />
            }<br />
        }<br />
        #endregion<br />


Please suggest as I am not able to find solution for this. I have already increased the size of "maxReceivedMessageSize" in config file. It is working for small size files but giving error for files greater then 55Kb).
Posted

1 solution

As a first response, I would say that you should not do that. You should upload files in chunks. I have been using a WCF based file server to send multi-Gigabyte disk images across the Atlantic and Pacific. However you can increase your size to considerably larger than 55k. I have been working with 1mb chunks.

As is very often the case the problem probably lies in the configuration files. Here is a binding tag that works for recieving files of 1mb.

<netTcpBinding>
               <binding name="tcp" closeTimeout="00:01:00" openTimeout="00:01:00"
                   receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
                   transferMode="Buffered" transactionProtocol="OleTransactions"
                   hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                   maxBufferPoolSize="524288" maxBufferSize="6553600" maxConnections="10"
                   maxReceivedMessageSize="65536">
                   <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="1638400"
                       maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                   <reliableSession ordered="true" inactivityTimeout="00:10:00"
                       enabled="false" />
                   <security mode="Transport">
                       <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                       <message clientCredentialType="Windows" />
                   </security>
               </binding>
           </netTcpBinding>


maxBufferSize ,maxReceivedMessageSize and maxArrayLength are the one's you should be concerned with. You need to make sure that all of the components are willing to accept files of the size that you want to send. Also as a worthy tip, use the netTcpBinding. The performance difference is very significant.
I hope that this was helpful
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900