C# 无法让全局errorhandler处理我的自托管wcf服务

C# 无法让全局errorhandler处理我的自托管wcf服务,c#,wcf,global,ierrorhandler,C#,Wcf,Global,Ierrorhandler,你好,我已经试着让Ierrorhandler工作了好几个小时了,我被卡住了:) 我从这本指南中得到了最多的结果 但是我不能让它与我的操作/功能一起工作 Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Phpwcfconsole { class program

你好,我已经试着让Ierrorhandler工作了好几个小时了,我被卡住了:) 我从这本指南中得到了最多的结果

但是我不能让它与我的操作/功能一起工作

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Phpwcfconsole
{
    class program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(Service1)))
            {
                try
                {
                    host.Open();
                    Console.WriteLine("Host open. Press any key to <EXIT>");
                    Console.ReadLine();
                    host.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(Environment.NewLine + e.Message);
                    host.Close(); 
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;


namespace Phpwcfconsole
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string GetData(int value);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Phpwcfconsole
{
    public partial class Service1 : IService
    {
        public string GetData(int value)
        {
            throw new Exception("error");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;
using System.ServiceModel.Configuration;

namespace Phpwcfconsole
{
    public class GlobalExceptionHandler : IErrorHandler
    {
        public bool HandleError(Exception ex)
        {
            return true;
        }

        public void ProvideFault(Exception ex, MessageVersion version,
                             ref Message msg)
        {
            // Do some logging here

            var newEx = new FaultException(
                string.Format("CALLED FROM YOUR GLOBAL EXCEPTION HANDLER BY {0}",
                              ex.TargetSite.Name));

            MessageFault msgFault = newEx.CreateMessageFault();
            msg = Message.CreateMessage(version, msgFault, newEx.Action);
        }
    }

    public class GlobalExceptionHandlerBehaviourAttribute : Attribute, IServiceBehavior
    {
        private readonly Type _errorHandlerType;

        public GlobalExceptionHandlerBehaviourAttribute(Type errorHandlerType)
        {
            _errorHandlerType = errorHandlerType;
        }

        public void Validate(ServiceDescription description, ServiceHostBase     serviceHostBase)
        {

        }

        public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase,
            Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
        {
            var handler = (IErrorHandler)Activator.CreateInstance(_errorHandlerType);

            foreach (ChannelDispatcherBase dispatcherBase in serviceHostBase.ChannelDispatchers)
            {
                var channelDispatcher = dispatcherBase as ChannelDispatcher;
                if (channelDispatcher != null)
                channelDispatcher.ErrorHandlers.Add(handler);
            }
        }
    }
}
Serverfunctions.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Phpwcfconsole
{
    class program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(Service1)))
            {
                try
                {
                    host.Open();
                    Console.WriteLine("Host open. Press any key to <EXIT>");
                    Console.ReadLine();
                    host.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(Environment.NewLine + e.Message);
                    host.Close(); 
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;


namespace Phpwcfconsole
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string GetData(int value);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Phpwcfconsole
{
    public partial class Service1 : IService
    {
        public string GetData(int value)
        {
            throw new Exception("error");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;
using System.ServiceModel.Configuration;

namespace Phpwcfconsole
{
    public class GlobalExceptionHandler : IErrorHandler
    {
        public bool HandleError(Exception ex)
        {
            return true;
        }

        public void ProvideFault(Exception ex, MessageVersion version,
                             ref Message msg)
        {
            // Do some logging here

            var newEx = new FaultException(
                string.Format("CALLED FROM YOUR GLOBAL EXCEPTION HANDLER BY {0}",
                              ex.TargetSite.Name));

            MessageFault msgFault = newEx.CreateMessageFault();
            msg = Message.CreateMessage(version, msgFault, newEx.Action);
        }
    }

    public class GlobalExceptionHandlerBehaviourAttribute : Attribute, IServiceBehavior
    {
        private readonly Type _errorHandlerType;

        public GlobalExceptionHandlerBehaviourAttribute(Type errorHandlerType)
        {
            _errorHandlerType = errorHandlerType;
        }

        public void Validate(ServiceDescription description, ServiceHostBase     serviceHostBase)
        {

        }

        public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase,
            Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
        {
            var handler = (IErrorHandler)Activator.CreateInstance(_errorHandlerType);

            foreach (ChannelDispatcherBase dispatcherBase in serviceHostBase.ChannelDispatchers)
            {
                var channelDispatcher = dispatcherBase as ChannelDispatcher;
                if (channelDispatcher != null)
                channelDispatcher.ErrorHandlers.Add(handler);
            }
        }
    }
}
ExeceptionHandler.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Phpwcfconsole
{
    class program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(Service1)))
            {
                try
                {
                    host.Open();
                    Console.WriteLine("Host open. Press any key to <EXIT>");
                    Console.ReadLine();
                    host.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(Environment.NewLine + e.Message);
                    host.Close(); 
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;


namespace Phpwcfconsole
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string GetData(int value);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Phpwcfconsole
{
    public partial class Service1 : IService
    {
        public string GetData(int value)
        {
            throw new Exception("error");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;
using System.ServiceModel.Configuration;

namespace Phpwcfconsole
{
    public class GlobalExceptionHandler : IErrorHandler
    {
        public bool HandleError(Exception ex)
        {
            return true;
        }

        public void ProvideFault(Exception ex, MessageVersion version,
                             ref Message msg)
        {
            // Do some logging here

            var newEx = new FaultException(
                string.Format("CALLED FROM YOUR GLOBAL EXCEPTION HANDLER BY {0}",
                              ex.TargetSite.Name));

            MessageFault msgFault = newEx.CreateMessageFault();
            msg = Message.CreateMessage(version, msgFault, newEx.Action);
        }
    }

    public class GlobalExceptionHandlerBehaviourAttribute : Attribute, IServiceBehavior
    {
        private readonly Type _errorHandlerType;

        public GlobalExceptionHandlerBehaviourAttribute(Type errorHandlerType)
        {
            _errorHandlerType = errorHandlerType;
        }

        public void Validate(ServiceDescription description, ServiceHostBase     serviceHostBase)
        {

        }

        public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase,
            Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
        {
            var handler = (IErrorHandler)Activator.CreateInstance(_errorHandlerType);

            foreach (ChannelDispatcherBase dispatcherBase in serviceHostBase.ChannelDispatchers)
            {
                var channelDispatcher = dispatcherBase as ChannelDispatcher;
                if (channelDispatcher != null)
                channelDispatcher.ErrorHandlers.Add(handler);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.ServiceModel;
使用System.ServiceModel.Channel;
使用System.ServiceModel.Dispatcher;
使用System.ServiceModel.Description;
使用System.Collections.ObjectModel;
使用System.ServiceModel.Configuration;
命名空间phpwconsole
{
公共类GlobalExceptionHandler:IErrorHandler
{
公共布尔句柄错误(例外情况除外)
{
返回true;
}
public-void-providefaulture(例外,例如,MessageVersion,
参考信息(msg)
{
//在这里做些日志记录
var newEx=新故障异常(
Format(“由{0}从全局异常处理程序调用”,
例如TargetSite.Name);
MessageFault msgFault=newEx.CreateMessageFault();
msg=Message.CreateMessage(版本,msgFault,newEx.Action);
}
}
公共类GlobalExceptionHandlerBehavior属性:属性,IServiceBehavior
{
私有只读类型_errorHandlerType;
公共GlobalExceptionHandlerBehaviorAttribute(类型errorHandlerType)
{
_errorHandlerType=errorHandlerType;
}
公共无效验证(ServiceDescription说明,ServiceHostBase ServiceHostBase)
{
}
public void AddBindingParameters(ServiceDescription、ServiceHostBase、ServiceHostBase、,
集合终结点、BindingParameterCollection参数)
{
}
public void ApplyDispatchBehavior(ServiceDescription,ServiceHostBase ServiceHostBase)
{
var handler=(IErrorHandler)Activator.CreateInstance(_errorHandlerType);
foreach(serviceHostBase.ChannelDispatchers中的ChannelDispatcherBase dispatcherBase)
{
var channelDispatcher=作为channelDispatcher的dispatcherBase;
if(channelDispatcher!=null)
channelDispatcher.ErrorHandlers.Add(处理程序);
}
}
}
}
好的,如果我在里面放一些控制台,writeLine

公共类GlobalExceptionHandlerBehavior属性:属性,IServiceBehavior 当我启动程序时,我看到它们运行的函数。但是我无法在指南中找到使用我的函数抛出异常并让我的IErrorhandler捕获该异常的示例

我在其他函数中尝试了一些异常,但我的IErrorhandler没有发生任何变化

但是有一个异常被捕获了,我发现到目前为止,当我在Wcftestclient中添加服务,然后停止调试并删除IService.cs中的[OperationContract],然后重新启动并尝试运行函数而不刷新时,该异常会被IErrorhandler捕获

所以我这里的问题是为什么我不能捕获函数中的异常? 非常感谢你的回答。
c(:

FaultContractAttribute是否足以满足您的要求? 看看msdn文档中的示例。 它听起来与您正在尝试做的事情相似,比定制服务行为复杂度更低


当跨进程/机器使用WCF时,FaultContracts相当于异常,因为使用者不一定是.net客户端。

hmm观看此站点[链接]我非常仔细地看到了我安装了带.net 4.0的windows xp的要求。现在运行windows update时大拇指交叉。有人知道这方面的情况吗?您使用哪些平台提供wcf服务?