WCF Service - Message / Object size related - c#

I have wcf service (S1) as client and another Wcf service(S2) as server. S1 consume S2 over net.tcp. S2 has one operation, which return a complex object with most of it's contents are string. The average size of this object is between 7-8 MB.
Recently I had to add more string contents to the actual object graph. This cause S2 to return following error
The socket connection was aborted. This could be caused by an error
processing your message or a receive timeout being exceeded by the
remote host, or an underlying network resource issue. Local socket
timeout was '00:05:00'. System.Net.Sockets.SocketException
(0x80004005): An existing connection was forcibly closed by the remote
host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32
offset, Int32 size, SocketFlags socketFlags) at
System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer,
Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
However, everything works as expected when I remove some of the string contents from the object graph.
After doing bit of a homework,I was managed to land on a conclusion that, it has something to do with the size of the returned object. This made me to revise my client and service configuration/quota's.
However, I have no luck even after modifying the client and service especially size related attributes.
Can someone guide me on this? Please ask me for more information if needed.
Thanks in advance.
Client (S1)
<netTcpBinding>
<binding name="NetTcp" closeTimeout="00:05:00"
openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="2147483647"
maxBufferSize="2147483647" maxConnections="10" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
<client>
<endpoint address="net.tcp://xxxxxxxx/xxxxxxService"
binding="netTcpBinding" bindingConfiguration="NetTcp" behaviorConfiguration="RBehavior"
contract="IService" name="NetTcp" />
</client>
<services/>
<behaviors>
<serviceBehaviors>
<behavior name="NSBehavior">
<etwTracking profileName="EndToEndMonitoringProfile"/>
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RBehavior">
**<dataContractSerializer maxItemsInObjectGraph="2147483647" />**
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
Server (S2)
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcp" closeTimeout="00:03:00"
openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="20000000"
maxBufferSize="20000000" maxConnections="10" maxReceivedMessageSize="20000000">
<readerQuotas maxDepth="32" maxStringContentLength="20000000"
maxArrayLength="20000000" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="Engine.Rules"
behaviorConfiguration="REServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://xxxxx:8005/Service"/>
</baseAddresses>
</host>
<endpoint address=""
binding="netTcpBinding" bindingConfiguration="netTcp"
contract="Ixxxx" />
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="REServiceBehavior">
<serviceMetadata httpGetEnabled="false"/>
**<dataContractSerializer maxItemsInObjectGraph="2147483647"/>**
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentSessions="1000"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Update: As per #ErikFunkenbusch's sensible support, I am attaching service trace information.
<ExceptionString>System.ServiceModel.CommunicationException: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:05:00'. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
--- End of inner exception stack trace ---</ExceptionString>
<InnerException>
<ExceptionType>System.Net.Sockets.SocketException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>An existing connection was forcibly closed by the remote host</Message>
<StackTrace>
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
</StackTrace>
<ExceptionString>System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)</ExceptionString>
<NativeErrorCode>2746</NativeErrorCode>
</InnerException>
</Exception>

Have you tried increasing the send and receive timeout values? You're sending a lot of data over the wire (7-8MB) and 5 seconds may not be enough time to complete that transaction. Try setting them to something like 30 seconds and then working from there.
E:
<binding name="netTcp" closeTimeout="00:30:00"
openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00">
That is what I'm referring to, specifically.

Related

Is my app.config working? Truncated data

