Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throwing exception from WCF



I am trying to throw an exception from WCF module to the client module. I am getting the following error "FaultException was unhandled by the user"

at the service side

public IList<UserEntity> SearchUserDetail(string userName)
{
    try
    {
        int y = 0;
        int u = 9 / y;
        return new UserViewModel().SearchUserDetail(userName);
    }
    catch (Exception ex)
    {
        throw new FaultException(
        new FaultReason(ex.Message),new FaultCode("Data Access Error"));
    }                 
}

at the client side

try
{             
    ServiceReference.ServiceClient servRef = new ServiceReference.ServiceClient();

    List<UserEntity> users = new List<UserEntity>();
    users.AddRange(servRef.SearchUserDetail("Jacson"));
    dataGridView1.DataSource = users;            
}
catch (System.ServiceModel.FaultException  exc)
{
    Logging.Log(new Exception("Search User", exc));
}

In the app.config of the Service module I have added the following attribute

 serviceDebug includeExceptionDetailInFaults="True" 

Anyone know a solution?

like image 224
Kapil Yadav Avatar asked Feb 24 '23 19:02

Kapil Yadav


2 Answers

You have to add the FaultContractAttribute to the method SearchUserDetail, if it has the OperationContractAttribute and is part of your ServiceContract class.

[ServiceContract]
interface IMyServiceContract
{
  [OperationContract]
  [FaultContract(typeof(...)]
  IList<UserEntity> SearchUserDetail(string userName)
}

I'm not sure about the type of the FaultContract. But have a look at msdn.

like image 192
Matthias Avatar answered Feb 27 '23 08:02

Matthias


Shameless plug:

  • First part of the explanation
  • Second part
like image 30
Johann Blais Avatar answered Feb 27 '23 08:02

Johann Blais