简体   繁体   中英

JsonServiceClient ResponseFilter doesn't trigger

Maybe I'm missing something simple, but I can't for the life of me get the ResponseFilter to trigger on a JsonServiceClient in ServiceStack. The RequestFilter triggers every time (I'm sending an API token to my API). Here is a sample console app that shows the ResponseFilter never writing to the console.

static void Main(string[] args)
{
    using (var client = new JsonServiceClient())
    {
        client.ResponseFilter = httpResponse =>
        {
            Console.WriteLine("ResponseFilter: StatusCode == " + httpResponse.StatusCode);
        };

        try
        {
            System.Net.HttpWebResponse response = client.Get("https://app.asana.com/api/1.0/users/me");
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception => " + e.Message);
        }
    }
    Console.ReadLine();
}

I'm using the latest ServiceStack v4.0.40.0

The example code you have provided, in isolation, looks correct apart from the client being disposed in the using statement.

Once the client is created, an action can be registered on the ResponseFilter as you have shown above.

I've created an example project on GitHub in empty ServiceStack ASP.NET solution with a unit test that shows this filter working.

The main part of the code is very similar to the snippet you provided.

bool calledRepsonseFilter = false;
var jsonServiceClient = new JsonServiceClient("http://techstacks.io/");
jsonServiceClient.ResponseFilter = response =>
{
    Console.WriteLine("A response!");
    calledRepsonseFilter = true;
};
var res = jsonServiceClient.Get(new AppOverview());
Assert.That(res.TopTechnologies != null);
Assert.That(calledRepsonseFilter == true);

I've given the same answer on the ServiceStack forums, but updated this question with the answer in case it helps others as well.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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