Germstorm
Germstorm

Reputation: 9839

Uploading text file to WCF RESTful service

I created a simple RESTful WCF service with a single method for receiving files and a simple client that uploads a file. The files are simple text files (of course I would like to upload binary files later).

The service will be published for many types of clients so I would like to use an implementation that does not add a service reference.

Am I doing this correctly?

Why do I get a The remote server returned an unexpected response: (400) Bad Request error when I try to upload a simple 1Mb text file?

Edit: The system works correctly if my file has only a size of 3.7kb. This means I just have to configure the service to accept larger packets. How do I do that?

Service:

    /// <summary>
    /// Data Service REST service that handles data upload.
    /// </summary>
    [AspNetCompatibilityRequirements(
    RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class DataService : Common.Contracts.IDataService
    {
        /// <summary>
        /// Uploads a data file to the server.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="fileContent">Content of the file.</param>
        public string UploadFile(string filename, Stream fileContent)
        {
            return filename;
        }
    }

Service config

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
        <webHttpEndpoint>
            <!--
        Configure the WCF REST service base address 
        via the global.asax.cs file and the default endpoint
        via the attributes on the <standardEndpoint> element below
    -->
            <standardEndpoint name="" helpEnabled="true"
             automaticFormatSelectionEnabled="true"/>
        </webHttpEndpoint>
    </standardEndpoints>
</system.serviceModel>

Contract:

    /// <summary>
    /// Data Service service contract interface.
    /// </summary>
    [ServiceContract]
    public interface IDataService
    {
        /// <summary>
        /// Uploads a data file to the server.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="fileContent">Content of the file.</param>
        [WebInvoke(UriTemplate = "/upload?filename={filename}", 
        Method = "POST", ResponseFormat = WebMessageFormat.Xml)]
        string UploadFile(string filename, System.IO.Stream fileContent);
    }

Client

WebChannelFactory<IDataService> cf = 
new WebChannelFactory<IDataService>(
                  new Uri(Settings.Default.UrlService));
IDataService channel = cf.CreateChannel();
StreamReader fileContent = new StreamReader(path, false);

string response = channel.UploadFile(path, fileContent.BaseStream);

Upvotes: 1

Views: 2444

Answers (2)

Rajesh
Rajesh

Reputation: 7876

Can you replace your standardEndpoint element with the below:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxBufferSize="500000" maxReceivedMessageSize="500000" >          
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />          
        </standardEndpoint>

Hope that helps.

Upvotes: 1

Jeff Mitchell
Jeff Mitchell

Reputation: 1479

Perhaps it has something to do with the maxReceivedMessageSize that you've got set in your config bindings.

Do you get the 400 when you upload something really small?

Upvotes: 0

Related Questions