我有一个Web API,它向执行某些任务/命令的Windows服务发出HTTP请求.
如果我的'service'抛出异常,那么我想使用JSON将该异常传递回Web API.然后我想将异常反序列化回异常对象并抛出它.
我的代码:
Web API和服务之间的共享异常:
public class ConnectionErrorException : Exception
{
public ConnectionErrorException()
{
}
public ConnectionErrorException(String message)
: base(message)
{
}
}
Run Code Online (Sandbox Code Playgroud)
现在在我的服务中,我有以下代码:
...
try
{
result = await ExecuteCommand(userId);
//If reached here nothing went wrong, so can return an OK result
await p.WriteSuccessAsync();
}
catch (Exception e)
{
//Some thing went wrong. Return the error so they know what the issue is
result = e;
p.WriteFailure();
}
//Write the body of the response:
//If the result …Run Code Online (Sandbox Code Playgroud)