none
ClientMessageInspector : AfterReceiveReply event not fired when httpStatusCode<>"OK" RRS feed

  • Question

  • Hi,

    I have a ClientMessageInspector associated with a client calling a REST API. I need to read the content of the httpResponse property when the response message is received by the client and apply some rules.

    When the calling REST API returns a httpStatusCode="OK", the AfterReceiveReply is triggered and I can do what I need.

    But if the REST API returns a http error 404 for example, the AfterReceiveReply event is not trigerred and I'm not able to read the httpResponse property of the response message.

    How can I do that?

    Thanks.

    Tuesday, March 28, 2017 4:19 PM

All replies

  • >>I have a ClientMessageInspector associated with a client calling a REST API

    How did you implement client to call Rest API? In my option, Rest API is called by HTTP request, in general, we do not use generated client code to request Rest API.

    Based on Message Inspectors, the transformation of fault exceptions thrown by the service occurs before the message inspector is called. It seems you need to catche the known ReplyValidationFault exception and replaces the reply message with an explicit fault message. This method ensures that no invalid messages are returned by the service implementation.

    I suggest you try to implement your requirement WF_WCF_Samples\WCF\Extensibility\MessageInspectors which is provided in Message Inspectors to check whether this issue still exist.

    It would be helpful if you could share us a simple demo which could reproduce your issue.


    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, March 29, 2017 5:21 AM
  • Hi,

    The caller of the API is a BizTalk send port. So the only option I have, because I use the webHttp adapter provided by BizTalk is to add a EndPointBehaviour. It is why I created a MessageInspector.

    You explained in your reply that I need to catch the ReplyValidationFault but I don't know where I can catch this exception. In case of an error, the AfterReceiveReply is not triggered so I can't catch the Exception...

    Wednesday, March 29, 2017 6:08 AM
  • >>The caller of the API is a BizTalk send port.

    I am not familiar with BizTalk, how did call Rest Service from a client?

    To check whether it is related with BizTalk, I suggest you create a winform which will use MessageInspector to call Rest API.

    >>I have a ClientMessageInspector associated with a client calling a REST API.

    To check whether it is related with your Rest API, I suggest you create a simple Rest Service, and a winform client with MessageInsepctor to consume Rest Service. If this issue could be reproduced, it would be helpful if you could share us your complete reproduced project.


    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.

    Thursday, March 30, 2017 2:59 AM
  • Hi,

    I tested with a simple .Net client and I have the exact same situation.

    When the API returned a 404 error, I received a EndpointNotFoundException with a InnerException of type WebException. It means that it is not very convenient to distinguish a real 404 error (for example a "resource does not exists" after a GET) and a situation where the endpoint is not listening on the other side...

    What is sure is that the ClientMessageInspector AfterReceiveReply method is never triggered when the HTTP code is not 200.

    <style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.5px Helvetica; background-color: #000000} </style>
    Thursday, March 30, 2017 7:55 PM
  • >>When the API returned a 404 error, I received a EndpointNotFoundException with a InnerException of type WebException

    Oh, I assume I have known your issue now.

    This result is expected to ClientMessageInspector AfterReceiveReply. AfterReceiveReply works in the channel between client and service which means the connection must be established between client and service.

    For a HTTP 404 error, client could not open a connection to service side. Service side did not receive anything from client side, in other words, the service would not response anything to client side. This is an error which is thrown by client instead of service side.

    If you check IClientMessageInspector.BeforeSendRequest, you will find it has been hit, because there is a request be sent from client.

    For this scenario, I would suggest you try error handler in client side, and check the exception to read response message.


    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, March 31, 2017 3:00 AM
  • Hi,

    Indeed it is not exactly what you mentionned.

    The API did return a 404 http code, I'm sure because I own the API and it received the request.

    I used a mock of my API to simulate multiple situations and as soon as I have a http code different than 200, the AfterReceiveReply event is not triggered...

    Friday, March 31, 2017 12:32 PM
  • >> The API did return a 404 http code, I'm sure because I own the API and it received the request.

    As far as I know, http 404 means Not Found.

    Did you return a custom error? Is there any error in service side? I am wondering why the service received the request, and it still returns 404 code.

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


    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, April 3, 2017 3:05 AM
  • Hi Edward,

    Yes you are right, 404 means Not Found. And it is exactely what we want: it is a REST API providing a "Get" method to retrieve a particular Order with the order id. When the order exists it returns the Order object but when it does not exists, we have a 404 error.

    And it is exactly what a REST API should do in this particular use case.

    As a reminder, the issue I have is that the webHttpBinding is not able to translate this 404 error into a comprehensive error I can catch in the client code, even with a ClientMessageInspector.

    Monday, April 3, 2017 5:25 AM
  • >> When the order exists it returns the Order object but when it does not exists, we have a 404 error.

    I think you need to return Not Found with HTTP 200 instead of 404. HTTP 404 error is used to indicate the request URL is not exist. For the Order does not exist, Http 200 with Not Found in the message body is much better.


    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, April 4, 2017 5:22 AM