This is my first WCF service project writing both client and server side. I'm from a Windows Forms background.
I think I've been having problems with packet/receive buffer size but I don't have a clue how to test whether my app.config is working correctly or what I've done wrong.
The client project's app.config is set as follows and it's testproj.exe.config reflects the contents correctly
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IPulseWebService"
closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="blah"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IPulseWebService"
contract="ServiceReference1.IPulseWebService" name="WSHttpBinding_IPulseWebService">
<identity>
<dns value="localhst" />
</identity>
</endpoint>
</client>
</system.serviceModel>
My web service's webservice.dll.config contains the following service.serviceModel section
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IPulseWebService"
closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true">
<readerQuotas
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxDepth="2147483647"
maxNameTableCharCount="2147483647"
maxStringContentLength="2147483647" />
</binding>
</wsHttpBinding>
<basicHttpBinding>
<binding name="BasicHttpBinding_IPulseWebService" 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="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="SmartcentreWcfLib.PulseWebService">
<host>
<baseAddresses>
<add baseAddress="http://l0calhost:8732/Design_Time_Addresses/SmartcentreWcfLib/PulseWebService/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IPulseWebService"
contract="SmartcentreWcfLib.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="l0calhost" />
</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>
<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>
</system.serviceModel>
In my server side wcf dll, I have the following function just as a test to return some rows of data. Each row is a set of key/value pairs stored in an Dictionary. Each object should be a simple byte[] or variable type.
List<IDictionary<string, object>> IAuditV1.ReadAuditForStaffMember(long staffId, DateTime startDate,
DateTime endDate, Int32 recordCount)
{
List<TableAdminAudit> tempResult = DatabaseInterface.Instance.AuditQueries.ReadAdminAudit(staffId, startDate,
endDate, recordCount);
return tempResult.Select(record => record.PrepareForWebInterface()).Cast<IDictionary<string, object>>().ToList();
}// function
In my client application I am running the following loop, which simply increments the number of received records.
Int32 totalRecords = 0;
while (true)
{
results = client.ReadAuditForStaffMember(1, DateTime.MinValue, DateTime.Now, totalRecords);
totalRecords += 10; //results.Length;
if (totalRecords == 0) break;
}
On the 3rd loop when the returned buffer reaches 30 records in size I receive the following error message.
I looked up the error and forums suggested it relates to the connection closing before the whole data had been received so change the various buffer sizes etc. As you can see from my config files, I've upped all the values I'm aware of to 2147483647 but it's not worked.
So I don't know where to go from here. Can anybody help please?
System.ServiceModel.CommunicationException occurred
HResult=-2146233087
Message=An error occurred while receiving the HTTP response to blah2/Design_Time_Addresses/SmartcentreWcfLib/PulseWebService/. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
Source=mscorlib
StackTrace:
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at WcfTester.ServiceReference1.IPulseWebService.ReadAuditForStaffMember(Int64 staffId, DateTime startDate, DateTime endDate, Int32 startIndex)
at WcfTester.ServiceReference1.PulseWebServiceClient.ReadAuditForStaffMember(Int64 staffId, DateTime startDate, DateTime endDate, Int32 startIndex) in W:\Projects\pulse.smartcentre.root\pulse.smartcentre\WcfTester\Service References\ServiceReference1\Reference.cs:line 481
at WcfTester.Program.Main(String[] args) in W:\Projects\pulse.smartcentre.root\pulse.smartcentre\WcfTester\Program.cs:line 40
InnerException: System.Net.WebException
HResult=-2146233079
Message=The underlying connection was closed: An unexpected error occurred on a receive.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
InnerException: System.IO.IOException
HResult=-2146232800
Message=Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
Source=System
StackTrace:
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
InnerException: System.Net.Sockets.SocketException
HResult=-2147467259
Message=An existing connection was forcibly closed by the remote host
Source=System
ErrorCode=10054
NativeErrorCode=10054
StackTrace:
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
InnerException:**
The file something.dll.config is never used. You need to copy those settings into the web.config or servicehost.exe.config file.
This is how .NET has worked since Day 1, and is not specific to WCF. Consider that a given DLL can be used by multiple "executables", and each of these may have different settings for the DLL.

WCF Client and Service Endpoints Using the Same Binding

I feel like this has probably been answered and maybe I am not searching with the right terms.
I have a WCF service that acting like a proxy. It is a middle man for two services that normally can talk to each other, but now go through this new service.
Old:
A -> B
New:
A -> MM -> B
I need to expose the same contract as a service endpoint (for "A" to talk to "MM") and as a client endpoint (for "MM" to talk to "B").
Can these two share a binding configuration and just give the endpoints different names? Or is there a better way to handle this scenario?
(inside of <system.serviceModel> tag)
<client>
<endpoint address="http://<remoteaddress>/" binding="basicHttpBinding"
bindingConfiguration="MyBinding" contract="IService" name="ToB" />
</client>
<services>
<service behaviorConfiguration="Behavior" name="Service">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="MyBinding" name="FromA" contract="IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/MyService" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="MyBinding" closeTimeout="00:15:00" openTimeout="00:15:00"
receiveTimeout="00:15:00" sendTimeout="00:15:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2048000000" maxBufferPoolSize="2048000000"
maxReceivedMessageSize="2048000000" messageEncoding="Text"
textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="4065536" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
Edit:
The error message with this setup is:
com.vsp.cal.webservice.external.SystemFault
Stack:
Server stack trace:
at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
A change in name is not enough to uniquely define an endpoint. Instead goes back to the old "ABC" - address, binding, contract. At least one of those must be different to uniquely define a new endpoint.
http://msdn.microsoft.com/en-us/library/ms733107(v=vs.100).aspx
Does this help?
Thank you to everyone who looked this over and tried to help!
In the end it was not the configuration, but a stupid coding error. The cryptic error message is the response from the other side (a Java web service). It wasn't happy because I was sending a null object due to a failed cast.
So the answer is that this configuration DOES WORK when you don't code like an idiot.

