tags:

views:

520

answers:

2

When working with WCF many times the exception massage doesn’t help us to solve the problem. The above massage is usually a symptom for one of the following problems:

  1. The return values are bigger than the value which was defined in the config file.
  2. There is a problem with the endpoint setting
  3. There is a problem with serialization of the data

I experienced the 3rt problem which was with enums The problem was that the an enum was defined explicitly with values

  Public Enum FrequencyEnums
        EveryTime = 1
        OncePerHour = 2
        OncePerDay = 3
        OncePerWeek = 4
        Never = 5
    End Enum

And the private property that was using this enum was defined as follows

Private m_sendFrequencyID As FrequencyEnums

Now because the enum does not have a definition of a default value and because the property is not initialized explicitly and because the enum value for 0 is missing from the enum and because the default value of enum regardless of the specified options is allwayes 0 When I tried to return instance of this class to the client I got this error: The underlying connection was closed: The connection was closed unexpectedly

The solution is one of the following:

  1. Define 0 value for enums or
  2. Define a default value for a property from the enum values.
  3. Assign initial value to the property

My question is how could I have found this error with Microsoft tools and not by trial and error.

+1  A: 

You need to create a data contract for the enum. See: http://consultingblogs.emc.com/merrickchaffer/archive/2007/04/03/Passing-Enum-values-into-WCF-Service-operations.aspx

Shiraz Bhaiji
The question is how to determine the problem, not how to solve it. The difficulties are to find out it from this exception: "The underlying connection was closed: The connection was closed unexpectedly", which doesn't say much.
Kamarey
Yes, that is one of the problems with WCF, often the error is very general or misleading. Which means that you cannot go from the error message to what the problem is. You need to look at what you are doing as much as what the error message is.
Shiraz Bhaiji
WCF specifically and by design uses "error masking", e.g. it by design doesn't reveal too much information about the error. Let's face it - most of the time, that info doesn't really help the end user anyway.....
marc_s
A: 

Refer to http://developergeeks.com/article/12/wcf-error-the-underlying-connection-was-closed for more info on this error.