Error 413 Request Entity Too Large - c#

I am running into the error (413) Request Entity Too Large. I have done some searching around and all I can see is that the maxRecievedMessageSize needs to be added to the binding and that binding needs to be added in the bindingConfiguration. I have taken those steps on both my client web.config and service web.config.
Client
<bindings>
<basicHttpBinding>
<binding name="secureHttpBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
...
<endpoint address="https://mysite.com/WcfServices/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="secureHttpBinding"
contract="MyService.IMyService" name="BasicHttpBinding_IMyService" />
WCF
<basicHttpBinding>
<binding name="secureHttpBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
I do not have any <service> tags specified explicitly in my WCF web.config, but I have had success before not explicitly defining the tag. Are there any other issues that could be in play here?

When using SSL/TLS, IIS can, under certain circumstances, buffer the entire HTTP request before passing it off to WCF. The default size of this buffer is only 48k, and if the buffer is exceeded IIS will return a 413 error.
To work around this, the size of the buffer has to be increased via the uploadReadAheadSize setting. In IIS 7.5, you can get to this setting via IIS Manager:
Select the web site in question under "Sites".
Launch "Configuration Editor".
Select Section: system.webServer/serverRuntime, From: ApplicationHost.config.
Set uploadReadAheadSize to the desired buffer size in bytes. Make sure it's larger than the largest request you might send.
Click "Apply".
For other IIS versions this setting may be in a different place.
There can be other reasons for getting a 413 as well, but this is the one I've run into. Of course, if you're not hosting the service in IIS this obviously wouldn't apply.

The basicHttpBinding you have defined in the service config file is not being used because it is not assigned to an endpoint. In WCF 4.0+, you will get default endpoints with default bindings if you don't explicitly specify an endpoint - and the default binding for http is basicHttpBinding. But this also means you will get the default values for the binding.
You can either add a service endpoint explicitly and assign your binding to it, or you can make your definition the default definition for basicHttpBinding by omitting the name attribute in the definition, like this:
<basicHttpBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647"/>
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
The above config snippet will then set your definition for basicHttpBinding as the default for any services uses basicHttpBinding in that application.

Related

SOAP redirect to another web service

I am consuming a SOAP Web Service through a client auto generated with the "Add Service Reference..." wizard in Visual Studio.
XML configuration on web.config like this :
<wsHttpBinding>
<binding name="ServiceBinding" allowCookies="true" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="52428800">
<readerQuotas maxDepth="52428800" maxStringContentLength="52428800" maxArrayLength="52428800" maxBytesPerRead="52428800" maxNameTableCharCount="52428800" />
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
I also pass an authorization header, and the service I call is in https
All working
Later I had to implement a redirect to another site without touching the client code
Server side then I made a 307 and changed the RedirectLocation of the ttpContext.Current.Response
The call to the other site is made but does not carry the authorization header
Situation: error - 401 not authorized
I also tried to make the other site in http, so that it is not necessary to pass the authorization header
With this XML configuration on web.config :
<wsHttpBinding>
<binding name="ServiceBinding" allowCookies="true" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="52428800">
<readerQuotas maxDepth="52428800" maxStringContentLength="52428800" maxArrayLength="52428800" maxBytesPerRead="52428800" maxNameTableCharCount="52428800" />
<security mode="None"> </security>
</binding>
</wsHttpBinding>
Situation : Error because when the call comes back it violates the contract
I can make the second site in http / https but the first site of origin must necessarily be in https
How can I do ?
Thanks in advance

WCF Memory Increases dramatically

I am using WCF service. The problem I have is its starts using double memory.
I am using HTTPS binding
<wsHttpBinding>
<binding name="secureHttpBinding" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="Transport" >
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
<endpoint address="https://localhost/test.svc"
binding="wsHttpBinding"
bindingConfiguration="secureHttpBinding"
contract="IWcfContract"
name="SecureBasicHttpBinding_WcfContract">
Here is the code I am using to upload
using (Stream fileStream = File.OpenRead(logsZipFullPath))
{
// Call web server
UploadFileResponse response = _webServiceHelper.UploadFile(fileStream, currentDate, ".zip", string.Empty);
fileStream.Close();
}
Here is my model
[MessageContract]
public class UploadFileRequest : IDisposable
{
[MessageBodyMember(Order = 1)]
public Stream FileByteStream;
[MessageHeader(MustUnderstand = true)]
public string FileDescription;
}
My zip file is of 80MB.
The problem I have is at the start of the service its using 26mb which is quite fine. At the first call it uses 136MB after call completes it goes down to 26mb. which is also fine. after the second call to upload it starts using 346MB
which again gets down to 26mb after service call. My question is why it is using 346MB when the file is of only 80MB? My GC and disponse has been called correctly. But, is this normal behaviour or I am missing anything?
finally found a work around for this. According to this post
wsHttpBinding is the full-blown binding, which supports a ton of WS-*
features and standards - it has lots more security features, you can
use sessionful connections, you can use reliable messaging, you can
use transactional control - just a lot more stuff, but wsHttpBinding
is also a lot *heavier" and adds a lot of overhead to your messages as
they travel across the network
I think this is the main reason why the memory usage is high. Our requirement was to use the HTTPS certificate and we are able to use this with basicHttpBinding. SO, I changed my settings like this
<basicHttpBinding>
<binding name="BasicHttpBinding_WcfContract" closeTimeout="04:01:00" openTimeout="04:01:00" receiveTimeout="04:10:00" sendTimeout="04:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed" useDefaultWebProxy="true">
<readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="Transport"> <-- this is the main change
<transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
Note: I changed security mode none to transport to support https.
Now, everything works without the issue.
Two possible things which causes memory issues.
wsHttpBinding overheads
Stream mode is not supported in wsHttpBinding

