WCF IErrorHandler扩展未返回指定的错误 [英] WCF IErrorHandler Extension not returning specified Fault

查看:117
本文介绍了WCF IErrorHandler扩展未返回指定的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这里有一些WCF向导可以发现我的错误.

Hopefully there are some WCF wizards out there that can spot my mistake here.

我正在尝试通过基于RESTful JSON WCF服务的基于IErrorHandler的behaviorExtension设置全局错误处理程序.该方法的装饰如下:

I am trying to set up a global error handler via an IErrorHandler based behaviorExtension on a RESTful JSON WCF Service. The method is decorated as such:

[OperationContract]
[WebGet(UriTemplate = "screens/info", ResponseFormat = WebMessageFormat.Json)]

IErrorHandler实现是:

The IErrorHandler implementation is:

public class ErrorHandler : IErrorHandler
{
    public void ProvideFault(Exception error, 
                             MessageVersion version, 
                             ref Message fault)
    {
        var error = new JsonError 
                        { 
                            Message = error.Message, 
                            FaultCode = -1, 
                            StackTrace = error.StackTrace
                        };

        fault = Message.CreateMessage(version, 
                    "", 
                    ideaScreeningError, 
                    new DataContractJsonSerializer(
                        ideaScreeningError.GetType()));

        // tell WCF to use JSON encoding rather than default XML
        var wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);
        fault.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);

        //Modify response
        var rmp = new HttpResponseMessageProperty 
                      { 
                          StatusCode = HttpStatusCode.BadRequest, 
                          StatusDescription = "Bad Request"
                      };

        fault.Properties.Add(HttpResponseMessageProperty.Name, rmp);
    }

    public bool HandleError(Exception error)
    {
        return true;
    }
}

我可以(通过断点)验证该扩展正在被调用并且正在正确执行.当我在浏览器中查看AJAX调用的结果时,可以看到WCF仍返回500 Internal Server Error,而不是我在错误处理程序中指定的Fault详细信息.

I can verify (via breakpoints) that the extension is being called and is executing properly. When I look at the result of the AJAX call in the browser, I can see that WCF is still returning a 500 Internal Server Error rather than the Fault details that I've specified in the error handler.

如果我更改WCF方法中引发的异常类型,这些异常类型将反映在浏览器的结果中,因此我可以推测WCF正在做一些处理异常并在内部返回的事情.

If I change Exception types being thrown in the WCF method, those are reflected in the result in the browser so I can surmise that WCF is doing something to handle the Exception and return something internally.

我如何使其停止!?

编辑

我要添加自定义行为元素:

I'm adding the custom Behavior Element:

public class ErrorBehaviorElement : BehaviorExtensionElement
{
    protected override object CreateBehavior()
    {
        return new ErrorBehavior();
    }

    public override Type BehaviorType
    {
        get { return typeof(ErrorBehavior); }
    }
}

行为:

internal class ErrorBehavior : WebHttpBehavior
{
    protected override void AddServerErrorHandlers(ServiceEndpoint endpoint,
        EndpointDispatcher endpointDispatcher)
    {
        // clear default error handlers.  
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear();

        // add the Json error handler.  
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(
            new ErrorHandler());
    }  
}

推荐答案

这里的问题在于WCF Rest Starter Kit(由于我没有开始这个项目,所以我没有意识到它在使用中),更具体地说WebServiceHost2.我在Reflector中打开了ServiceHost,并在OnOpening()中找到了这段可爱的小代码:

The issue here lies with the WCF Rest Starter Kit (which I didn't realize was in use since I didn't start this project), more specifically WebServiceHost2. I opened the ServiceHost in Reflector and found this lovely little piece of code in OnOpening():

if (endpoint.Behaviors.Find<WebHttpBehavior>() != null)
{
    endpoint.Behaviors.Remove<WebHttpBehavior>();
    WebHttpBehavior2 item = new WebHttpBehavior2();
    // other code omitted
    endpoint.Behaviors.Add(item);
}

如您所见,无论您要添加到端点的任何行为,只要它继承自WebHttpBehavior,Rest Start Kit组件都将劫持您的处理程序,将其删除,并将其替换为自己的处理程序.

As you can see, no matter what behavior you want added to the endpoint, as long as it inherits from WebHttpBehavior the Rest Start Kit components will hijack your handler, remove it, and replace it with its own.

请记住,WebHttpBehavior2也继承自WebHttBehavior,因此在我的扩展程序中继承自WebHttpBehavior2并没有帮助.

Keep in mind that WebHttpBehavior2 also inherits from WebHttBehavior so inheriting from WebHttpBehavior2 in my extension did nothing to help the matter.

第一步是创建一个新的WebSeriveHost,它从WebServiceHost2派生并覆盖OnOpening(),然后重新劫持Rest Starter Kit从我那里偷来的东西:

The first step was to create a new WebSeriveHost that derived from WebServiceHost2 and overrode OnOpening() and re-hijack what the Rest Starter Kit stole from me:

if(endpoint.Behaviors.Find<WebHttpBehavior>() != null)
{
    endpoint.Behaviors.Remove<WebHttpBehavior>();
    var item = ErrorBehavior();
    // other code
    endpoint.Behaviors.Add(item);
}

然后创建一个新的WebServiceHostFactory,以返回我的自定义WebServiceHost类型.

And then create a new WebServiceHostFactory that returned my custom WebServiceHost type.

这篇关于WCF IErrorHandler扩展未返回指定的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