Skip to content

Instantly share code, notes, and snippets.

@rudism
Created May 18, 2015 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudism/c11c2ed21452ddddb6d0 to your computer and use it in GitHub Desktop.
Save rudism/c11c2ed21452ddddb6d0 to your computer and use it in GitHub Desktop.
namespace RequestFilterTest
{
using System;
using Funq;
using ServiceStack;
using ServiceStack.Text;
using ServiceStack.Web;
// simple app host with a global request filter
public class AppHost : AppSelfHostBase
{
public AppHost() : base("RequestFilterTest", typeof (AppHost).Assembly) { }
public override void Configure(Container container)
{
GlobalRequestFilters.Add((req, res, dto) => "GlobalFilter".Print());
}
}
// simple request filter attribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class TestFilterAttribute : RequestFilterAttribute
{
public override void Execute(IRequest req, IResponse res, object requestDto)
{
"AttributeFilter".Print();
}
}
// dtos for two test services
[Route("/test/class", "GET")]
public class ClassAttrRequest : IReturnVoid { }
[Route("/test/method", "GET")]
public class MethodAttrRequest : IReturnVoid { }
// service class with testfilter at class level
[TestFilter(Priority = -100)]
public class ClassAttrService : Service
{
public void Get(ClassAttrRequest request) { }
}
// service class with testfilter at method level
public class MethodFilterService : Service
{
[TestFilter(Priority = -100)]
public void Get(MethodAttrRequest request) { }
}
class Program
{
static void Main(string[] args)
{
new AppHost().Init().Start("http://*:9090/");
"ServiceStack SelfHost listening on port 9090".Print();
var client = new JsonServiceClient(string.Format("http://{0}:9090", Environment.MachineName));
// we expect AttributeFilter to output before GlobalFilter on both tests since Priority is < 0
"\nFilter execution order with class attribute:".Print();
client.Get<IReturnVoid>("/test/class"); // this works as expected
"\nFilter execution order with method attribute:".Print();
client.Get<IReturnVoid>("/test/method"); // this does not
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment