简体   繁体   English

ServiceStack IService<> 错误处理

[英]ServiceStack IService<> Error Handing

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.我们已经成功实现了 ServiceStack IService<> 接口并让它与 iPhone 通信,但我们不确定实现异常处理和日志记录的最佳方式。

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.添加 try/catch 会造成相当大的性能损失,因此我们想知道 ServiceStack 框架中是否内置了一个函数来支持我们尝试实现的目标,并且仍然向 iPhone 返回格式良好的响应。

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.如果您从ServiceBase<T>继承而不是实现IService<T>您可以免费获得它(返回格式良好的响应),但是如果您查看ServiceBase<T>,您将看到这是由将代码包装在 try-catch 块中。

I don't think you will get much more than this from ServiceStack.我认为您不会从 ServiceStack 获得比这更多的东西。

If you're using ServiceStack's New API you can generically handle exceptions by implementing your own ServiceRunner.如果您正在使用ServiceStack 的新 API,您可以通过实现自己的 ServiceRunner 来处理异常。 With the old API you need to override HandleException() in a custom base class that inherits from ServiceBase<T> or RestServiceBase<T> .使用旧 API,您需要在继承自ServiceBase<T>RestServiceBase<T>的自定义基类中覆盖HandleException()

See this previous answer for more details .有关更多详细信息,请参阅此先前的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM