ASP.NET hosting WCF Services, needs to increase MaxStringContentLength, but how? - asp.net

My ASP.NET server is providing a set of WCF services which are consumed by my WPF client. Everything was working just fine until the length of a string field exceeded 8K. This now generates the following exception on the ASP.NET server...
There was an error deserializing the object of type Project.ModelType.
The maximum string content length quota (8192) has been exceeded while
reading XML data. This quota may be increased by changing the
MaxStringContentLength property on the XmlDictionaryReaderQuotas
object used when creating the XML reader.
I have increased the value of the MaxStringContentLength to be 64K on the WPF app.config but this has not solved the issue. So I guess I need to increase this value on the ASP.NET side as well. But I do not have any values in the web.config to change! Here is my web.config to show this...
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms name=".ASPXFTOAUTH"
timeout="10"
slidingExpiration="true"
cookieless="UseCookies"/>
</authentication>
<membership>
<providers>
<clear/>
</providers>
</membership>
<customErrors defaultRedirect="~/Error.htm" mode="RemoteOnly">
<error statusCode="404" redirect="~/404.aspx" />
</customErrors>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
So how do I update the server to indicate the higher MaxStringContentLength value? The app.config for my service looks like this...
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAccess" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="131072" maxBufferPoolSize="524288" maxReceivedMessageSize="131072"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="65536" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IAccess"
contract="AccessService.IAccess"
name="BasicHttpBinding_IAccess" />
</client>
</system.serviceModel>
Any ideas?
UPDATE:
My services are defined by having a class 'Access.svc'
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Access : IAccess
{
// ...IAccess method implementations
}
...which has the following markup...
<%# ServiceHost Language="C#" Debug="true"
Service="Ecotech.AMS.WebServer.Access"
CodeBehind="Access.svc.cs" %>
...there is nothing specific about the service in the web.config, as noted in comments.

Try clicking "Add Service Reference" via the right-click menu of your Project. Doing it this way will add the necessary configuration information into your web.config file.
Once you do this, you can set the maxReceivedMessageSize property on your binding. Doing it this way will most accurately reflect what you are doing on the WCF service side of things.

The place where you need to work on is the web config, you need to add service behaviours where you can set the size of data. For example like this,
<behaviors>
<serviceBehaviors>
<behavior name="SilverlightWCFLargeDataApplication">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="SilverlightWCFLargeDataApplication">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
if that does not work, post your web config here.Hope it helps.

Related

WCF Service wsHttpBinding

I'm getting error while creating wcf service using wsHtpBinding help page is getting error and services also not working
Help Page Error:
The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
http://localhost:80/Service1.svc/help
My old working wsHttpbinding project web.config but it's not working now:
web.config
<?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" executionTimeout="3600" maxRequestLength="10000000"/>
<customErrors mode="Off" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding >
<binding name="MyBasicwsHttpBinding">
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="WcfService1.Service1">
<endpoint address="mex" binding="wsHttpBinding" bindingConfiguration="MyBasicwsHttpBinding" name="WcfService1.Service1" contract="WcfService1.IService1"/>
</service>
</services>
<serviceHostingEnvironment minFreeMemoryPercentageToActivateService="0" aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
In my case, I found out that a Microsoft IIS filtering module Url Scan 3.1 was installed on IIS/website, which have it's own limit to reject incoming requests based on content size and return "404 Not found page".
It's limit can be updated in %windir%\System32\inetsrv\urlscan\UrlScan.ini file by setting MaxAllowedContentLength to the required value.
For eg. following will allow upto 300 mb requests
MaxAllowedContentLength=314572800

Wrong connection strings returned

