I have a Console app that uses a parallel.foreach producing a number of working threads that performs some tasks such as
reading OS data through SNMP from a number of servers in our intranet and
writes these values to a SQL server DB.
When I ran the code within Visual Studio 2010 in either debug- or release mode the program executes without exceptions. When I deploy the program and run it outside VS I get an exception (.NET Runtime Exceptiopn).
Stack Trace:
Application: ServerMonitorSNMP.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.AggregateException Stack: at System.Threading.Tasks.Parallel.ForWorker[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Int32, Int32, System.Threading.Tasks.ParallelOptions, System.Action`1,
...
at ServerMonitoringSNMP.Program.Main(System.String[])
The AggregateException details are:
System.UnhandledExceptionEventArgs
System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
...
(Inner Exception #0) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
...
(Inner Exception #1) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
...
(Inner Exception #2) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
...
System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at ServerMonitoringSNMP.Program.GetStorageValue(Dictionary`2 StorageQuery, Int32 diskOidIndex) in \Program.cs:line 896
This is an unhandled aggregate exception. You will need to catch this type of error, loop through its exceptions and log each one to figure out what is going on.
You are probably not breaking on these types of exceptions in your debug routine.
Turn on Break on All Exceptions (Debug, Exceptions) or (CTRL + ALT + E) and rerun the program.
This will show you the original exception when it was thrown in the first place.
Catch code:
catch (AggregateException e)
{
foreach (var ex in e.InnerExceptions)
{
//log here
}
}
The attached exception is too abstract. The best approach would be handle the TPL exception and log. Things shall be much clearer when you do so. Then you can updated the post with proper exception.
Eg:
try
{
YourSNMPTrapSenderMethod();
}
catch (AggregateException ae)
{
// This is where you can choose which exceptions to handle.
foreach (var ex in ae.InnerExceptions)
{
// log your exception. may be in temp directory
}
}
Or
try
{
YourSNMPTrapSenderMethod();
}
catch (AggregateException ex)
{
ex.Handle((x) =>
{
Log.Write(x);
return true;
});
}
static void YourSNMPTrapSenderMethod()
{
var exceptions = new ConcurrentQueue<Exception>();
Parallel.ForEach(data, d =>
{
try
{
//do your operations
}
catch (Exception e) { exceptions.Enqueue(e); }
});
if (exceptions.Count > 0) throw new AggregateException(exceptions);
}
Related
I occasionally see timeout errors in event hub client when trying to send messages to Azure event hub. It looks like resource limit is reached for client but I'm not sure...
Here the code:
MessagingFactory messagingFactory = null;
EventHubClient hubClient = null;
try
{
messagingFactory = MessagingFactory.CreateFromConnectionString(this.connectionString);
hubClient = messagingFactory.CreateEventHubClient(this.configurationManager.EventHubName);
var batchIterator = new EventBatchIterator<T>(events);
foreach (var batch in batchIterator)
{
await hubClient.SendBatchAsync(batch);
}
}
catch (Exception e)
{
this.logger.Error("An error occurred during sent messages to event hub", e);
}
finally
{
if (hubClient != null)
{
await hubClient.CloseAsync();
}
if (messagingFactory != null)
{
await messagingFactory.CloseAsync();
}
}
Exception is:
An error occurred during communication with 'N/A'. Check the
connection information, then retry.
Microsoft.ServiceBus.Messaging.MessagingCommunicationException
A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond
System.Net.Sockets.SocketException
According to you mentioned execption indicates that a user-initiated operation is taking longer than the operation timeout. My workaround is that you could increase OperationTimeout or retry count.
Demo code for increasing timeout
var builder = new ServiceBusConnectionStringBuilder("connection string")
{
TransportType = TransportType.Amqp,
OperationTimeout = TimeSpan.FromSeconds(90)
};
messagingFactory = MessagingFactory.CreateFromConnectionString(builder.ToString());
More info about Timeout exception you refer to this document.
Common causes
There are two common causes for this error: incorrect configuration, or a transient service error.
Incorrect configuration The operation timeout might be too small for the operational condition. The default value for the operation timeout in the client SDK is 60 seconds. Check to see if your code has the value set to something too small. Note that the condition of the network and CPU usage can affect the time it takes for a particular operation to complete, so the operation timeout should not be set to a very small value.
Transient service error Sometimes the Event Hubs service can experience delays in processing requests; for example, during periods of high traffic. In such cases, you can retry your operation after a delay, until the operation is successful. If the same operation still fails after multiple attempts, please visit the Azure service status site to see if there are any known service outages.
I am using code snippet to send message into the service bus topic.
try
{
// sb is instance of ServiceBusConfig.GetServiceBusForChannel
await sb.SendAsync(message);
}
catch (Exception ex)
{
this.logger.LogError(
"chanel",
"An error occurred while sending a notification: " + ex.Message,
ex);
throw;
}
and implementation is
public async Task SendAsync(BrokeredMessage message)
{
if (this.topicClient == null)
{
this.topicClient = TopicClient.CreateFromConnectionString(this.primaryConnectionString, this.topicPath);
this.topicClient.RetryPolicy = this.retryPolicy;
}
await this.topicClient.SendAsync(message);
}
Error:-
"ErrorCode,12005,Message,""An error occurred while sending a
notification: The operation did not complete within the allotted
timeout of 00:01:00. The time allotted to this operation may have
been a portion of a longer timeout. For more information on exception
types and proper exception handling,
Exception,""Microsoft.ServiceBus.Messaging.MessagingException: The
operation did not complete within the allotted timeout of 00:01:00.
The time allotted to this operation may have been a portion of a
longer timeout.
For more information on exception types and proper exception handling
This happens occasionally. Azure can change and shift their underlying hardware willy nilly. These sort of errors pop up every now and then. As long you have some appropriate retry logic where appropriate, the message will eventually get through ...
I am trying to catch if timeout error occurs in Oracle. After googling a lot i did not find any specific fix for it. I have done below
try{}
catch (OracleException ex)
{
if (ex.Number == 01013)
CatchException(ex);
}
But i am not sure if timeout exception number is 01013.
Yes the code is 01013. Please refer the Oracle docs:
Default is 0 seconds, which enforces no time limit.
When the specified timeout value expires before a command execution
finishes, the command attempts to cancel. If cancellation is
successful, an exception is thrown with the message of ORA-01013: user
requested cancel of current operation. If the command executed in time
without any errors, no exceptions are thrown.
In a situation where multiple OracleCommand objects use the same
connection, the timeout expiration on one of the OracleCommand objects
may terminate any of the executions on the single connection. To make
the timeout expiration of a OracleCommand cancel only its own command
execution, simply use one OracleCommand for each connection if that
OracleCommand sets the CommandTimeout property to a value greater than
0.
Please note that there are possibly two other error codes which will throw when the .CommandTimeout is exceeded. Those are ORA-00936 and ORA-00604. Found in the docs.
When the specified timeout value expires before a command execution finishes, the command attempts to cancel. If cancellation is successful, an exception is thrown with the message of ORA-01013: user requested cancel of current operation. Other possible exceptions thrown after a command timeout expiration occurs include ORA-00936 and ORA-00604. If the command executed in time without any errors, no exceptions are thrown.
So your code should be:
try
{
// Data Access
}
catch (OracleException ex)
{
if (ex.Number == 01013 || ex.Number == 00936 || ex.Number == 00604)
CatchException(ex);
}
I have an ASP.Net application with the following code:
try
{
sql = new SqlProc("prcCustomerAgeSelect",
SqlProc.InParam("#DateFrom", SqlDbType.DateTime, 8, _OrderDateFrom),
SqlProc.InParam("#DateTo", SqlDbType.DateTime, 8, _OrderDateTo),
sql.Command.CommandTimeout = 1;
dt = sql.ExecuteTable();
}
catch (SqlException ex)
{
Filter.ErrorMessage = "Please narrow your search criteria.";
}
Note the line:
sql.Command.CommandTimeout = 1;
Which causes a SqlException to be thrown (for testing).
I would have thought that the catch block would catch this exception, but it doesn't. Instead, I get:
Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
[SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.]
Why doesn't it catch it?? Am I using the wrong type? What am I missing here?
Thanks in advance!!
-Ev
It sounds like what you're seeing isn't a SqlException.
It's possible that SqlProc is itself catching SqlExceptions, extracting some information from them, then throwing new exceptions of a different type (embedding some of the original info in the new exception's message).
It looks like it may be a connection timeout, rather than a command timeout.
I would check to make sure that you can connect and run the query with a more sensible timeout value just to verify this.
Suggestions on resolving this? I keep coming up with "missing" line numbers when consulting the google machine and that isn't the issue we are having. We have a line number but it doesn't point to anything but a closing brace.
Could this be because it is a timeout? It seems strange that it consistently gives up at the very end of the method, and the same method no less. The time outs are not necessarily frequent and the application (win forms calling asmx web service) does timeout in other places at times.
Edit: Code and Stack trace.
public DataSet GetData(...)
{
// About 18 try/catch blocks loading tables in dataset, all similar to below
try
{
// Create Table Adapter
// Fill Table
}
catch (Exception ex)
{
LogError(ex, System.Reflection.MethodBase.GetCurrentMethod(), null);
throw ex;
}
} //Line 479
System.Web.Services.Protocols.SoapException:
System.Web.Services.Protocols.SoapException:
Server was unable to process request.
---> System.Data.SqlClient.SqlException:
Timeout expired. The timeout period
elapsed prior to completion of the
operation or the server is not
responding. at
MonitoringDataService.AddAllData(DataSet
Data, DateTime LastSync, String
PrevAreas, String NewAreas, DateTime
PrevStartDate, DateTime PrevEndDate,
DateTime NewStartDate, DateTime
NewEndDate, Int32 CurrentUser, Boolean
IsInGroup) in
MonitoringDataService.cs:line 479
Worth noting that this is the inner exception.
Likely causes:
The code that is running is different from the source you are debugging from. This is the most likely cause.
Line could be the line after a throw new exception(...)
More than likely it's not really erroring on the end brace, but the line before it.