413 Request Entity Too Large in wcf service when uploading large image file - wcf

i have created wcf Service in which i am receiving Base64 string and converting base64 string to image to store in my project.
but when i am calling my wcf service method from rest client like this i am getting error like this:
413 Request Entity Too Large
and sometimes nothing happens when i am calling my wcf method from rest client.
above is how i am calling my wcf service method.
above base 64 string is an image of size 177Kb.
but when i am passing small base64 string of image with size 2 kb or 3 kb then my wcf service method is calling.
this is my web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
i have search alot on internet and added this:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<readerQuotas maxDepth="200" maxStringContentLength="8388608" maxArrayLength="16384" maxBytesPerRead="2147483647" maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
but error is still not resloved.
can anybody please help me???

While you have defined a basicHttpBinding with larger than default values, you have not told the WCF service to actually use that binding. When no service endpoints are defined, the default out of the box binding is basicHttpBinding with the default values.
You can either set your defined binding as the default binding configuration for any services using basicHttpBinding that use that config file by omitting the name attribute in the binding definition, like this:
<bindings>
<basicHttpBinding>
<binding maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas maxDepth="200"
maxStringContentLength="8388608"
maxArrayLength="16384"
maxBytesPerRead="2147483647"
maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
OR you can assign the defined binding to an explicit endpoint, which would look something like this:
<services>
<service name="MyService">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService"
contract="MyNamespace.IServiceContract" />
</endpoint>
</service>
A third place to check is the IIS maximum request length:
<configuration>
<system.web>
<httpRuntime maxRequestLength="2147483647" />
</system.web>
</configuration>
If you're uploading very large files you may also want to look at chunking or streaming.
Whole <system.serviceModel>
Option 1: With default binding defined (omitting name attribute from binding configuration)
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas maxDepth="200"
maxStringContentLength="8388608"
maxArrayLength="16384"
maxBytesPerRead="2147483647"
maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Option 2: With explicit endpoint using specified binding configuration via endpoint's bindingConfiguration attribute:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas maxDepth="200"
maxStringContentLength="8388608"
maxArrayLength="16384"
maxBytesPerRead="2147483647"
maxNameTableCharCount="16384" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="MyService">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService"
contract="MyNamespace.IServiceContract" />
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Either one of these options should resolve the issue. If they don't, try the third option I gave in my original answer.

Related

Issue with handling large byte array on WCF service side

Good afternoon. There is a problem passing a large array size in a request to a WCF service.
I am getting an 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.
All the answers I found boil down to increasing maxArrayLength in the web.config file, but it doesn't help.
My config.file:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.8" />
<httpRuntime targetFramework="4.8"/>
</system.web>
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="EdmBehaviorExtensionElement" type="Bis.Edm.Service.Interceptors.EdmBehaviorExtensionElement, Bis.Edm.Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EdmInspertorBehavior">
<EdmBehaviorExtensionElement />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpsBinding>
<binding name="basicHttpsBinding_Edm" closeTimeout="00:01:30" openTimeout="00:01:30" sendTimeout="00:01:30" receiveTimeout="00:01:30"
messageEncoding="Mtom" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="Transport" />
</binding>
</basicHttpsBinding>
</bindings>
<services>
<service name="Bis.Edm.Service.EdmService">
<endpoint binding="basicHttpsBinding" contract="Bis.Edm.Service.IEdmService" bindingConfiguration="basicHttpsBinding_Edm" behaviorConfiguration="EdmInspertorBehavior"/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
My service request model:
[DataContract]
public class EdmRequestModel
{
[DataMember]
public byte[] Data { get; set; }
}
PS. If you pass a small array in the request, everything works fine
Please tell me where I make a mistake, please forgive me for my possible mistakes in the text :)

InvalidOperationException: Client found response content type of '', but expected 'text/xml'. The request failed with an empty response

i just created one web application that deal with WCF Service. Now in linqpad5 i just load all methods from this class. and just write to just test output received from this service or not.
here is my pc's screenshot:
but at time of executing small program i just got this error like :
InvalidOperationException: Client found response content type of '', but expected 'text/xml'.
The request failed with an empty response.
for more information i just include hosted wcf service config :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" defaultLanguage="c#" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" requestValidationMode="2.0" />
<pages validateRequest="false" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="LargeWeb" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="RestServiceImplClient">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeWeb" contract="IRestServiceImpl" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebHttp">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
<staticContent>
<mimeMap fileExtension=".svc" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>
what going wrong here... please help me out of this guys...
update
this service working with iis browsing :

WCF Request Entity Too Large Driving Me Crazy

