I have a certain issue which I'm facing. My contract implementation (besides other stuff) has something like this:
try
{
response = _socketRequest.SendRequest(request, emmiterHeader);
trCode = response.TraceCodeInt;
if (trCode == 0)
statusRequest = (int) TSRequestAttemptStatus.active;
}
catch (TimeoutException tex)
{
throw;
}
catch (Exception)
{
statusRequest = (int) TSRequestAttemptStatus.notActive;
throw;
}
finally
{
tsra = CreateTireaSincoRequestAttemptRow(DateTime.Now, statusRequest, emmiterHeader.HeaderId,
string.Empty,
string.Empty,
string.Empty,
previousContractNumber,
taxIdentificationCode,
policyHolderName,
policyHolderFirstName,
policyHolderLastName,
registrationNumberType.Substring(0, 1),
registrationNumber,
((byte) ControlCodes.F),
DateTime.Now.AddDays(emmiterHeader.ValidityPeriod));
}
Then in
_socketRequest.SendRequest(request, emmiterHeader);
Among other stuff is something like below:
using (var client = new TcpClient(header.SocketServerAddress,
header.SocketServerPort == null ? 1 : (int)header.SocketServerPort))
{
Socket socket = client.Client;
// send data with timeout 10s
//socket.Send(arr);
Send(socket, arr, 0, arr.Length, 1000);
if (header.DebugMode)
_logger.LogInfo(InterfaceName, string.Format("Data Socket Send: {0} ", tempArr));
// receive data with timeout 10s
//Receive(client, arr);
len = Receive(socket, arrResponse, 0, arrResponse.Length, 5000, header.DebugMode, _logger);
if (socket.Connected)
socket.Close();
if (client.Connected)
client.Close();
}
The part under the using key word is never called because built in WCF Client is "hanging" on the TCPClient part, whcich in conclusion raises a SocketExeption error. I have set the timeouts in the web config to lets say 5 seconds. What I like to achieve is to throw not socket exception but the timeout exception. It looks like the SocketException is thrown but I can't make my wcf service throw a timeout exception. Is it possible to do that? I hope my questin is understandable what I want to do. If not I will try to explain as clearly as I can.
TcpClient does not know or care what you talk to. It has no notion of WCF, web services or the TimeoutException
that you want.
All it does it maintain a TCP connection.
Catch the SocketException
and analyze the error code stored in it. There is a code for timeouts. Throw TimeoutException
yourself.
And get rid of this superstitious dispose stuff:
if (socket.Connected)
socket.Close();
if (client.Connected)
client.Close();
Once is enough.