WCF 数据服务中的自定义错误响应

发布于 2024-09-17 21:17:51 字数 1205 浏览 5 评论 0原文

在我的 DataService 派生类中,我重写了 HandleException 方法,以便进行自定义异常处理(简化示例):

    protected override void HandleException(HandleExceptionArgs args)
    {
       args.Exception = new DataServiceException(
            args.ResponseStatusCode,
            "RecordAlreadyExist",
            "The record with the given ID already exists in the database",
            System.Globalization.CultureInfo.CurrentCulture.Name,
            null);

        base.HandleException(args);
    }

这将导致类似于以下 JSON 响应的结果:

    {
       "error": { 
             "code": "RecordAlreadyExist",
             "Message": {
                  "Language": "en-US",
                  "Message": "The record with the given ID already exists in the database"
             }
        }
     }

但我想在错误响应中提供一些附加信息,例如

    {
       "error": { 
             "code": "RecordAlreadyExist",
             "Message": {
                  "Language": "en-US",
                  "Message": "The record with the given ID already exists in the database"
             },
             "RecordID": "1234"
        }
     }

:我可以这样做吗? DataServiceException 仅支持错误代码和消息...

In my DataService derrived class I have overriden the HandleException method in order to do custom exception handling (simplified example):

    protected override void HandleException(HandleExceptionArgs args)
    {
       args.Exception = new DataServiceException(
            args.ResponseStatusCode,
            "RecordAlreadyExist",
            "The record with the given ID already exists in the database",
            System.Globalization.CultureInfo.CurrentCulture.Name,
            null);

        base.HandleException(args);
    }

This would result into something like the following JSON response:

    {
       "error": { 
             "code": "RecordAlreadyExist",
             "Message": {
                  "Language": "en-US",
                  "Message": "The record with the given ID already exists in the database"
             }
        }
     }

But I would like to have some additional information in the error response like:

    {
       "error": { 
             "code": "RecordAlreadyExist",
             "Message": {
                  "Language": "en-US",
                  "Message": "The record with the given ID already exists in the database"
             },
             "RecordID": "1234"
        }
     }

How can I do that? DataServiceException only supports an error code and a message...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文