简体   繁体   中英

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 : 在此处输入图片说明

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

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)
    { }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM