handle unhandled exception - c#

I have a Ria service to call logic code. I want to write try catch block in every logic function to provide ways to handle unhandeled exceptions.
try
{
//something
}
catch(BussinessException e)
{
//save e.info to database
}
But I don't want to write this block code everywhere in my logic, and I don't want to put the exception handling piece in RIA service since another type of service also call the logic.
Does anybody have a one-place exception handling solution for me?

Based off your history I am pretty sure this is C# so here is my take.
The best way to avoid the duplication would be to wrap your logic like this.
private static void ExecuteLogic(Action action)
{
try
{
action();
}
catch(BussinessException e)
{
//save e.info to database
}
}
With this in place you can easily perform various operations that share the same error handling.
ExecuteLogic(
() =>
{
// Do something...
}
);

If you want only to log exception, as I can see from your example, you can subscribe to AppDomain.FirstChanceException. But you wouldn't be able to handle it. Oh. btw this event was introduces only in .NET 4 :(.

Here is a more conventional Object Oriented solution using Command Pattern.
public interface ICommand {
void Execute();
}
public class BankAccountWebServiceCall: ICommand(){
string name;
int accountNo;
public BankAccountWebServiceCall(string name, int accountNo) {
this.name= param1;
this.accountNo= accountNo;
}
//ICommand implementation
public void Execute() {
SomeWebService.Call(name, accountNo);
}
}
public class WebServiceCaller {
public void CallService(ICommand command) {
try {
command.Execute();
} catch (SomeBusinessException ex) {
//handle exception
}
}
}
public class WebServiceCallerTest {
public static void CallServiceTest() {
new WebServerCaller().CallService(new TwoParameterwebServiceCall("Igor", 12345));
}
}

implement an IHttpModule
web.config:
<httpModules>
...
<add type="Citiport.Web.Module.ErrorHandleModule" name="GlobalErrorHandler" />
...
</httpModules>
The Class:
public class ErrorHandleModule : IHttpModule
{
private static readonly ILog logger = LogManager.GetLogger("Citiport.Web.Module.ErrorHandleModule");
public ErrorHandleModule()
{
}
void IHttpModule.Dispose()
{
}
void IHttpModule.Init(HttpApplication context)
{
context.Error += new System.EventHandler(onError);
}
public void onError(object obj, EventArgs args)
{
HttpContext ctx = HttpContext.Current;
HttpResponse response = ctx.Response;
HttpRequest request = ctx.Request;
Exception exception = ctx.Server.GetLastError();
/* handling exception here*/
}
}
}
refer:http://code.google.com/p/citiport2/source/browse/trunk/CitiportV2_website/App_Code/Web/ErrorHandleModule.cs

Related

How to get the values from inside the code when exception occurs?