I've written a WCF service application.
When the project is generated, visual studio creates 3 web config files.
I've finished the project and to date I've been using a hard coded connection string within my GetOpenConnection() function, so I now want to move the connection string to the web.config files.
The following call returns null.
ConnectionStringSettings csSettings = ConfigurationManager.ConnectionStrings["PulseWcfConnectionString"];
When I run the following code it doesn't return the string set in my web.debug.config file.
for(int idx = 0; idx < ConfigurationManager.ConnectionStrings.Count; idx++)
Debug.WriteLine(ConfigurationManager.ConnectionStrings[idx].ConnectionString);
it returns the following 2 items, 2nd one is an empty string. I don't recognise the first line, maybe it's a default one?
data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
""
What am I missing please?
My web.debug.config contains the following which should be for a local sql server instance
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add
name="PulseWcfConnectionString"
connectionString="Data Source=WIN8-CLAIRE\SQLSRVDEV2008;Initial
Catalog=gcll;Persist Security Info=True;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
</configuration>
For now my web.release.config contains the same thing (it's being published to it's destination tomorrow so I'll change the details for it then)
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add
name="PulseWcfConnectionString"
connectionString="Data Source=WIN8-CLAIRE\SQLSRVDEV2008;Initial
Catalog=gcll;Persist Security Info=True;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Windows" />
</system.web>
<system.serviceModel>
<services>
<service name ="pulse.smartcentre.wcf.service.app.PulseWebService"
behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:52478/Design_Time_Addresses/pulse.smartcentre.wcf.service.app/PulseWebService/" />
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding"
bindingConfiguration="wsHttpBinding"
contract="pulse.smartcentre.wcf.service.app.IPulseWebService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<!-- CNH -->
<bindings>
<!-- Secure binding (to use) -->
<wsHttpBinding>
<binding name="wsHttpBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true" transactionFlow="true">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Behaviors.EndpointBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
If you use the transformation files, you need to add the transformation property, and specify what you would like to do, Insert, Remove, Replace...
I use to put the local connection in the master web.config and then transform it in the Release configuration, by Replacing the attributes of the defined connection string.
Check this article: Web.config Transformation Syntax for Web Project Deployment Using Visual Studio
If you want to use your way, just add xdt:Transform="Insert" in the <add> node.
You can test your transformation using this web tester: Web.config Transformation Tester

Not able to send long string to WCF ,the remote server returned an unexpected response: (400) Bad Request

I am sending a very long string to service which at present is 318771 characters long.
I am getting an error The remote server returned an unexpected response: (400) Bad Request.
** CLIENT APP.CONFIG **
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_INBFC" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="20000000" maxBufferPoolSize="20000000" maxReceivedMessageSize="20000000"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="200000000"
maxArrayLength="200000000" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
** SERVICE APP.CONFIG **
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
<pages>
<namespaces>
<add namespace="System.Runtime.Serialization"/>
<add namespace="System.ServiceModel"/>
<add namespace="System.ServiceModel.Web"/>
</namespaces>
</pages>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
A case of malformed request from W3
Check if the service is able to recieve the request using SOAP UI, also check if the datatypes between server and client calls match.
I suspect that your issue involves the WCF binding configuration. In particular, the maxReceivedMessageSize value and/or the readerQuota settings. If you provide your client/server bindings (or the entire relevant sections of the configuration file, we could provide better guidance.)
Note: you may want to enable WCF tracing, if you have not already, to help determine the cause of the issue. Reference: http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

WCF - Remote server returned an error: 404

I am new to WCF and i am trying to host my first WCF application in a different machine. It works in my local machine from IIS. I hosted in the remote machine and i am able to view the WSDL. I have enabled anonymous authentication as well.
Problem is when i try to test my .svc with SOAP UI, it errors out saying
Error loading [http://domain.com/folder/wcfservice.svc?xsd=xsd0]: java.io.IOException: Attempted read from closed stream
So, i tried using svcutil command and it errors out with
Attempting to download metadata from 'http: //domain.com/folder/wcfService.svc?wsdl' using WS-Metadata Exchange or
DISCO. Error: Cannot obtain Metadata from http:
//domain.com/folder/wcfService.svc?wsdl WS-Metadata Exchange Error
URI: http: //domain.com/folder/wcfService.svc?wsdl Metadata contains a
reference that cannot be resolved: 'http:
//domain.com/folder/wcfService.svc?wsdl'.
There was no endpoint listening at http: //domain.com/folder/wcfService.svc?wsdl that could accept the
message. This is often caused by an incorrect address or SOAP action.
See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.HTTP GET Error
URI: http ://domain.com/folder/wcfService.svc?wsdl The document was understood, but it could not be processed. The WSDL document
contains links that could not be resolved. There was an error
downloading 'http ://domain.com/folder/wcfService.svc?xsd=xsd0'. The
underlying connection was closed: An unexpected error occurred on a
receive. Unable to read data from the transport connection: An
existing connection was forcibly closed by the remote host.
Here is my webconfig for your reference.
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="defaultBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
<serviceTimeouts transactionTimeout="10.00:00:00"/>
</behavior>
<behavior name="metadataSupport">
<serviceDebug includeExceptionDetailInFaults="False" />
<!--Enable WSDL Data Binding-->
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="wcfservice.SSO"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="wcfservice.wcfservice_Service" behaviorConfiguration="defaultBehaviors">
<host>
<baseAddresses>
<add baseAddress="http://domain.com/folder/wcfService.svc" />
</baseAddresses>
</host>
<endpoint address="xml"
binding="basicHttpBinding"
bindingConfiguration="wcfservice.SSO"
contract="wcfservice.Iwcfservice" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange">
</endpoint>
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
I think its worthwhile to mention that this WCF application was created in VS2012 and i just changed the targetframework to 4.0 in web.config after deployment.
Can somebody please help what is causing the error? Am i missing something in web.config?

Passing bmp file/Image as parameter in wcf service

I am trying to pass a BMP file as a parameter in a WCF Service. I'mm getting an error when the size of the BMP file is 350kb. When I pass a small file of 10-20 kb it runs perfectly.
The remote server returned an unexpected response: (413) Request Entity Too Large.
[OperationContract]
byte[] ConvertData(Bitmap bmp);
I have tried to solve the issue with changing IIS setting following this link: http://blogs.msdn.com/b/jiruss/archive/2007/04/13/http-413-request-entity-too-large-can-t-upload-large-files-using-iis6.aspx but no luck
My Server web.config is
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime executionTimeout="240000"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" hostNameComparisonMode="StrongWildcard" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="ReDrawImgService.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="" contract="ReDrawImgService.IService1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
My Client Web.Config is
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime executionTimeout="240000"/>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" hostNameComparisonMode="StrongWildcard" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="ReDrawImgService.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="" contract="ReDrawImgService.IService1"/>
</service>
</services>
<client>
<endpoint address="http://localhost:8012/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceClient.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
On your readerQuotas, try to set maxStringContentLength to 2147483647 as well, and make sure this config is set on both ends.
So both the server and the client need to have these values. It's no help if the client is allowed to send a large file, if the server will reject it anyway.
What is the value of maxArrayLength on the wcf server application

Resources