我們已經成功實現了ServiceStack IService接口,並讓它與iPhone通信,但我們不確定實現異常處理和日誌記錄的最佳方式。ServiceStack IService <>錯誤處理
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 };
}
}
}
添加一個try/catch有相當多的性能損失,所以我們想知道是否有一個功能構建到ServiceStack框架來支持我們正在努力實現的,仍然返回到iPhone形成的阱響應。
另請參閱此前有關如何一般[捕獲和處理ServiceStack中的異常] [1]的答案。 [1]:http://stackoverflow.com/questions/13143502/catching-exceptions-with-servicestack – mythz