I am writing WCF client for service writtern in Java by one of the partner. I am getting an exception when I make first service call to thier service. But subsequent request did not throw any exception. I am using console application to test this. Why is that failing on first time not other times?
Here is code how I am calling the service multiple times
for (int i = 0; i < 3; i++)
{
ServiceClientTest();
}
Here is the Binding code
TransportBindingElement transportElement = null;
transportElement = new HttpsTransportBindingElement();
((HttpsTransportBindingElement)transportElement).AuthenticationScheme = AuthenticationSchemes.Basic;
var messegeElement = new TextMessageEncodingBindingElement
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None),
ReaderQuotas =
{
MaxArrayLength = 200000,
MaxBytesPerRead = 200000,
MaxDepth = 200000,
MaxNameTableCharCount = 200000,
MaxStringContentLength = 200000
}
};
var binding = new CustomBinding(messegeElement, transportElement);
return binding;
Here is the exception details of first request
Failed to score for CompanyXYZ ServiceCompany System.ServiceModel.CommunicationException: An error occurred while receiving the HTTP response to https://test.intelligentcusomer.ServiceCompany.com/XYZCompanyAdapter/1.0. 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. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.HttpWebRequest.MakeMemoryStream(Stream stream) --- End of inner exception stack trace --- 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 MyCompany.Services.Clients.ServiceCompany.XYZCompanyAdapter.process(processRequest request) at MyCompany.Services.Clients.ServiceCompany.XYZCompanyAdapterClient.MyCompany.Services.Clients.ServiceCompany.XYZCompanyAdapter.process(processRequest request) in C:\Projects\MyCompany\MyCompany.Distribution\CustomDeliveryProcessor\Clients\XYZCompanyServiceClient.cs:line 1122 at MyCompany.Services.Clients.ServiceCompany.XYZCompanyAdapterClient.process(process process1) in C:\Projects\MyCompany\MyCompany.Distribution\CustomDeliveryProcessor\Clients\XYZCompanyServiceClient.cs:line 1129 at CustomDeliveryProcessor.CusomerDelivery.CompanyTestTest() in C:\Projects\MyCompany\MyCompany.Distribution\CustomDeliveryProcessor\CusomerDelivery.cs:line 131
I fixed this error by setting KeepAlive property to false. What I found was happened, when first request sent to the service endpoint, got 401 response from server (it is obivous because service using the basic authontication and then client sent basic authorization header in next request) that closes the connection. Default behaviour of client endpoint is to keep the connection alive. Though connection closed, client still send next request by using the same connection that already closed by the server that causes the above said error in the question. But this error won't happen again for subsequent requests because client send request with authorization header in the first request itself. So there is no chance of 401 response. What I did to fix this issue that set Keep alive property to false. So that, after 401 response, client send another request with new connection instead of using the old connection. Here is code
Binding binding = null;
var transportElement = new HttpsTransportBindingElement
{
AuthenticationScheme = AuthenticationSchemes.Basic,
KeepAliveEnabled = false,
};
var messegeElement = new TextMessageEncodingBindingElement
{
MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, AddressingVersion.None),
ReaderQuotas =
{
MaxArrayLength = 200000,
MaxBytesPerRead = 200000,
MaxDepth = 200000,
MaxNameTableCharCount = 200000,
MaxStringContentLength = 200000
}
};
binding = new CustomBinding(messegeElement, transportElement);
return binding;