c# WCF Service return collection

I've created wcf service and everything works correctly but when i try to return Type[] (i got it from Assembly.GetTypes()) i have following exception:
System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly.
---> System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at ConsoleApplication1.ServiceReference1.IWcfAssembly.GetAssemblyTypes(String a)
at ConsoleApplication1.ServiceReference1.WcfAssemblyClient.GetAssemblyTypes(String a) in D:\Projekty\ConsoleApplication1\ConsoleApplication1\Service References\ServiceReference1\Reference.cs:line 50
at ConsoleApplication1.Program.Main(String[] args) in D:\Projekty\ConsoleApplication1\ConsoleApplication1\Program.cs:line 17
I thought it might be maxItemsInObjectGraph property or maxArrayLenght (and similar) but it didn't help. Still got the same exception. Maybe I did something wrong with the property declaration?
this is my client config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IWcfAssembly" 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="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:57040/WcfAssembly.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IWcfAssembly" contract="ServiceReference1.IWcfAssembly"
name="BasicHttpBinding_IWcfAssembly" />
</client>
</system.serviceModel>
</configuration>
and this is my service config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IWcfAssembly" receiveTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647"
maxBufferPoolSize="52428899">
<readerQuotas maxDepth="128"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="4096"
maxNameTableCharCount="2147483647" />
<security mode="None"/>
</binding>
</basicHttpBinding>
</bindings>
<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>
</configuration>
I have also added [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)] as a class attribute. I had it in my service config before but this is one of tries.
Do you have any ideas guys? Why the array returned from Assembly.GetTypes() causes such error? (big array of int[] works OK)
The problem is that the System.RunTimeType is not serialize (which is in the internal implementation of Type)
using this mock:
void Main()
{
var dto = new MyDto();
dto.Tada = new Type[] { this.GetType() };
DataContractSerializer ser =
new DataContractSerializer(typeof(MyDto));
var ms = new MemoryStream();
ser.WriteObject(ms, dto);
ms.Dump();
dto.Dump();
}
public class MyDto
{
public Type[] Tada { get; set; }
}
Throws:
SerializationException: Type 'System.RuntimeType' with data contract
name 'RuntimeType:http://schemas.datacontract.org/2004/07/System' is
not expected. Consider using a DataContractResolver or add any types
not known statically to the list of known types - for example, by
using the KnownTypeAttribute attribute or by adding them to the list
of known types passed to DataContractSerializer.
Which makes sense because RunTimeType is internal (Type itself is abstract). You have to come up with your own serialized type that can convey the info you want.

An existing connection was forcibly closed by the remote host

