相关疑难解决方法(0)

当HTTP状态代码为401 Unauthorized时,IErrorHandler返回错误的消息体

我已经实现了IErrorHandler来处理在我的restful WCF服务的构造函数中抛出的授权异常.捕获常规异常时,我的自定义类型按预期返回,但ContentType标头不正确.

HTTP/1.1 500 Internal Server Error
Content-Type: application/xml;
...

{"ErrorMessage":"Error!"}
Run Code Online (Sandbox Code Playgroud)

但是,当错误处理程序尝试返回401 Unauthorized http状态代码时,消息正文将被覆盖为默认类型,但ContentType标头应该是它应该的样本.

HTTP/1.1 401 Unauthorized
Content-Type: application/json; 
...

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
Run Code Online (Sandbox Code Playgroud)

显然这里有些不对劲,但我不确定是什么.

如何实现IErrorHandler,使其在json中使用正确的标头返回我的自定义类型?

BaseDataResponseContract对象:

[Serializable]
[DataContract( Name = "BaseDataResponseContract" )]
public class BaseDataResponseContract
{
    [DataMember]
    public string ErrorMessage { get; set; }

} // end
Run Code Online (Sandbox Code Playgroud)

这是我想要返回的对象.我的应用程序中的所有其他对象都继承自此对象.当抛出异常时,我们真正关心的是http状态代码和错误消息.

IErrorHandler实现(为简洁起见,未显示日志记录):

namespace WebServices.BehaviorsAndInspectors
{
    public class ErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            return true;

        } // end

        public void ProvideFault(Exception ex, MessageVersion version, ref Message fault)
        {
            // Create a …
Run Code Online (Sandbox Code Playgroud)

c# wcf json ierrorhandler

8
推荐指数
1
解决办法
822
查看次数

WCF路由服务 - 动态错误处理

我正在学习使用WCF路由服务可以做些什么.仍然在'拧紧它,看看它能做什么'阶段.

我对路由服务的理解是,当消息通过时,服务将尝试将其传递给备份列表中首先出现的任何端点.如果失败了,它将继续尝试下一个,然后是下一个,直到任何一个有效或者没有什么可以尝试.

我想做的是访问该失败事件,以便我可以:

  1. 记录失败
  2. 通过电子邮件发送端点失败的通知
  3. (可选)从备份列表中删除端点,以便它不会减慢将来的消息流过系统的速度

无法找到如何扩展WCF框架以获取此特定事件.

这是WCF路由服务可以做的事情吗?任何朝着正确方向的推动都将非常感激.


目前,我在IIS下托管了30-beh动态生成的路由服务(或者更准确地说,是Visual Studio 2010的ASP.NET开发服务器).我正在设置Global.asax中服务的路由,如下所示.

    protected void Application_Start(object sender, EventArgs e)
    {
        List<Type> serviceTypes = ServiceUtility.GetServiceTypes();
        foreach (Type st in serviceTypes)
        {
            string route = String.Format("Services/{0}.svc", ServiceUtility.GetServiceName(st));
            RouteTable.Routes.Add(new ServiceRoute(route, new RoutingServiceHostFactory(st), typeof(System.ServiceModel.Routing.RoutingService)));
        }
    }
Run Code Online (Sandbox Code Playgroud)

ServiceUtility和RoutingServiceHostFactory是自定义类.请注意,IPolicyService是我感兴趣的程序集中的WCF服务契约接口.

public static class ServiceUtility
{
    public static List<Type> GetServiceTypes()
    {
        Type policyInterfaceType = typeof(IPolicyService);
        Assembly serviceContractsAssembly = Assembly.GetAssembly(policyInterfaceType);
        Type[] serviceContractsAssemblyTypes = serviceContractsAssembly.GetTypes();

        List<Type> serviceTypes = new List<Type>();
        foreach (Type t in serviceContractsAssemblyTypes)
        {
            if (!t.IsInterface)
                continue;

            object[] attrib = …
Run Code Online (Sandbox Code Playgroud)

c# wcf wcf-routing custom-error-handling

6
推荐指数
1
解决办法
1591
查看次数