Reputation: 3304
I've having a very simple WCF service (a console application for file upload). I keep getting error (400) bad request. It works when I upload small files (4kb) but failing for 700kb.
From the readings I've done from stack overflow and other, I'll have to increase the MaxReceivedMessageSize. This was implemented using a custom class and overriding the OnOpening method but it still didn't work.
I'm testing with a console application using the webclient
outputBytes = webClient.UploadData(baseUrl + "/uploads/2." + filenameOnly, File.ReadAllBytes(filename));
Also, I'm using the WebServiceHost as in
var uri = new Uri("http://localhost:8000");
var svc = new WebServiceHost(typeof (UploadService), uri);
How do I solve this issue? PS: Application does not have a config file so I'll looking at how to set this in code. If not and a config file is needed, then what should be the content.
Notes: I found this link Bad Request Error 400 - WCF Client where it was explained that those properties are only valid for soap based services. He suggested updating the web.config. Since this is a console application, I'm wondering how this can be done'
regards.
Upvotes: 0
Views: 3173
Reputation: 1463
You can set the maximumreceivedmessage programatically like this:
var binding = new wsHttpBinding(); // or whatever binding you are using
binding.MaxReceivedMessageSize = Int32.MaxValue;
var wcfClient = new WCFServiceTestClient(binding, strServiceURL);
Hopefully this will be enough to solve your problem, but you might also want to consider chopping up the file into bits (batching) before sending it to the server. Sending very large chunks of data can incurr a huge memory penalty as the client has to serialize the entire thing up front before sending it to the server (and then the same to deserialize it on the server side).
Edit: Based on your additional comment (below), the code on the application hosting the service might look something like this:
WebServiceHost webServiceHost = new WebServiceHost(typeof(UploadService), uri);
WebHttpBinding binding = new WebHttpBinding();
binding.MaxReceivedMessageSize = Int32.MaxValue;
webServiceHost.AddServiceEndpoint(typeof(IUploadService), binding, "WebServiceHost");
webServiceHost.Open();
Upvotes: 1
Reputation: 50682
Did you have a look at the documentation?
It covers both code and configuration.
Upvotes: 1