My config file is here.
Looked other question tried everything but ...
Why i cant set maxRequestSize etc. That is not working.
It gives an error that "Request Entity Too Large"
Can anyone help me, please?
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="RST_SSDOP_TO_LOGO.Service.Server">
<endpoint name="EndPoint1" binding="basicHttpBinding" bindingConfiguration="qwerty" contract="RST_SSDOP_TO_LOGO.Service.IServer"></endpoint>
</service>
</services>
<client>
<endpoint address="http://192.168.1.249:52215/Server.svc" binding="basicHttpBinding"
bindingConfiguration="qwerty" contract="RST_SSDOP_TO_LOGO.Service.IServer"
name="EndPoint1" kind="" endpointConfiguration="" />
</client>
<bindings>
<basicHttpBinding>
<binding name="qwerty" closeTimeout="01:00:00" maxBufferPoolSize="2147483647"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
I guess you receive this error on client side.
remember you have to set the max size at client side!!!
<binding name="SoapBinding" maxReceivedMessageSize="2000000" />

Configure MaxClockSkew in wsHttpBinding with TransportWithMessageCredential Security Mode

I have the following configuration for my service that is working fine.
The problem appears when i need to change the value of MaxClockSkew in wsHttpBinding with TransportWithMessageCredential security mode.
How can I change the MaxClockSkew value in this configuration?
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="HttpsService_wsHttpBinding" closeTimeout="01:10:00"
openTimeout="01:10:00" receiveTimeout="Infinite" sendTimeout="Infinite"
maxBufferPoolSize="999999999" maxReceivedMessageSize="99999999">
<readerQuotas maxDepth="999999999" maxStringContentLength="999999999"
maxArrayLength="999999999" maxBytesPerRead="999999999" maxNameTableCharCount="999999999" />
<reliableSession inactivityTimeout="Infinite" enabled="true" />
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="WcfServiceApp.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="HttpsService_wsHttpBinding"
name="wsHttpEndPoint" contract="CommonTypes.IService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="CommonTypes.CustomValidator, CommonTypes" />
</serviceCredentials>
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
<bufferedReceive maxPendingMessagesPerChannel="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<diagnostics wmiProviderEnabled="true">
<messageLogging
logEntireMessage="true"
logMalformedMessages="true"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="true"
maxMessagesToLog="3000"
/>
</diagnostics>
</system.serviceModel>
Based on the information in this post, it appears you can't change the maxClockSkew property on a standard (i.e., WCF-provided) binding, you need to use a custom binding. Something like this:
<system.serviceModel>
<bindings>
<customBinding>
<binding name="HttpsService_wsHttpBinding"
closeTimeout="01:10:00"
openTimeout="01:10:00"
receiveTimeout="Infinite"
sendTimeout="Infinite">
<reliableSession inactivityTimeout="Infinite"
enabled="true" />
<security authenticationMode="SecureConversation">
<secureConversationBootstrap authenticationMode="UserNameOverTransport" />
<localServiceSettings maxClockSkew="00:30:00" />
</security>
<textMessageEncoding messageVersion="soap12">
<readerQuotas maxDepth="999999999"
maxStringContentLength="999999999"
maxArrayLength="999999999"
maxBytesPerRead="999999999"
maxNameTableCharCount="999999999" />
</textMessageEncoding>
<httpsTransport maxBufferPoolSize="999999999"
maxReceivedMessageSize="99999999" />
</binding>
<customBinding>
</bindings>
</system.serviceModel>
Note that the clock skew is set in the <security> section with the <localServiceSettings> element's attribute maxClockSkew (in this example, 30 minutes). Custom bindings can be a little intimidating and/or confusing at first, but careful examination of the above example and using MSDN will be helpful.
CustomBindings is a good place to start, and note that the article indicates a specific order for the elements.
For an overview of the config section elements and attributes, you can look here: <customBinding>.
You will also need to set the maxClockSkew property on the client's custom binding to the same value as the service, I believe.

The remote server returned an unexpected response: (400) Bad Request WCF REST

I found similar posts, but everything I've tried doesn't work. Can you please tell me what am I doing wrong? First is my client config and second my server config.
First I thought that it is because I try to receive a large amount of data, but that wasn`t the problem in my case.
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime maxRequestLength="32768" targetFramework="4.5" />
</system.web>
<system.serviceModel>
<client>
<endpoint address="http://localhost:56923/TestService.svc?wsdl"
behaviorConfiguration="webBehavior"
binding="webHttpBinding"
bindingConfiguration="webHttpBinding"
contract="TestService.TestService"
name="WcfServiceApp.TestService" />
</client>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding"
maxBufferSize="2097151"
maxBufferPoolSize="2097151"
maxReceivedMessageSize="2097151">
<readerQuotas maxDepth="128"
maxStringContentLength="2147483647"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
</system.webServer>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime maxRequestLength="32768" targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding"
maxBufferSize="2097151"
maxBufferPoolSize="2097151"
maxReceivedMessageSize="2097151">
<readerQuotas maxDepth="128"
maxStringContentLength="2097151"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
One thing that jumps out is the <client> configuration in the client's config, specifically the address attribute.
address="http://localhost:56923/TestService.svc?wsdl"
First, you said you're using REST, so the WSDL file doesn't play a role in that (it's for SOAP services).
Second, the WSDL file is not the service, it's the Web Service Description Language file, which describes the web service so clients can build a proxy to it (again, this is for SOAP, not REST).
Try changing the address in your client config to just the service file:
address="http://localhost:56923/TestService.svc"