自定义错误处理消息自定义WebServiceHost [英] Custom Error Handling message for Custom WebServiceHost
问题描述
我有一个自定义WCF服务主机(<$ C C $> webServicehost2 )和工厂是做一些依赖注入(实现自定义的 IInstanceProvider
),也有一些自定义验证(impementing一个自定义的 RequestInterceptor
)。
I Have a Custom WCF Service Host (webServicehost2
) and Factory that is doing some dependency injection (implementing a custom IInstanceProvider
) and also some custom authentication (impementing a custom RequestInterceptor
).
我在一个非常小的问题,当我浏览到不exsist一个REST资源。 例如:
I Have a very small issue in that when i navigate to a REST resource that does not exsist. for example
http://localhost/restservice.svc/
http://localhost/restservice.svc/blah/
我收到了 404错误
,OK,预计。
我想知道,反正是有,我可以捕捉到返回的HTTP错误和格式化它更好一点。
What I would like to know is there anyway that I can capture the returning Http error and format it a little nicer.
推荐答案
我不知道是否有更简单的方法,但我做了一些快速原型这里,发现做到这一点的一种方法是通过添加自定义IDispatchMessageInspector实现,着眼于服务的响应消息和替换自己的自定义HTML页面的内容。
I'm not sure if there's an easier way, but I did some quick prototyping here, and found that one way to accomplish this is by adding a custom IDispatchMessageInspector implementation that looks at the response message of the service and replaces the content with your own custom HTML page.
它去基本上是这样的:
- 实施IDispatchMessageInspector.BeforeSendReply()
- 看回复信息,并获取<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.htt$p$psponsemessageproperty.aspx"相对=nofollow> HTT presponse 的财产,并检查它是否是一个基于<一个错误href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.htt$p$psponsemessageproperty.status$c$c.aspx"相对=nofollow>状态code 属性。
- 如果这是一个错误,打造一个全新的WCF消息体设置为要服务于HTML内容。
- 添加HTT presponse和<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.webbodyformatmessageproperty.aspx"相对=nofollow> webBodyFormatMessageProperty 性能再新的消息,并设置作为回复。
- Implement IDispatchMessageInspector.BeforeSendReply()
- Look at the reply message, and grab the httpResponse property, and check if it is an error based on the StatusCode property.
- If it's an error, create a brand new WCF message with the body set to the HTML content you want to serve.
- Add the httpResponse and webBodyFormatMessageProperty properties to the new message again and set that as the reply.
我有我的样本,成功地做到这一点,但它的方式丑陋的;我需要发布之前,把它清理干净一点。
I have my sample that does this successfully, but it's way ugly; I'll need to clean it up a bit before posting it.
这篇关于自定义错误处理消息自定义WebServiceHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!