Huma Ali
Huma Ali

Reputation: 1809

Exception Handling in WCF's Completed Event

I am wondering how to catch Exception in my WCF's completed event method and show it to the User in the MessageBox.

I call the GetProductTypeAsync(); method that fetches records from the Database. I want to catch any Exception that occurs here and send it to the service_GetProductTypeCompleted Event where it should show the Exception Message to the User.

public List<ProductType> GetProductType()
{
    List<ProductType> productType = new List<ProductType>();
    try
    {
        using (SqlConnection con = new SqlConnection(_connectionString))
        {
            SqlCommand cmd = new SqlCommand("usp_Get_ProductType", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    ProductType pType = new ProductType(Convert.ToInt32(reader["pkProductTypeID"]), reader["Name"].ToString());
                    productType.Add(pType);
                }
            }
        }
    }
    catch(Exception ex)
    {
        //Catch Exception and send to the service_GetProductTypeCompleted Event Method
    }
    return productType;
} 

Here is the service_GetProductTypeCompleted Event

void service_GetProductTypeCompleted(object sender, GetProductTypeCompletedEventArgs e)
{
    if (e.Result.Count != 0)
    {
        productTypes = e.Result.ToList();
        cboProductType.DataContext = productTypes;
    }
}

Upvotes: 0

Views: 103

Answers (2)

M. Tovbin
M. Tovbin

Reputation: 547

The FaultException does not flow to Silverlight. You will need to return a class similar to the following from your service layer:

public class ServiceReturnInformation<T>
{
    public T DataContext { get; set; }

    private IList<string> _warnings;
    public IList<string> Warnings
    {
        get { return _warnings ?? (_warnings = new List<string>()); }
        set { _warnings = value; }
    }

    private IList<string> _errors;
    public IList<string> Errors
    {
        get { return _errors ?? (_errors = new List<string>()); }
        set { _errors = value; }
    }
}  

If an exception occurs, your service needs to catch the exception and set the Errors/Warnings properties.

    var result = new ServiceReturnInformation<Employee>();
    try
    {
        // Do something
        result.DataContext = GetEmployee();
    }
    catch (Exception ex)
    {
        result.DataContext = null;
        result.Errors.Add(ex.Message);
    }

    return result;

Upvotes: 0

ANewGuyInTown
ANewGuyInTown

Reputation: 6437

When you send an exception from your service, the client would receive generic error message:

The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs."

If you want to catch the specific exception on the client side, then you should throw what is called FaultException from the service. These are Soap Faults and can be recognized by any consumers. Please note that your consumers for your service is not always .NET consumer, so throwing CLR exception wouldn't be recognized. That's the reason why you need to throw Soap exception.

E.g. this is how you would catch and throw Fault Exception

catch (FaultException<AnyTypeThatCanBeSerialized> ex)
        {
            throw;
        }

Read this article for more details on wcf Faults

Upvotes: 1

Related Questions