wsDualHttpBinding not sending request to server

i'm currently working on a client/server program which is using wshttpdualbinding for communication in vs with c#. So i'm calling webservices over this binding from the server.
Everything worked fine, but in the last days i'm encountering the following problem: if i call a service from the server, the client just stops working and stops reacting. Nothing gets send from the client.
I can't post much, because it is a big project, but i'll try to post the best snippets:
binding:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_AuthenticationService" closeTimeout="03:10:00"
openTimeout="03:10:00" receiveTimeout="03:10:00" sendTimeout="03:10:00"
bypassProxyOnLocal="false" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
useDefaultWebProxy="true" allowCookies="false" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_Service" closeTimeout="03:10:00"
openTimeout="03:10:00" receiveTimeout="03:10:00" sendTimeout="03:10:00"
bypassProxyOnLocal="false" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
useDefaultWebProxy="true" messageEncoding="Text" textEncoding="utf-8" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</wsDualHttpBinding>
</bindings>
problem causing code:
Clusterrationalemapping cr = new Clusterrationalemapping
{
Textbricks = rationale,
Cluster = cluster,
Project = CurrentProject
};
var clusterrationaleId = 0;
if (cluster.AddClusterRationaleMapping(cr))
{
clusterrationaleId = _service.SaveItem(cr);
cr.Id = clusterrationaleId;
if(rationale.Clusterrationalemappings == null) rationale.Clusterrationalemappings = new List<Clusterrationalemapping>();
rationale.Clusterrationalemappings.Add(cr);
}
A function gets called which creates a new clusterrationalemapping for a specific cluster. The rational and CurrentProject variables are always the same. just cluster is different.
Most of the time everything works and service.SaveItem returns the new Id as expected. But for one special cluster it is not working. service.SaveItem just stops and freezes the tool until i get some exception (server did not respond in time/gave no answer).
I'm sure that the wsDualHttpBinding is set correctly. If i comment out the line Project = CurrentProject and replace it with Project = new Project(); everything works fine for this cluster again.
So it looks like clusterrationalmapping might get too big for sending. But the exception is not saying that. And i got it from the server - from that direction it worked.
I used Microsoft Service Trace Viewer for debugging purpose and i can see there, that it instantly throws an exception that the endpoint could not be reached. So i'm pretty sure that the service dies when serializing the object.
Is there any way to debug into the service to see when exactly, or better why, it fails?
Thanks in advance for your help.
Since I have not been able to find a solution - I still think that there has been a problem with the serializer and a too big/nested class, i changed the binding to nettcp and it is working now.

maxArrayLength issue in silverlight

I have been reading lots of articles on this issue for long, but i haven't got it worked.
Server Side web.config file
service.clientconfig
client side asp.net web.config
Still getting the following error
The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.
Any help..
you should use this................
<bindings>
<basicHttpBinding>
<binding name="HttpBinding" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" receiveTimeout="00:10:00" sendTimeout="00:10:00">
<readerQuotas maxDepth="700" maxArrayLength="2147483647" maxStringContentLength="2147483647" />
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
I changed all the sizes to 2097152 instead of 2147483647 and it started working.
I have configured the web config for allowing 2MB. But it is throwing the same exception if i upload more than 1MB file.

Getting started with Paypal Adaptive Payments in C# SOAP

I'm trying to start with Adaptive Payments by Paypal using SOAP interface.
When adding service reference to https://svcs.sandbox.paypal.com/AdaptivePayments?WSDL the following warning is shown by Visual Studio:
Custom tool warning: Cannot import wsdl:binding
Detail: The WSDL binding named AdaptivePaymentsSOAP11Binding is not valid because no match for operation CancelPreapproval was found in the corresponding portType definition.
XPath to Error Source: //wsdl:definitions[#targetNamespace='http://svcs.paypal.com/services']/wsdl:binding[#name='AdaptivePaymentsSOAP11Binding'] C:\cproj\daemon\Service References\PaypalSandboxApi\Reference.svcmap 1 1 daemon
Discarding this message, the reference added successfully.
In order to perform a transaction, I try to create the client:
var client = new PaypalSandboxApi.AdaptivePaymentsPortTypeClient()
This throws InvalidOperationException:
Could not find default endpoint element that references contract 'PaypalSandboxApi.AdaptivePaymentsPortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Am I missing something?
Should I use missing AdaptivePaymentsSOAP11Binding and not AdaptivePaymentsPortTypeClient?
It looks like importing this WSDL doesn't generate the servicemodel config. I kludged one together like this (and updated the relevant classname to match yours, so you can copy/paste):
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="PaypalAdaptivePayBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1048576" maxBufferPoolSize="1048576" maxReceivedMessageSize="1048576" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="65536" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://svcs.sandbox.paypal.com/AdaptivePayments"
binding="basicHttpBinding" bindingConfiguration="PaypalAdaptivePayBinding"
contract="PaypalSandboxApi.AdaptivePaymentsPortType"
name="PaypalAdaptivePay" />
</client>

Categories