My service's app.config
<system.serviceModel>
<client>
<endpoint name="DataLocal" address="net.tcp://SomeAddress" binding="netTcpBinding" contract="ISomeContract" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="SecureBehaviorName">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</client>
<services>
<service name="SomeService">
<host>
<baseAddresses>
<add baseAddress="http://SomeService" />
<add baseAddress="net.tcp://SomeService" />
</baseAddresses>
</host>
<endpoint name="SomeService_Normal" address="Secure" binding="netTcpBinding" contract="ISomeService" bindingConfiguration="TcpNormal"/>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="TcpNormal" transferMode="Buffered" receiveTimeout="24.20:31:23.6470000">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None" />
</binding>
<binding name="TcpCustomSecurity" transferMode="Buffered" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="24.20:31:23.6470000" sendTimeout="00:01:00" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="SecureBehaviorName">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="None"/>
</serviceCertificate>
</clientCredentials>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
Following are the stack statements for the error i am getting..
> System.dll!System.Net.Sockets.Socket.Send(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags) + 0x5a bytes
System.ServiceModel.dll!System.ServiceModel.Channels.SocketConnection.Write(byte[] buffer, int offset, int size, bool immediate, System.TimeSpan timeout) + 0xa7 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.SocketConnection.Write(byte[] buffer, int offset, int size, bool immediate, System.TimeSpan timeout, System.ServiceModel.Channels.BufferManager bufferManager) + 0x34 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.BufferedConnection.WriteNow(byte[] buffer, int offset, int size, System.TimeSpan timeout, System.ServiceModel.Channels.BufferManager bufferManager) + 0x90 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.BufferedConnection.Write(byte[] buffer, int offset, int size, bool immediate, System.TimeSpan timeout, System.ServiceModel.Channels.BufferManager bufferManager) + 0x47 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.FramingDuplexSessionChannel.OnSend(System.ServiceModel.Channels.Message message, System.TimeSpan timeout) + 0x115 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.OutputChannel.Send(System.ServiceModel.Channels.Message message, System.TimeSpan timeout) + 0x81 bytes
System.ServiceModel.dll!System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(System.ServiceModel.Channels.Message message, System.TimeSpan timeout) + 0x154 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.Call(string action, bool oneway, System.ServiceModel.Dispatcher.ProxyOperationRuntime operation, object[] ins, object[] outs, System.TimeSpan timeout) + 0x206 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(System.Runtime.Remoting.Messaging.IMethodCallMessage methodCall, System.ServiceModel.Dispatcher.ProxyOperationRuntime operation) + 0x59 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.Invoke(System.Runtime.Remoting.Messaging.IMessage message) + 0x65 bytes
mscorlib.dll!System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(ref System.Runtime.Remoting.Proxies.MessageData msgData, int type) + 0xee bytes
// First the service call in the above stack...
Service Behavior is this:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
The error occured after few hours of constant testing...unable to reproduce it while debugging..
EDIT:
I've got the tracing results thanks to Dejan...
on the first occurance of error i get this message: Maximum number of pending connections has been reached.
on the second occurance i get this message : The system hit the limit set for throttle 'MaxConcurrentConnections'.Limit for this throttle was set to 200.Throttle value can be changed by modifying attribute 'maxConcurrentSessions' in service throttle element.
So for the second message i did this:
and assigned this service behavior to the service. do i need to set the maxConnections on the binding for the first message.Moreover, which is the root cause for the problem , first one or second one or both. Because in the subsequent messages i always get the second message.
Please guide to update my app.config to avoid such scenario.
UPDATE
I've got the tracing results...
on the first occurance i get this message: Maximum number of pending connections has been reached.
on the second occurance i get this message : The system hit the limit set for throttle 'MaxConcurrentConnections'.Limit for this throttle was set to 200.Throttle value can be changed by modifying attribute 'maxConcurrentSessions' in service throttle element.
So for the second message i did this:
<serviceBehaviors>
<behavior name="tcpNormalBehavior">
<serviceThrottling maxConcurrentSessions="800" maxConcurrentInstances="800" maxConcurrentCalls="200" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
and assigned this service behavior to the service.
do i need to set the maxConnections on the binding for the first message.Moreover, which is the root cause for the problem , first one or second one or both. Because in the subsequent messages i always get the second message.
Please guide.
There is a possibilty that you have an unhandled exception such as stack overflow somewhere in your WCF service. If I get it correctly, the service works but stops working after a few hours.
The first step to debugging a WCF service is WCF Tracing. Enable it, try to test your service and after it stops working again, there should be a trace entry in the .svclog file.
EDIT
Regarding your updated question: You get that exception because you are not closing the connection from the client side after you're done with the call. Try refactoring your code so calls to WCF look like this:
WCFProxy clientProxy = null;
try
{
clientProxy = new WCFProxy();
clientProxy.SomeCall();
clientProxy.Close();
}
catch (Exception)
{
if (clientProxy != null)
{
clientProxy.Abort();
}
throw;
}
and then see if it will cause problems.

Large Binary (byte[]) File transfer through WCF

