I have a WCF method that accepts arbitrary binary data which the service will save to a file on the filesystem. Here is the WCF method:
[OperationContract]
[WebInvoke( Method="PUT", UriTemplate="/products/{productId}/resources/{resourceName}")]
public void PutResource(String productId, String resourceName, Stream resource)
However whenever my clients (a WinForms HttpWebRequest contraption) attempt to make this request the system (either IIS or WCF or something in the chain) responds with HTTP 400 Bad Request with an empty body (the content-length is set to zero anyway). I set a breakpoint on my WCF method and the method is never called, in fact I get the same error when I call the same URI with the method commented out entirely.
I set another breakpoint in my ProvideFault method in my IErrorHandler class, but that is never called, which suggests something is going wrong before WCF is invoked.
I just tried the method again, but with a smaller request resource (a text stream, 3 bytes long) an开发者_运维问答d that actually worked fine, it seems whenever I use anything larger than a few kilobytes it fails.
I can't think of what to change to get this to work - any suggestions?
EDIT: I'm using the WCF REST Starter Kit 2, which uses CustomBinding under the hood which doesn't seem to use the MaxReceivedMessageSize property (and the default to that is 65536, but my method fails with requests over a few KB).
EDIT2: It seems after rebuilding my project and testing a variety of files, that the limit is actually 64KB. Now to find out where that 64KB limit is being specified...
I solved it. Here's the code I added to my WebServiceHost2 subclass to fix the problem:
protected override void OnOpening() {
base.OnOpening();
foreach(ServiceEndpoint ep in base.Description.Endpoints) {
if( ep.Binding is CustomBinding ) {
CustomBinding cb = (CustomBinding)ep.Binding;
foreach(BindingElement e in cb.Elements) {
if( e is HttpTransportBindingElement ) {
HttpTransportBindingElement h = (HttpTransportBindingElement)e;
h.MaxReceivedMessageSize = Int32.MaxValue; // "2GB should be enough for anyone".
}
}
}
精彩评论