[英]WCF FaultException IErrorHandler - Not returning to client and causing timeout exception
[英]WCF IErrorHandler Extension not returning specified Fault
希望那里有一些WCF向导可以在这里发现我的错误。
我试图通过基于RESTful JSON WCF服务的基于IErrorHandler的behaviorExtension设置全局错误处理程序。 该方法的修饰如下:
[OperationContract]
[WebGet(UriTemplate = "screens/info", ResponseFormat = WebMessageFormat.Json)]
IErrorHandler实现是:
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详细信息。
如果我更改了WCF方法中引发的Exception类型,则这些类型将反映在浏览器的结果中,因此我可以推测WCF正在做某些事情来处理Exception并在内部返回某些内容。
我如何使其停止!?
编辑
我要添加自定义行为元素:
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()
发现了这段可爱的小代码:
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组件都将劫持您的处理程序,将其删除,并将其替换为自己的处理程序。
请记住,WebHttpBehavior2也继承自WebHttBehavior,因此在我的扩展程序中继承自WebHttpBehavior2并没有帮助。
第一步是创建一个从WebServiceHost2
派生的新WebSeriveHost,并覆盖OnOpening()
并重新劫持Rest Starter Kit从我那里偷来的东西:
if(endpoint.Behaviors.Find<WebHttpBehavior>() != null)
{
endpoint.Behaviors.Remove<WebHttpBehavior>();
var item = ErrorBehavior();
// other code
endpoint.Behaviors.Add(item);
}
然后创建一个新的WebServiceHostFactory,它返回了我的自定义WebServiceHost类型。
不要忘记也设置响应的ContentType:
rmp.Headers[HttpResponseHeader.ContentType] = "application/json";
根据评论,我将尝试删除常见的webHttpBehavior。 您已经定义了从webHttp派生的自己的行为。 您的服务配置中没有理由出现两种webHttp行为。 此外,webHttp行为还添加了自己的错误处理程序,其行为与您描述的完全相同。 也许没有帮助,但是您可以尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.