We have been using ServiceStack for REST based services for a while now and so far it has been amazing.
All of our services have been written as:
public class MyRestService : RestService<RestServiceDto>
{
public override object OnGet(RestServiceDto request)
{
}
}
For each DTO we have Response equivalent object:
public class RestServiceDto
{
public ResponseStatus ResponseStatus {get;set;}
}
which handles all the exceptions should they get thrown.
What I noticed is if an exception is thrown in the OnGet() or OnPost() methods, then the http status description contains the name of the exception class where as if I threw a:
new HttpError(HttpStatus.NotFound, "Some Message");
then the http status description contains the text "Some Message".
Since some of the rest services are throwing exceptions and others are throwing new HttpError(), I was wondering if there was a way without changing all my REST services to catch any exceptions and throw a new HttpError()?
So for example, if the OnGet() method throws an exception, then catch it and throw a new HttpError()?
Using Old API - inherit a custom base class
As you're using the old API to handle exceptions generically you should provide a Custom Base class and override the HandleException method, e.g:
public class MyRestServiceBase<TRequest> : RestService<TRequest>
{
public override object HandleException(TRequest request, Exception ex)
{
...
return new HttpError(..);
}
}
Then to take advantage of the custom Error handling have all your services inherit your class instead, e.g:
public class MyRestService : MyRestServiceBase<RestServiceDto>
{
public override object OnGet(RestServiceDto request)
{
}
}
Using New API - use a ServiceRunner
Otherwise if you're using ServiceStack's improved New API then you don't need to have all services inherit a base class, instead you can just tell ServiceStack to use a custom runner in your AppHost by overriding CreateServiceRunner:
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(
ActionContext actionContext)
{
return new MyServiceRunner<TRequest>(this, actionContext);
}
Where MyServiceRunner is just a just custom class implementing the custom hooks you're interested in, e.g:
public class MyServiceRunner<T> : ServiceRunner<T> {
public override object HandleException(IRequestContext requestContext,
TRequest request, Exception ex) {
// Called whenever an exception is thrown in your Services Action
}
}
Related
I am using Servicestack. I have a base class for my Services, like so:
public abstract class ServiceHandlerBase : Service
and then some methods and properties in there of interest. I already have several methods that accesses the IRequest object, like:
protected AlfaOnline GetContactItem()
{
string deviceUUID = Request.Headers.Get(Constants.DEVICE_UUID); // <-- calling this method from constructor will give NullRef on Request here
string authToken = Request.Headers.Get(Constants.AUTH_TOKEN);
// do stuff
return existingContactItem;
}
which works well inside my service implementations, no problems there.
Now, I wanted to use this exact same method directly from the base class, calling it in the constructor:
public ServiceHandlerBase()
{
AlfaOnline ao = GetContactItem();
}
but I then get a NullReferenceException on the Request object as noted above.
When is the Request object ready to access and use? Because it's not null inside the service implementations.
You can't access any dependencies like IRequest in the constructor before they've been injected, they're only accessible after the Service class has been initialized like when your Service method is called.
You can use a Custom Service Runner to execute custom logic before any Service is Executed, e.g:
public class MyServiceRunner<T> : ServiceRunner<T>
{
public override void OnBeforeExecute(IRequest req, TRequest requestDto) {
// Called just before any Action is executed
}
}
And register it with ServiceStack in your AppHost with:
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext ctx)
{
return new MyServiceRunner<TRequest>(this, ctx);
}
But if you just want to run some logic for a Service class you can now override OnBeforeExecute() in your base class, e.g:
public abstract class ServiceHandlerBase : Service
{
public override void OnBeforeExecute(object requestDto)
{
AlfaOnline ao = GetContactItem();
}
}
See ServiceFilterTests.cs for a working example.
If you're implementing IService instead of inheriting the Service base class you can implement IServiceBeforeFilter instead.
The new Service Filters is available from v5.4.1 that's now available on MyGet.
I'd like to enforce authentication on some auto querys.
[Authenticate]
public class BusinessEntitiesService : QueryDb<DataModel.dbo.BusinessEntity>
{
}
Here's my issue. The above class is in my ServiceModel project... in order to add the [Authenticate] attribute, I need to add a reference to ServiceStack.dll which I think can cause issues down the road (according to previous guidance to only reference ServiceStack.Interfaces in the ServiceModel). I can't add the above class to ServiceInterfaces because then I'd have to reference that everywhere I use the client.
I've also tried using a GlobalRequestFilter... but that appears to goof with the AdminFeature plugin:
private bool IsAProtectedPath(string path)
{
return !path.StartsWith("/auth") && !path.StartsWith("/autoquery");
}
GlobalRequestFilters.Add((httpReq, httpResp, requestDto) =>
{
if(IsAProtectedPath(httpReq.PathInfo))
new AuthenticateAttribute().Execute(httpReq, httpResp, requestDto);
});
Not really sure how to best handle this.
In order to apply the [Authenticate] attribute to AutoQuery Services you would need to create a custom AutoQuery implementation and apply your Filter attributes on that, e.g:
[Authenticate]
public class MyProtectedAutoQueryServices : Service
{
public IAutoQueryDb AutoQuery { get; set; }
public object Any(QueryBusinessEntity query) =>
AutoQuery.Execute(query, AutoQuery.CreateQuery(query, Request));
public object Any(QueryBusinessEntity2 query) =>
AutoQuery.Execute(query, AutoQuery.CreateQuery(query, Request));
}
An alternative is to dynamically add attributes to your AutoQuery Request DTO, but these would need to be registered before Configure() is called, either before appHost.Init() or in your AppHost constructor, e.g:
public class AppHost : AppHostBase
{
public AppHost()
{
typeof(QueryBusinessEntity)
.AddAttributes(new AuthenticateAttribute());
}
}
Given below are my different class declarations and how I am trying to setup unity container configuration to get a Interface to Concrete class implementation. The code currently throws either an stackoverflow exception or suggests that a interface cannot be constructed.
Please help me fix, either the class structure or the container configuration.
CodesController Class -
public class CodesController : ApiController
{
private readonly IUnitOfWorkAsync unitOfWork;
private readonly ICodeRepository repository;
public CodesController(IUnitOfWorkAsync unitOfWork, ICodeRepository codeRepository)
{
if (unitOfWork == null)
{
throw new ArgumentNullException("unitOfWork");
}
this.unitOfWork = unitOfWork;
this.repository = codeRepository;
}
//Other class level methods here
}
CodeRepository class -
public class CodeRepository : ICodeRepository
{
private readonly ICodeRepository codeRepository;
public CodeRepository(ICodeRepository repository)
{
this.codeRepository = repository;
}
public virtual async Task<IEnumerable<Code>> GetCodeAsync(string codeKey)
{ //Some implementation here}
}
ICodeRepository Interface -
public interface ICodeRepository : IRepositoryAsync<Code>
{
Task<IEnumerable<Code>> GetCodeAsync(string codeKey);
}
IRepositoryAsync Interface -
public interface IRepositoryAsync<TEntity> : IRepository<TEntity> where TEntity : class, IPersistenceHint
{
Task<bool> DeleteAsync(params object[] keyValues);
Task<bool> DeleteAsync(CancellationToken cancellationToken, params object[] keyValues);
Task<TEntity> FindAsync(params object[] keyValues);
Task<TEntity> FindAsync(CancellationToken cancellationToken, params object[] keyValues);
}
Unity Container Configuration-
container.RegisterType<IUnitOfWorkAsync, UnitOfWork>(
"test",
new TransientLifetimeManager(),
new InjectionConstructor(container.Resolve<IDataContextAsync>("test")));
container.RegisterType<ICodeRepository, CodeRepository>();
container.RegisterType<CodesController, CodesController>();
With this given configuration and class structure, based on my experimentation with container config, I get following exception -
JSON
exceptionMessage=An error occurred when trying to create a controller of type 'CodesController'. Make sure that the controller has a parameterless public constructor.
exceptionType=System.InvalidOperationException
innerException
exceptionMessage=Type '<Namespace>.Api.Controllers.CodesController' does not have a default constructor
stackTrace= at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
Please suggest, if anything is wrong here, so that I can fix the same. Already struggling many days on this.
You're injecting ICodeRepository to CodeRepository, which probably causes to stackoverflow exception, since it will keep generating ICodeRepositories. It will generate a recursive call. Somewhat like this one:
public class BaseFoo
{
public BaseFoo(BaseFoo foo){ }
}
public class Foo : BaseFoo
{
public Foo() : base(new Foo()) { }
}
And regarding the "does not have a default constructor"-exception, have you registered a DependencyResolver for Web API? See one of these questions for more detailed information how to do it:
Using Unity with Web Api 2 gives error does not have a default constructor
Unity.WebApi | Make sure that the controller has a parameterless public constructor
ASP.Net MVC 4 Web API controller dosn't work with Unity.WebApi
As a side note, you shouldn't have to register the CodesController in your unity registration.
My implemention of the JAX-RS Filters as as follows,
The request filter is :
public class AuthorizationRequestFilter implements ContainerRequestFilter {
public static long entryTime;
#Override
public void filter(ContainerRequestContext requestContext)
throws IOException {
/*some preprocessing before unmarshalling*/
}
}
the Response filter is:-
public class ResponseFilter implements ContainerResponseFilter {
#Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
if (!requestContext.getMethod().equalsIgnoreCase("Head")) {
/*Some processing after Marshalling*/
}
}
my handler is :-
#POST
#Path("abc")
#Produces(MediaType.APPLICATION_XML)
public Response createABC(App app){
/*lines of code*/
return Response.status(Status.CRETED).entity(abc).build();
}
My question is are the filters called after Marshallling and Unmarshalling,i.e is the app object created after the method AuthorizationRequestFilter and abc is Marshalled before calling of Response Filter
The request filter is called before unmarshalling. This should be apparent, as you still have access to the entity input stream (meaning it hasn't been read from yet). You could easily test this by writing a trivial MessageBodyReader. Set an arbitrary header in the request filter, then you will be able to extract the same header from the readers readFrom method.
The response filter will be called before the marshalling. Again this could easily be tested by writing a trivial MessageBodyWriter. Set an arbitrary header in the filter, and you should be able to access it in the writer's writeTo method.
If you want to perform some operation after marshalling, you instead could use a WriterInterceptor, which wraps the call the writer's marshalling method. For example
#Provider
public class SimpleIntercetor implements WriterInterceptor{
#Override
public void aroundWriteTo(WriterInterceptorContext context)
throws IOException, WebApplicationException {
// Processing before marshalling
context.proceed(); // marshal
// Processing after marshalling
}
}
Some Resources:
How to Write Custom Entity Providers
Interceptors
The links are to Jersey documentation, but interceptors, readers, writers are standard to JAX-RS 2.0, so the documentation should be applicable to whatever implementation you're using (for the most part :-)
We are currently using ServiceStack as our core framework for service provisioning.
Does anyone know if it's possible to wrap custom exceptions into custom ErrorResponse objects?
From SS wiki:
"In addition to the above options, you can override the serialization of ad-hoc exceptions by implementing the IResponseStatusConvertible.ToResponseStatus() method and have it return your own populated ResponseStatus instance instead."
That seems to fit with my needs, but I cannot figure out where I can override this serialization.
Again, I've tried to use custom httphandler by registering them within the AppHost, but they are not invoked when exceptions occur.
I am certainly missing something, is there anyone who can guide me through this?
Here is ServiceStack's Error Handling wiki page.
What it's saying is that you can control how the ResponseStatus is serialized if your custom exceptions implement IResponseStatusConvertible. Here is the source code example of ValidationException implementing it:
public class ValidationException : ArgumentException, IResponseStatusConvertible
{
public IEnumerable<ValidationFailure> Errors { get; private set; }
public ValidationException(IEnumerable<ValidationFailure> errors) : base(BuildErrorMesage(errors)) {
Errors = errors;
}
private static string BuildErrorMesage(IEnumerable<ValidationFailure> errors) {
var arr = errors.Select(x => "\r\n -- " + x.ErrorMessage).ToArray();
return "Validation failed: " + string.Join("", arr);
}
public ResponseStatus ToResponseStatus()
{
var errors = Errors.ConvertAll(x =>
new ValidationErrorField(x.ErrorCode, x.PropertyName, x.ErrorMessage));
var responseStatus = ResponseStatusUtils.CreateResponseStatus(typeof(ValidationException).Name, Message, errors);
return responseStatus;
}
}
But this only controls how ResponseStatus is serialized, not how the generic responses are created. Look at the description of IAppHost.ServiceExceptionHandler or use a custom service runner if you want to change the error response returned.