WCF 剩余错误处理

标签 wcf rest error-handling

我在使用 WCF 4.0 RESTful 服务时遇到了令人兴奋的问题。我正在尝试创建一个休息服务,如果出现错误,它将返回一个描述问题的 xml 文档 例如:

    <ErrorHandler>
        <cause>Resource not  available</cause>
        <errorCode>111103</errorCode>
    </ErrorHandler>

为了做到这一点,我使用 Visual Studio 提供的模板创建了一个默认的 REST 服务 这是我的服务类别:

public class Service1
    {
        // TODO: Implement the collection resource that will contain the SampleItem instances

        [WebGet(UriTemplate = "")]        
        public List<SampleItem> GetCollection()
        {
            // TODO: Replace the current implementation to return a collection of SampleItem instances\
           // throw new WebException("lala");
           throw new WebFaultException<ErrorHandler>(new ErrorHandler { cause = "Resource not  available", errorCode = 100 }, System.Net.HttpStatusCode.NotFound);
            //return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
        }

        [WebInvoke(UriTemplate = "", Method = "POST")]
        public SampleItem Create(SampleItem instance)
        {
            // TODO: Add the new instance of SampleItem to the collection           
            return new SampleItem() { Id = 3, StringValue = "59" };

        }

        [WebGet(UriTemplate = "{id}")]
        public SampleItem Get(string id)
        {
            // TODO: Return the instance of SampleItem with the given id
            throw new NotImplementedException();
        }

        [WebInvoke(UriTemplate = "{id}", Method = "PUT")]
        public SampleItem Update(string id, SampleItem instance)
        {
            // TODO: Update the given instance of SampleItem in the collection
            throw new NotImplementedException();
        }

        [WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
        public void Delete(string id)
        {
            // TODO: Remove the instance of SampleItem with the given id from the collection
            throw new NotImplementedException();
        }

    }
}

正如您从上面的代码中看到的,我在 GetCollection 方法中抛出了 WebFaultException。应该在响应正文中放入一个“ErrorHandler”对象。 这是我的 ErrorHandler 类的样子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace WcfRestService1
{
    [DataContract]
    public class ErrorHandler
    {
        [DataMember]
        public int errorCode { get; set; }
        [DataMember]
        public string cause { get; set; }

    }
}

疯狂的是,这个东西可以工作,但不能用:))。我想说的是,Visual Studio 给我一个错误,指出用户代码未捕获 WebFaultException :|它暂停了我的应用程序。如果我按继续,一切都会正常进行。 以下是一些描述我的问题的图片:

fiddler 的第一步: First Step

接下来 Visual Studio 的错误:Visual Studio Error

最后按“继续”后一切正常:

这对我来说毫无意义,我不知道为什么会发生这种情况以及如何解决它:P。我在网上搜索了好几天试图找到解决方案但没有运气。我使用的是 Visual Studio 2010 Ultimate

最诚挚的问候:)

最佳答案

这里没有任何问题。您正在调试,抛出异常,它中断,您继续,它会按预期工作。

我怀疑您已将异常处理选项(Ctrl+Alt+E)设置为在引发异常时中断。 (选项中的“Thrown”)每当抛出异常时,无论是否处理它,都会导致中断。

WCF 操作中引发的异常将由 WCF 运行时处理,如果它们是故障,它们将按原样发送回来,以便 channel 不会出现故障。

现在,关于发回 XML,您只需使用 WebFaultException<string> 发送 XML 的字符串表示形式即可。 .

关于WCF 剩余错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626334/

相关文章:

image-processing - Photoshop/InDesign CS5 脚本 : image size conversion error is using too much memory?

php - 下载文件时抑制错误消息

Perl:如果 IPC::Run 终止,则从进程中检索输出

c# - 返回大字节数组的 Web 服务

api - Node.js Provider Rest API 和 angular.js WEB APP

c# - DataMember IsRequired 属性与 Nullable 类型的组合是否矛盾?

java - 如何在过滤器方法中使用 ContainerRequestContext 获取 REST api 的有效负载

http - REst GET 与大消息正文的 POST

wcf - 如何将 WSDL URL 从内部计算机名称更改为公共(public)计算机名称?

c# - 创建与 Windows 服务通信的非托管 DLL(在 C++ 中)?