Hadler Error EndPoint in my host - WCF - Behaviour

[亡魂溺海] 提交于 2019-12-04 07:00:31

问题


A few days ago I opened a question if I succeed with the answers. I had not focused the question well, and now with something more knowledge I ask again.

I need to capture the errors of all my endpoints to have them included in the same site. The idea is to add a behavior to these endpoints.

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)
            {

            }
        }

    }
}

Now I know that it will never enter my ApplyDispatchBehaviour if the client does not implement my behaviour, and this will NEVER happen, since they are external providers and I do not have access to the code.

Also, my first error does not even leave my code, since I'm causing a NOT FOUND error.

I have found many similar sources with my problem without solution. I have found by several sites to add the following in ApplyClientBehaviour:

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

But this does not work.

Other sources that happened to me: https://riptutorial.com/csharp/example/5460/implementing-ierrorhandler-for-wcf-services

It is NOT a solution, since it is for Services Behavior. I need to do it in EndPoint Behavior.

Thank you


回答1:


Please refer to the following example.
Server side.

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;
        }
}

Client.

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

Result.

Feel free to let me know if there is anything I can help with.



来源:https://stackoverflow.com/questions/55512991/hadler-error-endpoint-in-my-host-wcf-behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!