小编Ili*_*mov的帖子

使用JSON在两个C#程序之间传递异常

我有一个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)

c# json exception json.net asp.net-web-api

5
推荐指数
1
解决办法
2529
查看次数

标签 统计

asp.net-web-api ×1

c# ×1

exception ×1

json ×1

json.net ×1