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);
Can you replace your standardEndpoint element with the below:
Hope that helps.