Search code examples
c#azureexceptionasp.net-coreazure-service-fabric

handling exceptions in service fabric


I have an a controller method in my api calling a service which throws a custom exception.

customException : 
    public class DematerialisationException : Exception
{
    private static readonly string DefaultMessage = "An error occurred during dematerialisation.";

    public DematerialisationException() : base(DefaultMessage)
    { }

    public DematerialisationException(string message) : base(message) 
    { }

    public DematerialisationException(Exception inner) : base (DefaultMessage, inner)
    { }

    public DematerialisationException(string message, Exception inner) : base (message, inner)
    { }
}

the method called in the service directly throws the exception :

public async Task<LiasseDetails> CreateLiasseAsync(LiasseCreate liasseData) => 
throw new DematerialisationException();

then the caller : enter image description here

I would have expected ex3 to be hit instead it's ex2 as you can see in the screenshot. the innerException is in the correct type, so why is the wrong breakpoint hit? What did I do wrong?

thanks


Solution

  • In the variable watcher, you can see that a ServiceException is thrown when getting the value of InnerException, this happened because the exception object could not be serialized. So try to add the attribute [Serializable()] to DematerialisationException class :

    [Serializable()]
     public class DematerialisationException : Exception
    {
        private static readonly string DefaultMessage = "An error occurred during dematerialisation.";
    
        public DematerialisationException() : base(DefaultMessage)
        { }
    
        public DematerialisationException(string message) : base(message) 
        { }
    
        public DematerialisationException(Exception inner) : base (DefaultMessage, inner)
        { }
    
        public DematerialisationException(string message, Exception inner) : base (message, inner)
        { }
    }