简体   繁体   English

JsonServiceClient ResponseFilter不触发

[英]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. 也许我缺少一些简单的东西,但是我终生JsonServiceClient在ServiceStack的JsonServiceClient上让ResponseFilter触发。 The RequestFilter triggers every time (I'm sending an API token to my API). 每次都会触发RequestFilter (我正在向我的API发送一个API令牌)。 Here is a sample console app that shows the ResponseFilter never writing to the console. 这是一个示例控制台应用程序,该示例显示ResponseFilter从未写入控制台。

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 我正在使用最新的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. 孤立地提供的示例代码,除了在using语句中放置的客户端之外,看上去都是正确的。

Once the client is created, an action can be registered on the ResponseFilter as you have shown above. 一旦创建了客户端,就可以在ResponseFilter上注册一个动作,如上所示。

I've created an example project on GitHub in empty ServiceStack ASP.NET solution with a unit test that shows this filter working. 我已经在空的ServiceStack ASP.NET解决方案中的GitHub上创建了一个示例项目,其中的单元测试显示了此过滤器的工作原理。

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. 我在ServiceStack论坛上给出了相同的答案,但是以答案为基础更新了这个问题,以防其他人也对它有所帮助。

Hope that helps. 希望能有所帮助。

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

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