Reputation: 73
I'm using wcf Service to insert data to DB and my service crashes saying exception un-handled. I'm trying to pass on exception from one WCF service method to other WCF method and from there throwing exception to client.
Here is My code:
Data Inserting Method of WCF Service: method to insert data to DB
public int insertStatements(SqlCommand cmd)
{
Try
{
//insert data to the db
}
catch(SqlException ex)
{
if (ex.Number == 2627) // if unique key constraint error
{
throw new FaultException( "error reason", new FaultCode("Error Code: ");
}
else
{
throw new FaultException("DB error: ", new FaultCode("Error Code: " +);
}
}
catch (FaultException ex)
{
throw new FaultException("Unknown Error",new FaultCode("Unknown Error"));
}
}
WCF Insert location method, which is service exposed method(Public)
public int insertLocation (string name)
{
try
{
// this method communicates with client
dataconnection.insertStatements(cmd);
}
catch
{
throw; // Here i'm getting error
}
}
in my client: winform Application
try
{
AreaDataServicesClient DataObject = new AreaDataServicesClient("BasicHttpBinding_IAreaDataServices");
int rowsAffected = DataObject.InsertProvince(ProvinceObject.AreaName, ProvinceObject.AreaKm);
return rowsAffected;
}
catch(FaultException ex)
{
messagebox.show("erro occured");
}
This is the Error i get: "An exception of type 'System.ServiceModel.FaultException' occurred in EMSDataServices.dll but was not handled in user code"
Why service method does not pass exception to the client.
Upvotes: 0
Views: 290
Reputation: 29282
The line marked ///Here i'm getting the error
is a throw
statement. That's exactly what it's supposed to do. It's taking the exception which has been caught (because it's in a try
) and making it unhandled again, as if it had never been caught.
That's what you might do if you wanted to log or inspect the exception and then let it bubble up. In this case it's exactly the same as removing the entire try/catch.
This:
public int insertLocation (string name)
{
try
{
dataconnection.insertStatements(cmd);
}
catch
{
throw; // Here i'm getting error
}
}
is the same as this:
public int insertLocation (string name)
{
dataconnection.insertStatements(cmd);
}
Upvotes: 0
Reputation: 73
i found it. There was nothing wrong with the code. i was running the services on debug mode and in exception setting 'break at this point if user code is unhandled' was checked,so it was stopping the service to perform further action and client was crashing due to no response from service.
i unchecked it and it works as expected.
Upvotes: 1
Reputation: 241
@Mohsin, catch (FaultException ex) in insertStatements is useless. It will never catch your exception. because you are throwing the exception outside the method. You should handle the exception somewhere which is re-thrown. That you can do it in the insertLocation method. See the bellow modified code.
public int insertLocation(string name)
{
try
{
dataconnection.insertStatements(cmd);
}
catch (FaultException ex)
{
// you should handle the exception here. Do not retrow here too. Otherwise you may need to handle somewhere else again.
}
}
public int insertStatements(SqlCommand cmd)
{
try
{
//insert data to the db
}
catch (SqlException ex)
{
if (ex.Number == 2627) // if unique key constraint error
{
throw new FaultException("error reason", new FaultCode("Error Code: ");
}
else
{
throw new FaultException("DB error: ", new FaultCode("Error Code: ");
}
}
}
Upvotes: 0