none
Throw FaultException From MessageInspector at AfterReceiveRequest Event RRS feed

  • Question

  • I have one service which have IContractBehavior attached. In MessageInspector at the AfterReceiveRequest event I am checking a custom header in request and if it is not present then throwing an exception.  Then Exception is caught by IErrorHandler at ProvideFault Method .ProvideFault  method  again call one of the method of same service with required custom header to log exception in database.

    The problem is  that second call from ProvideFault  method  is never reached to MessageInspector at the AfterReceiveRequest  and service stops executing until Timeout exception raised at the client.

    I have also enabled tracing but no clue why it is happening.

    Any thoughts ?

    • Edited by Akshukla89 Saturday, May 5, 2018 1:20 PM
    Saturday, May 5, 2018 1:17 PM

Answers

  • Hi Akshukla89,

    I suggest you try to check whether below code meet your requirement.

    ErrorHandler

        public class ErrorHandler : IErrorHandler
        {
            public bool HandleError(Exception error)
            {
                return false;
            }
    
            public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
            {
                Task.Factory.StartNew(()=> {
                    var compositeType = new ServiceClient().GetCompositeData(new CompositeType { BoolValue = true, StringValue = "random" });
                });
            }
        }
    

    AfterReceiveRequest

            public virtual object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                //some condition is false then throw exception
    
                if (request.Headers.FindHeader("NS1","NS1")==-1)
                {
                    throw new FaultException("Auth failed on service");
                }
    
                return null;
            }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Akshukla89 Friday, May 11, 2018 1:04 PM
    Thursday, May 10, 2018 6:17 AM

All replies

  • Hi Akshukla,

    Could you share us a simple project which could reproduce your issue?

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 7, 2018 2:22 AM
  • Hi 

    Please download sample project from below given link.

    Sample project  (https://1drv.ms/u/s!AlrPNHoqWM8GvSaTPWcLcapRbqxD)

    Monday, May 7, 2018 12:34 PM
  • Due to security policy, I could not access the download address.

    Could you share us through OneDrive?


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 8, 2018 2:53 AM
  • Hi

    provided link was generated by  onedrive itself.

    Try with below link

    https://onedrive.live.com/?authkey=%21AJM9ZwtxqlFurEM&cid=06CF582A7A34CF5A&id=6CF582A7A34CF5A%217846&parId=root&action=locate


    • Edited by Akshukla89 Tuesday, May 8, 2018 4:28 AM
    Tuesday, May 8, 2018 2:58 AM
  • Hi Akshukla,

    Thanks for sharing the project, and I could reproduce your issue.

    What do you want to do with “compositeType” from new Created Client?

    For calling the Service method, I think you could initialize “Service” class direct instead of creating a new client.

            public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
            {
                //var compositeType = new ServiceClient().GetCompositeData(new CompositeType { BoolValue = true, StringValue = "random" });
                var compositeType = new Service().GetDataUsingDataContract(new CompositeType { BoolValue = true, StringValue = "random" });
            }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 9, 2018 2:45 AM
  •  Hi Tao Zhou,
    I can't call service class directly.
    Because in actual project Provide fault use a generic logger  which call this service internally.
    This sample project is just to replicate the scenario.

    I think time out is occurring because Message inspector is synchronous and second call  again go to message inspector which is still processing first call .

    Is there anyway to make message inspector process multiple request simultaneously without waiting inprogress call?
    Wednesday, May 9, 2018 2:59 AM
  • Hi Akshukla89,

    I suggest you try to check whether below code meet your requirement.

    ErrorHandler

        public class ErrorHandler : IErrorHandler
        {
            public bool HandleError(Exception error)
            {
                return false;
            }
    
            public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
            {
                Task.Factory.StartNew(()=> {
                    var compositeType = new ServiceClient().GetCompositeData(new CompositeType { BoolValue = true, StringValue = "random" });
                });
            }
        }
    

    AfterReceiveRequest

            public virtual object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
            {
                //some condition is false then throw exception
    
                if (request.Headers.FindHeader("NS1","NS1")==-1)
                {
                    throw new FaultException("Auth failed on service");
                }
    
                return null;
            }
    

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Akshukla89 Friday, May 11, 2018 1:04 PM
    Thursday, May 10, 2018 6:17 AM
  • Hi Tao Zhou,

    Thanks for your help. However same thing i.e calling the logger in new thread, I have already tried and it works.

    But in actual project logger return log-Id after logging the exception and that log-Id is sent back to client in fault message.

    And to get log-Id from thread  ProvideFault method must wait for the thread to complete, which will cause the same time-out issue.

    Any thoughts, How I can achieve this functionality  ?

    Friday, May 11, 2018 6:56 AM
  • Hi Akshukla,

    For further requirement, I would suggest you post a new thread, and then we could focus on this new requirement.

    For current thread, I would suggest you mark the helpful reply as asnwer to close current thread.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 11, 2018 7:00 AM