locked
CommunicationException: The underlying connection was closed: The connection closed unexpectedly RRS feed

  • Question

  • Hello - I'm trying to mock up an example of a FaultException<ArgumentNullException>. The following code works:

    if(string.IsNullOrEmpty(id))
    {
    string message = "id parameter must have value";
    var ex = new ArgumentNullException(message);
    throw new FaultException<ArgumentNullException>(ex, new FaultReason(message));
    }

    The next step is I try to pass in an inner exception to the ArgumentNullException to test that I'm able to pass an actual exception stack trace as an inner exception to the client:

    if(string.IsNullOrEmpty(id))
    {
    string message = "id parameter must have value";
    var innerEx = new System.Exception("inner exception stack trace here");
    var ex = new ArgumentNullException(message, innerEx);
    throw new FaultException<ArgumentNullException>(ex, new FaultReason(message));
    }

    However, when I include an inner exception in my ArgumentNullException, the client encounters a CommunicationException when calling the service before the service returns the FaultException<ArgumentNullException>. The exception encountered is:

    CommunicationException: The underlying connection was closed: The connection closed unexpectedly.

    Am I doing something incorrectly here? Is it not possible to pass inner exceptions as part of FaultException types in WCF?
    Thursday, August 16, 2012 2:30 AM

Answers

  • Hi,

    It seems to be that the Exception cannot be serialized.

    As I debug your code and I've found that the ArgumentNullException(without InnerException) only send the string to the client-side, not the ArgumentNullException itself.

    I suggest you to create a class by your own:

    http://msdn.microsoft.com/en-us/library/ms752208.aspx

    • Marked as answer by Otomii Lu Friday, August 24, 2012 6:34 AM
    Friday, August 17, 2012 7:18 AM

All replies

  • Hi,

    Have you added the inner exception as a FaultContract?


    Please "Mark as Answer" if a post has answered your question or "Vote as Helpful" if it was helpful in some way. Here's why

    Thursday, August 16, 2012 3:30 AM
  • Hi,

    Have you added the inner exception as a FaultContract?


    Please "Mark as Answer" if a post has answered your question or "Vote as Helpful" if it was helpful in some way. Here's why

    I don't think so.  Can you show me how I would do that?
    Thursday, August 16, 2012 4:10 AM
  • Hi,

    In your service contract, go to the operation contract and add FaultContract attributes for all the types that you expect the client to encounter in the Fault section of the response sent back from the service.

    For example, in your case it would be something like -

    [OperationContract]
    [FaultContract(typeof(ArgumentException))]
    [FaultContract(typeof(Exception))]
    void SomeMethod();
    

    Regards,

    Nauzad Kapadia


    Please "Mark as Answer" if a post has answered your question or "Vote as Helpful" if it was helpful in some way. Here's why

    Thursday, August 16, 2012 4:26 AM
  • @Nauzad - I tried adding the additional attribute per your example above:

    [FaultContract(typeof(Exception))]

    However, the service still encounters the CommunicationException error....

    Thursday, August 16, 2012 4:36 AM
  • Hi,

    It seems to be that the Exception cannot be serialized.

    As I debug your code and I've found that the ArgumentNullException(without InnerException) only send the string to the client-side, not the ArgumentNullException itself.

    I suggest you to create a class by your own:

    http://msdn.microsoft.com/en-us/library/ms752208.aspx

    • Marked as answer by Otomii Lu Friday, August 24, 2012 6:34 AM
    Friday, August 17, 2012 7:18 AM