I have a worker service in .net core 3.1
in my Program.cs i have the below codes
public static void Main(string[] args)
{
try
{
CreateHostBuilder(args).Build().Run();
}
catch(Exception ex)
{
Handler(ex);
}
}
static void Handler( Exception e)
{
var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
List<Test> _Test = new List<Test>()
{
new Test()
{
}
};
LogEventInfo eventInfo = new LogEventInfo
{
Level = LogLevel.Error,
Properties = { { "Application",_Test } }
};
logger.Log(eventInfo);
}
private class Test
{
public string Name{get;set;}
public string Place{get;set;}
}
In my worker class i have code as below
public class Worker : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
do
{
string Name ="MyName";// These values will be fetched from different file
string Place="MyPlace";
//Some Logic where an exception may occur
}
while (!stoppingToken.IsCancellationRequested);
}
}
Is there anyway to get the values of Name and Place of worker class to Handler method in program class when an exception arises. Since I'm thinking of a global exception handler I'm thinking of not putting any more try catch blocks. I want to handle all the exception with the try catch in the program.cs file. How can i get the Name and Place values onto my handler on such scenario so that it can be logged?
While a custom exception is a possibility, you could also simply decorate any exception thrown inside your service with those properties using the Data property:
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
do
{
string Name = "MyName";
string Place = "MyPlace";
try
{
//Some Logic where an exception may occur
}
catch (Exception e)
{
e.Data["Name"] = Name;
e.Data["Place"] = Place;
throw;
}
}
while (!stoppingToken.IsCancellationRequested);
}
Create a custom Exception class where you can set Name & Place as properties.
In the Worker, add a try catch block around the code that may throw an exception. Create and throw your custom exception, setting the original exception as the InnerException (https://learn.microsoft.com/en-us/dotnet/api/system.exception.innerexception?view=net-6.0)
Then in your handler get the name / place from the wrapper exception, and then use the InnerException for the rest.

How to generate transient errors for testing

I have implemented custom retry strategy on an API which is in .NET Core 2.2. Retry strategy should work only for transient errors from the database(Azure SQL). How can I generate transient errors, to test this feature?
I don't know your code, but this is one solution. The idea is to separate the retry logic from the actual action that needs to be retried, so that each can be tested.
A static RetryHandler could also work, but it depends on your need.
Psedo code, does not compile.
public RetryHandler : IRetryHandler
{
private static List<Type> transientErros = new List<Type>
{
typeof(TimeoutException),
typeof(SomeotherExceptionThatRequiresRetry),
}
public void RetryOnTransientError(Action action, int attempts = 3)
{
for (var i = 0; i < attempts; i++)
{
try
{
action();
return;
}
catch(Exception e)
{
if(transientErros.Contains(e.GetType())
continue;
throw;
}
}
}
}
public class UserRepository
{
private IMyDbContext context;
private IRetryHandler retryHandler;
public Repository(IMyDbContext context, IRetryHandler retryHandler)
{
this.context = context;
this.retryHandler = retryHandler;
}
public void InsertUser(User user)
{
retryHandler.RetryOnTransientError(() => DoInsertUser(user));
}
private void DoInsertUser(User user)
{
// insert logic
context.SaveChanges();
}
}
[Test]
public void InsertUserRetriesOnTransientError()
{
// Arrange
var contextMock = new Mock<IMyDbContext>();
var repository = new Repository(contextMock);
var user = CreateUser();
contextMock.Setup(x => x.SaveChanges()).Throws<TransientException>());
// Act
Assert.Throws<TransientException>(() => repository.InsertUser(user));
// Assert
// verify SaveChanges() was called 3 times
}

Application Insights with invalid instrumentation key - how to handle as no error is thrown

Is there a way to capture an error when when an invalid instrumentation key is used when tracing messages to Application Insights?
I'm programmatically specifying an instrumentation key like below but no exception is thrown. I'm trying to build a Logging WebApi that will return a success or failure dependent on whether the message was successfully logged to Application Insights?
TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
config.InstrumentationKey = "ABC";
client.TrackTrace("Test"),SeverityLevel.Information);
You should implement your own channel that implements ITelemetryChannel, and handle exceptions as you want.
Here's a naive example:
public class SynchronousTelemetryChannel : ITelemetryChannel
{
private const string ContentType = "application/x-json-stream";
private readonly List<ITelemetry> _items;
private object _lock = new object();
public bool? DeveloperMode { get; set; }
public string EndpointAddress { get; set; }
public SynchronousTelemetryChannel()
{
_items = new List<ITelemetry>();
EndpointAddress = "https://dc.services.visualstudio.com/v2/track";
}
public void Send(ITelemetry item)
{
lock (_lock)
{
_items.Add(item);
}
}
public void Flush()
{
lock (_lock)
{
try
{
byte[] data = JsonSerializer.Serialize(_items);
new Transmission(new Uri(EndpointAddress), data, ContentType, JsonSerializer.CompressionType).SendAsync().Wait();
_items.Clear();
}
catch (Exception e)
{
// Do whatever you want.
}
}
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
Then, initialize the configuration with your channel via code or configuration file:
TelemetryConfiguration.Active.TelemetryChannel = new SynchronousTelemetryChannel();

Control flow in asynchronous calls

I'm facing difficulties understanding how to handle program control during asynchronous flow.
I have a SessionManager class which calls the initiates the session and we need to register
for the event OnStartApplicationSessionResponse and my control will return to the calling point. I will get the session id in the eventhandler after sometime or the error code if there is an error.
class SessionManager
{
public bool startUp(Object params)
{
try
{
serviceProvider = new ServiceProvider();
serviceProvider.OnStartApplicationSessionResponse += new StartApplicationSessionResponseHandler(ServiceProvider_OnStartApplicationSessionResponse);
serviceProvider.startUp(params);
}
}
public void ServiceProvider_OnStartApplicationSessionResponse(object sender, ServiceProvider.StartApplicationSessionResponseArgs e)
{
//e.getError
//I will get the session Id here or error code
}
}
How do I get sessionId or the error as my control is now at the calling position?
You could use TaskCompletionSource to make the Event awaitable.
class SessionManager
{
private ServiceProvider _serviceProvider;
public int SessionId
{
get;
private set;
}
public Task<bool> StartUp(Object param)
{
_serviceProvider = new ServiceProvider();
var tcs = new TaskCompletionSource<bool>();
_serviceProvider.OnStartApplicationSessionResponse += (sender, args) =>
{
// do your stuff
// e.g.
SessionId = 0xB00B5;
tcs.SetResult(true);
};
_serviceProvider.startUp(param);
return tcs.Task;
}
}
The call would look like:
private static async void SomeButtonClick()
{
var mgr = new SessionManager();
var success = await mgr.StartUp("string");
if (success)
{
Console.WriteLine(mgr.SessionId);
// update ui or whatever
}
}
note: This Feature is available in .Net 4.5.
With the C# feature async and await you are able to rewrite an asynchronous flow into something that is like a synchronous flow. You have only provided some fragments of your code so to provide a complete example I have created some code that resembles your code:
class StartEventArgs : EventArgs {
public StartEventArgs(Int32 sessionId, Int32 errorCode) {
SessionId = sessionId;
ErrorCode = errorCode;
}
public Int32 SessionId { get; private set; }
public Int32 ErrorCode { get; private set; }
}
delegate void StartEventHandler(Object sender, StartEventArgs e);
class ServiceProvider {
public event StartEventHandler Start;
public void Startup(Boolean succeed) {
Thread.Sleep(TimeSpan.FromSeconds(1));
if (succeed)
OnStart(new StartEventArgs(321, 0));
else
OnStart(new StartEventArgs(0, 123));
}
protected void OnStart(StartEventArgs e) {
var handler = Start;
if (handler != null)
handler(this, e);
}
}
The ServiceProvider.Startup method will delay for a second before firing an event that either signals success or failure depending on the succeed parameter provided. The method is rather silly but hopefully is similar to the behavior of your ServiceProvider.Startup method.
You can convert the asynchronous startup into a task using a TaskCompletionSource:
Task<Int32> PerformStartup(ServiceProvider serviceProvider, Boolean succeed) {
var taskCompletionSource = new TaskCompletionSource<Int32>();
serviceProvider.Start += (sender, e) => {
if (e.ErrorCode > 0)
throw new Exception(e.ErrorCode.ToString());
taskCompletionSource.SetResult(e.SessionId);
};
serviceProvider.Startup(succeed);
return taskCompletionSource.Task;
}
Notice how an error signaled by the Start event is converted into an Exception (in production code you should use a custom exception type instead).
Using the async and await feature of C# you can now write code that looks very much like synchronous code even though it actually is asynchronous:
async void Startup(Boolean succeed) {
var serviceProvider = new ServiceProvider();
try {
var sessionId = await PerformStartup(serviceProvider, succeed);
Console.WriteLine(sessionId);
}
catch (Exception ex) {
Console.WriteLine(ex);
}
}
If an error is reported by the Start event you can now deal with in the catch block. Also the session ID is simply a return value of the function. The "magic" is that using await on a Task will return the result of the task when it completes and if an exception is thrown in the task it can be caught on the thread awaiting the task.

Check if a async call to service is still alive

I have an async service
The service contract defined like:
[ServiceBehavior(InstanceContextMode = InstanceContext.PerCall]
Myservice
My client is defined like:
MyServiceClient task= null;
InstanceContext instanceContext = new InstanceContext(this);
task = new MyServiceClient(instanceContext);
task.MyMethod();
And the client class implements the call back methods (finish, progress etc...).
It's works fine, but if I call to the method, and she start running on the server and I shut down the server,I can't know the status of my call, and the client still think that the methods still running.
So, how can I check if this call is still running?
Thanks for helpers :)
Edit:
CallBack Interface:
public interface IServiceCallback
{
[OperationContract(IsOneWay=true)]
void NotifyFinished();
[OperationContract(IsOneWay=true)]
void NotifyProgress(int x);
[OperationContract(IsOneWay=true)]
void NotifyFailed(Exception exception);
}
Service Interface:
[ServiceContract(CallbackContract = typeof (IServiceCallback)]
public interface IAsyncService
{
[OperationContract(IsOneWay=true)]
void AsyncRunning();
}
Service Class:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class AsyncService : IAsyncService
{
private IServiceCallback ServiceCallback {get; set;}
public void AsyncRunningProxy ()
{
for(int x=0; x<100 ; x++)
{
AsyncService.NotifyProgress(x);
}
}
private void EndMethod(IAsyncResult res)
{
AsyncResult result = (AsyncResult)res;
try
{
((dynamic)result.AsyncDelegate).EndInvoke(res);
AsyncService.NotifyFinished();
}
catch (Exception e)
{
AsyncService.NotifyFailed(e);
}
}
public void AsyncRunning ()
{
ServiceCallback = OperationContext.Current.GetCallBackChannel<IServiceCallback>();
Action action = AsyncRunningProxy;
action.BeginInvoke(EndMethod, null);
}
}
Client Class:
public class ServiceRunner : IServiceCallback
{
private ManualResetEvent reset {get; set;}
public ServiceRunner()
{
reset = new ManualResetEvent(false);
}
public void Run()
{
AsyncServiceClient client = null;
InstanceContext instanceContext = new InstanceContext(this);
client = new AsyncServiceClient(instanceContext);
client.AsyncRunning();
reset.WaitOne();
}
public void NotifyProgress(int x)
{
Console.WriteLine(x);
}
public void NotifyFinished()
{
}
public void NotifyFailed(Exception e)
{
Console.WriteLine(e.Message);
reset.Set();
}
}
Edit: new client Class:
Client Class:
public class ServiceRunner : IServiceCallback
{
private ManualResetEvent reset { get; set; }
private string IsRunning { get; set; }
public ServiceRunner()
{
reset = new ManualResetEvent(false);
IsRunning = true;
}
public void Run()
{
AsyncServiceClient client = null;
InstanceContext instanceContext = new InstanceContext(this);
client = new AsyncServiceClient(instanceContext);
client.AsyncRunning();
new Thread(()=>
{
while(IsRunning)
{
try
{
client.IsAlive();
Thrad.Sleep(60 * 1000);
}
catch (Exception e) // The server is not responding.
{
NotifyFailed(e);
return;
}
}
}).Start();
reset.WaitOne();
}
public void NotifyProgress(int x)
{
Console.WriteLine(x);
}
public void NotifyFinished()
{
IsRunning = false;
reset.Set();
}
public void NotifyFailed(Exception e)
{
IsRunning = false;
Console.WriteLine(e.Message);
reset.Set();
}
}
In order to have more control of your clients request to the service, you should be able to use the inbuilt Task and Async support to monitor and if necessary handle connection delays.
The support for generating Task-based operations on the client side will be available to users who rely on proxies generated by our client generation tools (svcutil.exe or Add Service Reference), as well as to users who prefer to directly use ChannelFactory
The following code provides a rough example:
Task<string> task = new MyServiceClient().MyMethod();
if (task == await Task.WhenAny(task, Task.Delay(1000)))
{
Console.WriteLine(await task);
}
else
{
// handle delay …
}
Refer to the following MSDN blog entry for more information:
http://blogs.msdn.com/b/endpoint/archive/2010/11/13/simplified-asynchronous-programming-model-in-wcf-with-async-await.aspx
Regards,
As #adkSerenity mention you may implement timeout logic, but I guess your question not about that.
The callback method will be(and should be) called in case of exception for example connection lose or internal connection time out.
private static void CallbackSample(IAsyncResult asynchronousResult)
{
try
{
// State of request is asynchronous.
RequestState myRequestState=(RequestState) asynchronousResult.AsyncState;
HttpWebRequest myHttpWebRequest2=myRequestState.request;
myRequestState.response = (HttpWebResponse);
//next line may throw exception
myHttpWebRequest2.EndGetResponse(asynchronousResult);
}
catch(WebException e)
{
}
}
So async communication looks like fire and forget. Your callback method will be called when you get the result(exception too), but if you decide not handle it(custom time out logic) you must "foret" about callback processing. No way to check is alive(except of course custom api).

Categories