Reputation: 1170
We have successfully implemented the ServiceStack IService<> interface and have it communicating with an iPhone but we are unsure of the best way to implement our exception handling and logging.
public class AddCarService : IService<AddCar>
{
public UnitOfWork UnitOfWork { get; set; }
public object Execute(AddCar request)
{
try
{
PersonBo person;
if (UnitOfWork.PersonService.ValidateSession(request.SessionGuid, out person))
{
var car = new CarBo
{
PersonId = person.PersonId,
Name = request.Car.Name,
RegistrationNumber = request.Car.RegistrationNumber,
IsActive = true,
IsDeleted = false
};
UnitOfWork.CarService.Add(car);
var cars = UnitOfWork.CarService.GetCarByPersonId(person.PersonId);
var mobileCars = cars.Select(carBo => new Car { CarId = carBo.CarId, Name = carBo.Name, RegistrationNumber = carBo.RegistrationNumber }).ToList();
return new GetCarResponse { Cars = mobileCars, Status = ResponseStatus.Ok };
}
return new GetCarResponse { Cars = null, Status = ResponseStatus.Unauthorised };
}
catch (Exception ex)
{
UnitOfWork.Logging.Error("AddCarService", ex);
return new GetCarResponse { Cars = null, Status = ResponseStatus.Exception };
}
}
}
Adding a try/catch has quite a performance penalty so we wondered if there was a function build into the ServiceStack framework to support what we are trying to achieve and still return a well formed response to the iPhone.
Upvotes: 1
Views: 248
Reputation: 143409
If you're using ServiceStack's New API you can generically handle exceptions by implementing your own ServiceRunner. With the old API you need to override HandleException()
in a custom base class that inherits from ServiceBase<T>
or RestServiceBase<T>
.
See this previous answer for more details.
Upvotes: 1
Reputation: 18132
If you inherit from ServiceBase<T>
instead of implementing IService<T>
you get this for free (returning a well formed response), but if you take a look at the ServiceBase<T>
class, you will see that this is done by wrapping the code in a try-catch block.
I don't think you will get much more than this from ServiceStack.
Upvotes: 1