Try to get required roles and permissions in exception handler

I have added a custom ServiceExceptionHandler to display a bit more information on errors like 'ServiceStack.HttpError: Invalid Role'. Support staff needs more details:

  1. User and it’s roles and permissions
  2. Required roles and permissions of the called service

So 1. I have solved.

But I am stuck with 2. I tried to use Reflection to find CustomAttributes (something like System.Reflection.MemberInfo.GetCustomAttributes(true);) But this is terrible acrobatics and probably terribly slow too. Is there no smarter way to get this information? I mean it would be very helpful to be able to programmatically query required permissions and roles of every service, but I could not find anything like that.

You can access ServiceStack metadata about each operation from the Metadata property, e.g. you can get required roles for a Service with:

ServiceExceptionHandlers.Add((req, dto, ex) => {
    var requestType = dto.GetType();
    var op = Metadata.GetOperation(requestType);
    //op.RequiredRoles
    return null;
});

Works great, exactly what I was looking for! Thanks a lot.

1 Like