我的主机中的Hadler错误EndPoint-WCF-行为

时间:2019-04-04 09:55:40

标签: c# wcf handler behavior

几天前,我提出一个问题,如果我能成功回答。我没有很好地解决问题,现在又有了更多的知识。

我需要捕获所有端点的错误,以将它们包含在同一站点中。想法是向这些端点添加行为。

namespace SIPE.Search.Helpers
{
    /// <summary>
    /// Implements methods that can be used to extend run-time behavior for an endpoint in either a client application.
    /// </summary>
    public class ExternalClientBehavior : BehaviorExtensionElement
    {
        protected override object CreateBehavior()
        {
            return new ExternalClientBehaviorClass();
        }

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

        /// <summary>
        /// JSON REST[GET] Converter Behavior
        /// </summary>
        private class ExternalClientBehaviorClass : IEndpointBehavior
        {
            public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
            {                
            }

            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
            {
                ExternalClientMessageInspector clientInspector = new ExternalClientMessageInspector(endpoint);
                clientRuntime.MessageInspectors.Add(clientInspector);

                foreach (ClientOperation op in clientRuntime.Operations)
                {
                    op.ParameterInspectors.Add(clientInspector);
                }
            }

            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
            {
                //("Behavior not supported on the consumer side!");
            }

            public void Validate(ServiceEndpoint endpoint)
            {

            }
        }

    }
}

现在,我知道,如果客户端未实现我的行为,它将永远不会输入我的ApplyDispatchBehaviour,而且永远不会发生,因为它们是外部提供者,并且我无法访问代码。

此外,我的第一个错误甚至都没有离开我的代码,因为我引起了NOT FOUND错误。

我发现了很多类似的问题,但都没有解决方案。我发现有几个站点在ApplyClientBehaviour中添加了以下内容:

IErrorHandler errorHandler = new CustomErrorHandler();
clientRuntime.CallbackDispatchRuntime.ChannelDispatcher.ErrorHandlers.Add(errorHandler);

但这不起作用。

发生在我身上的其他消息来源:https://riptutorial.com/csharp/example/5460/implementing-ierrorhandler-for-wcf-services

这不是解决方案,因为它用于服务行为。我需要在“端点行为”中执行此操作。

谢谢

1 个答案:

答案 0 :(得分:0)

请参考以下示例。
服务器端。

class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("http://localhost:1100");
            BasicHttpBinding binding = new BasicHttpBinding();
            using (ServiceHost sh = new ServiceHost(typeof(MyService), uri))
            {
                ServiceEndpoint se = sh.AddServiceEndpoint(typeof(IService), binding, "");
                ServiceMetadataBehavior smb;
                smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                {
                    smb = new ServiceMetadataBehavior();
                    smb.HttpGetEnabled = true;
                    smb.HttpGetUrl = new Uri("http://localhost:1100/mex");
                    sh.Description.Behaviors.Add(smb);
                }
                MyEndpointBehavior bhv = new MyEndpointBehavior();
                se.EndpointBehaviors.Add(bhv);


                sh.Open();
                Console.WriteLine("service is ready");
                Console.ReadKey();
                sh.Close();
            }
        }
    }
    [ServiceContract(ConfigurationName = "isv")]
    public interface IService
    {
        [OperationContract]
        string Delete(int value);
        [OperationContract]
        void UpdateAll();
    }
    [ServiceBehavior(ConfigurationName = "sv")]
    public class MyService : IService
    {
        public string Delete(int value)
        {
            if (value <= 0)
            {
                throw new ArgumentException("Parameter should be greater than 0");
            }
            return "Hello";
        }

        public void UpdateAll()
        {
            throw new InvalidOperationException("Operation exception");
        }
    }
    public class MyCustomErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            return true;
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            FaultException faultException = new FaultException(error.Message);
            MessageFault messageFault = faultException.CreateMessageFault();
            fault = Message.CreateMessage(version, messageFault, error.Message);
        }
    }
    public class MyEndpointBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            return;
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            MyCustomErrorHandler myCustomErrorHandler = new MyCustomErrorHandler();
            endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(myCustomErrorHandler);
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            return;
        }
}

客户。

static void Main(string[] args)
{
    ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
    try
    {
        client.Delete(-3);
    }
    catch (FaultException fault)
    {
        Console.WriteLine(fault.Reason.GetMatchingTranslation().Text);
    }
}

结果。
enter image description here
随时让我知道是否有什么可以帮助您的。