WCF, out of memory exception while serializing huge amount of data - wcf

Basically, I am fetching data from SQL Server. If I select all data (which is about 80000 rows), then I am getting the following error (Receiving 100 rows is working fine)
Exception of type 'System.OutOfMemoryException' was thrown. <br/>
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br/>
Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
Stack Trace:
[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
System.Text.StringBuilder.ToString() +29
System.IO.StringWriter.ToString() +14
System.Net.WebUtility.HtmlEncode(String value) +110
System.Web.Util.HttpEncoder.HtmlEncode(String value) +54
System.Web.UI.HtmlControls.HtmlContainerControl.set_InnerText(String value) +24
PerformanceCompare._Default.Page_Load(Object sender, EventArgs e) in C:\Users\KK33562\Documents\Visual Studio 2010\Projects\TestWebService\PerformanceCompare\Default.aspx.cs:34
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
Web.config (Client Side)
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="endpointbehaviour">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:50:00"
openTimeout="00:50:00" receiveTimeout="00:50:00" sendTimeout="00:50:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53268/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="WCFService.IService1"
name="BasicHttpBinding_IService1" behaviorConfiguration="endpointbehaviour" />
</client>
</system.serviceModel>
Web.config (Server-Side)
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TestWCF.Service1Behavior">
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TestWCF.Service1" behaviorConfiguration="TestWCF.Service1Behavior">
<endpoint address="" binding="basicHttpBinding" contract="TestWCF.IService1" bindingConfiguration="BasicHttpBinding_IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:50:00" closeTimeout="00:50:00" sendTimeout="00:50:00" receiveTimeout="00:10:00">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>

If you're returning 80K rows from your service then I think you'd better get rid of the service all together. Just get the client to access the DB directly and then they can pull all the rows they need from the DB.
EDIT
Introducing a service boundary where it's not necessary is a very common error, and one I see happening more and more. It would be cleaner in most cases to have the client call the database directly using NHibernate or even ADO and a stored proc or view.
If your client can't physically see the database on the network then you may have to use a service.

Related

WCF Calling Error: The underlying connection was closed: The connection was closed unexpectedly

I have a really annoying problem regarding with WCF Services. I have a wcf service which has basichttpbindng.
The service is published to Azure Web App and timeout is set to 20 minutes. After 180 seconds I get the following error:
The underlying connection was closed: The connection was closed
unexpectedly
I have a method like following:
public ProcessResult TestErrorMessage()
{
try
{
System.Threading.Thread.Sleep(320000);
return new ProcessResult()
{
};
}
catch (Exception ex)
{
throw ex;
}
}
[OperationContract]
ProcessResult TestErrorMessage();
You can find both Config below.
Server Config:
<bindings>
<basicHttpBinding>
<binding name="basicBindingConfiguration" closeTimeout="00:20:00" openTimeout="00:20:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<!--<security mode="None">-->
<security mode="Transport">
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="Tefal.WebService.Integration.DivaIntegration" behaviorConfiguration="customBehavior">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBindingConfiguration" contract="Tefal.WebService.Integration.IDivaIntegration" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="customBehavior">
<!--<serviceMetadata httpsGetEnabled="false" />-->
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
Cleint config:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDivaIntegration" closeTimeout="00:20:00"
openTimeout="00:20:00" receiveTimeout="00:20:00" sendTimeout="00:20:00"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
useDefaultWebProxy="true">
<security mode="Transport" />
</binding>
<binding name="BasicHttpBinding_IDivaIntegration1" receiveTimeout="00:20:00"
sendTimeout="00:20:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="ClientBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>

Contract requires Session, but Binding 'WSHttpBinding' doesn't support it; 404 with TransportWithMessageCredential

I am trying to enable my WCF service to communicate via HTTPS.
Server config:
<system.serviceModel>
<services>
<service name="PortalServiceLibrary.PortalService" behaviorConfiguration="PortalServiceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="PortalServiceLibrary.IPortalService">
<identity>
<dns value="vmserver"/>
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/PortalServiceLibrary/PortalService/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="PortalServiceBehavior">
<serviceMetadata httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="bindingAction" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
</binding>
</wsHttpBinding>
</bindings>
<protocolMapping>
<add scheme="http" binding="wsHttpBinding"/>
<add scheme="https" binding="wsHttpBinding"/>
</protocolMapping>
</system.serviceModel>
Client Config:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IPortalService" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="TransportWithMessageCredential">
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://vmserver/PortalService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IPortalService"
contract="PortalService.IPortalService" name="WSHttpBinding_IPortalService">
</endpoint>
</client>
</system.serviceModel>
When I use:
<security mode="Transport">
I receive the following error:
Contract requires Session, but Binding 'WSHttpBinding' doesn't support it or isn't configured properly to support it.
But when I change it to:
<security mode="TransportWithMessageCredential">
I receive the following exception:
[WebException: The remote server returned an error: (404) Not Found.]
System.Net.HttpWebRequest.GetResponse() +6592536
System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +55
[EndpointNotFoundException: There was no endpoint listening at https://vmserver/PortalService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10733331
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
Portal.PortalService.IPortalService.GetToken(TokenRequest request) +0
...
I'm really not sure what direction to go in here as I appear quite stuck. Any help will be greatly appreciated.
First of all,
The <wsHttpBinding> bindings specified in your Client config is different from what is specified in your service config.
Please copy the bindings from your client config and paste it in your service config.
<wsHttpBinding>
<binding name="WSHttpBinding_IPortalService" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="100000" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="TransportWithMessageCredential">
</security>
</binding>
</wsHttpBinding>
Also,
Update your service endpoints (which you have not posted here) to bindingConfiguration="WSHttpBinding_IPortalService" and also the endpoint address.
This should atleast resolve the not found issue.
Hope this helps

Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding

I have a service with a basichttp binding and I am trying to add a net.tcp binding to it. here is how the config looks like:
<services>
<service behaviorConfiguration="ServiceBehavior" name="Some.L.E.S.BusinessService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IBusinessService"
contract="Some.L.E.Services.IBusinessService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="/BusinessService.svc" binding="netTcpBinding" bindingConfiguration="netTcpBinding_IBusinessService" contract="Some.L.E.Services.IBusinessService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:63487/"/>
<add baseAddress="net.tcp://localhost/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- 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>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<!-- Start of HostServices client configurations -->
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IBusinessService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None" realm="" />
</security>
</binding>
</basicHttpBinding>
<!-- RSF -->
<netTcpBinding>
<binding name="netTcpBinding_IBusinessService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
transferMode="Buffered">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
</netTcpBinding>
</bindings>
I start this service up in VS2010 and I am trying to connect to it from another VS2010 project. However I get the following error:
Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http]
Most solutions seem to recommend changes to the web.config in IIS virtual directory. But my Service is not hosted in IIS.
Any help will be much appreciated.
Thank you,
GM

Issue on max readers quotas in WCF web service

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:GetFileResult. The InnerException message was 'There was an error deserializing the object of type WindowsClient.CloudServiceProxy.GetFileResponse. 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. Line 1, position 41572.'. Please see InnerException for more details.
I am getting this issue the
sever web.config is
<system.serviceModel>
<services>
<service behaviorConfigura
tion="CloudServiceBehaviour" name="Web.CloudService">
<endpoint name="CloudServiceClientEndPoint" bindingConfiguration="CloudBindingConfig" address="http://localhost:53243/CloudService.svc" binding="wsHttpBinding" contract="Web.ICloudService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CloudServiceBehaviour">
<serviceMetadata httpGetEnabled="True" httpGetUrl=""/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="CloudBindingConfig" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="200" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
and the client web.config is ,
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="CloudServiceClientEndPoint" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:30:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53243/CloudService.svc" binding="wsHttpBinding"
contract="CloudServiceProxy.ICloudService" name="CloudServiceClientEndPoint" />
</client>
</system.serviceModel>
Since the error is while deserializing the GetFileResponse object, that tells you the problem is in the client-side stack.
Your client side binding is using the default configuration for wsHttpBinding because you have omitted to specify the bindingConfiguration name on the endpoint. Try adding bindingConfiguration="CloudServiceClientEndPoint" to the endpoint element and then your large values for the readerQuotas settings will be picked up.

WCF , The remote server returned an error: (400) Bad Request

I am getting this exception when trying to access a wcf web service.
[WebException: The remote server returned an error: (400) Bad Request.]
System.Net.HttpWebRequest.GetResponse() +5314029
System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +54
Server Binding Information
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="wcfSmartConnect" closeTimeout="10:01:00"
maxBufferSize="104857600" maxBufferPoolSize="104857600"
maxReceivedMessageSize="104857600" openTimeout="10:01:00"
receiveTimeout="10:10:00" sendTimeout="10:01:00"
messageEncoding="Mtom" transferMode="StreamedRequest">
<readerQuotas maxDepth="104857600" maxStringContentLength="104857600"
maxArrayLength="104857600" maxBytesPerRead="104857600"
maxNameTableCharCount="104857600" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfSmartConnect.Service1"
behaviorConfiguration="WcfSmartConnect.Service1Behavior">
<endpoint
address=""
binding="basicHttpBinding"
bindingConfiguration="wcfSmartConnect"
contract="WcfSmartConnect.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfSmartConnect.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Client Binding Information
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="wcfSmartConnect" closeTimeout="10:01:00"
maxBufferSize="104857600" maxBufferPoolSize="104857600"
maxReceivedMessageSize="104857600" openTimeout="10:01:00"
receiveTimeout="10:10:00" sendTimeout="10:01:00"
messageEncoding="Mtom" transferMode="StreamedRequest">
<readerQuotas maxDepth="104857600" maxStringContentLength="104857600"
maxArrayLength="104857600" maxBytesPerRead="104857600"
maxNameTableCharCount="104857600" />
</binding>
</basicHttpBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_IService11" closeTimeout="01:00:00"
openTimeout="01:00:00" receiveTimeout="01:00:00"
sendTimeout="01:00:00" bypassProxyOnLocal="false"
transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="1048576000" maxReceivedMessageSize="1048576000"
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true" allowCookies="false">
<readerQuotas maxDepth="104857600" maxStringContentLength="104857600"
maxArrayLength="104857600" maxBytesPerRead="1048576000"
maxNameTableCharCount="104857600" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Windows"
proxyCredentialType="None" realm="" />
<message clientCredentialType="Windows"
negotiateServiceCredential="true" algorithmSuite="Default"
establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint name="WSHttpBinding_IService11"
address="http://abc.com/API/serv.svc"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService11"
contract="SmartConnectRepublic.IService1" >
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint name="BasicHttpBinding_IService1"
address="http://localhost:4649/Service1.svc"
binding="basicHttpBinding"
bindingConfiguration="wcfSmartConnect"
contract="SmartConnect.IService1" />
</client>
</system.serviceModel>
Can you tell us a bit about your setup?
what binding do you use?
is this the only service method? If not: do the others work?
show us the configs on client and server! (everything inside <system.serviceModel> is of interest)
It would appear as if the client times out waiting for the server - this can be because the server takes too long to respond (if you do a lot of data loading); in that case, you need to increase your timeouts on the server and the client
Or you might be sending too large a data packet over the wire - in that case, you might also need to increase the settings for maxReceivedMessageSize et al.
#Pinu: are you trying to upload 5 MB? You have transferMode=StreamRequest, which means your request from the client to the server will be streamed.
If you're transferring 5 MB, it's a good idea to stream - which way do you transfer those bytes??