I am trying to build a WCF service that allows me to send large binary files from clients to the service.
However I am only able to successfully transfer files up to 3-4MB. (I fail when I try to transfer 4.91MB and, off course, anything beyond)
The Error I get if I try to send the 4.91MB file is:
Exception Message: An error occurred while receiving the HTTP response to http://localhost:56198/Service.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
Inner Exception Message: The underlying connection was closed: An unexpected error occurred on a receive.
Inner Exception Message: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
Inner Exception Message: An existing connection was forcibly closed by the remote host
This error occurs at client side as soon as the byte[] file is sent as a method parameter to the exposed service method.
I have a breakpoint at the service method's first line, in case of successful file transfers (below 3MB) that break point is hit and the file gets transferred. However in this case as soon as the method is called, the error comes. The breakpoint in the service is not hit in case of this error.
I am going to paste my sections of my Service Web.config and Asp Page (Client) Web.config. If you also require the code that send the file and accepts the file, let me know, I'll send that as well.
Service Web.Config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpEndpointBinding" closeTimeout="01:01:00"
openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646"
messageEncoding="Mtom" textEncoding="utf-8" transferMode="StreamedRequest"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483646" maxStringContentLength="2147483646" maxArrayLength="2147483646"
maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="DragDrop.Service.ServiceBehavior" name="DragDrop.Service.Service">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpEndpointBinding" contract="DragDrop.Service.IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DragDrop.Service.ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Client (Asp.net page) Web.Config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483646" maxBufferPoolSize="2147483646" maxReceivedMessageSize="2147483646"
messageEncoding="Mtom" textEncoding="utf-8" transferMode="StreamedResponse"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483646" maxStringContentLength="2147483646" maxArrayLength="2147483646"
maxBytesPerRead="2147483646" maxNameTableCharCount="2147483646" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="">
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="debuggingBehaviour">
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost:56198/Service.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference.IService"
name="BasicHttpBinding_IService" behaviorConfiguration="debuggingBehaviour" />
</client>
</system.serviceModel>
(While I agree that streaming transfer would be preferrable, the below should make it work without any other changes)
You also need to increase the maximum message length in the Web.config:
<configuration>
<system.web>
<httpRuntime maxMessageLength="409600"
executionTimeoutInSeconds="300"/>
</system.web>
</configuration>
This will set the maximum message length to 400 MB (parameter is in kB). Check this MSDN page for more information.
As pointed out, try using Streaming Transfer, here's some example code showing both sending and receiving (possibly) large amounts of data using streamed transfer.
Use a binding like this, notice the MaxReceivedMessageSize and TranferMode settings.
<binding name="Streaming_Binding" maxReceivedMessageSize="67108864"
messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed">
</binding>
Add some service code:
[OperationContract]
public Stream GetLargeFile()
{
return new FileStream(path, FileMode.Open, FileAccess.Read);
}
[OperationContract]
public void SendLargeFile(Stream stream)
{
// Handle stream here - e.g. save to disk
ProcessTheStream(stream);
// Close the stream when done processing it
stream.Close();
}
And some client code:
public Stream GetLargeFile()
{
var client = /* create proxy here */;
try
{
var response = client.GetLargeFile();
// All communication is now handled by the stream,
// thus we can close the proxy at this point
client.Close();
return response;
}
catch (Exception)
{
client.Abort();
throw;
}
}
public void SendLargeFile(string path)
{
var client = /* create proxy here */;
client.SendLargeFile(new FileStream(path, FileMode.Open, FileAccess.Read));
}
Also, make sure you are not getting a timeout, a large file might take a while to transfer (the default receiveTimeout is 10 minutes though).
You can download Microsoft WCF/WF sample code here (top C# link is broken at the time of writing but other samples code seems ok).
Have you had a look at using Streaming Transfer?
Windows Communication Foundation (WCF)
can send messages using either
buffered or streamed transfers. In the
default buffered-transfer mode, a
message must be completely delivered
before a receiver can read it. In
streaming transfer mode, the receiver
can begin to process the message
before it is completely delivered. The
streaming mode is useful when the
information that is passed is lengthy
and can be processed serially.
Streaming mode is also useful when the
message is too large to be entirely
buffered.
http://msdn.microsoft.com/en-us/library/ms789010.aspx
I'll echo what others have said and say that using a Streaming Transfer is the way to go when using Windows Communication Foundation. Below is an excellent guide that explains all of the steps in order to stream files over WCF. It's quite comprehensive and very informative.
Here it is: Guide on Streaming Files over WCF.

Categories

Resources