使用ServiceStack MiniProfiler来分析所有服务客户端调用

时间:2013-05-23 07:36:07

标签: servicestack mvc-mini-profiler

上下文:我正在使用ServiceStack编写服务。此服务正在调用其他一些远程服务(使用ServiceStack JsonServiceClient)。

要求:将每次调用远程服务显示为MiniProfiler中的一个步骤。

问题:以通用方式实现这一目标的最佳方式是什么?


我服务中的原始代码如下所示:

// Registration of the serviceclient in Apphost.cs:
// container.Register<IRestClient>(x => new JsonServiceClient("http://host:8080/"));
var client = ResolveService<IRestClient>();

HelloResponse response;
using (Profiler.Current.Step("RemoteService: Get Hello"))
{
   response = client.Get(new Hello { Name = "World!" });
}
// ... do something with response ...

我想在代码的这一部分中删除using (Profiler.Current.Step()),以便于阅读和编写。

// Registration of the serviceclient in Apphost.cs:
// container.Register<IRestClient>(x => new ProfiledRestClient("RemoteService", new JsonServiceClient("http://host:8080/")));
var client = ResolveService<IRestClient>();

HelloResponse response = client.Get(new Hello { Name = "World!" });
// ... do something with response ...

我围绕现有客户端创建了一个包装器,其中包含Profiler.Current.Step()接口的每个方法的IRestClient代码  提到客户端的名称,方法和请求(类型)。

// The implementation of the wrapper:
public class ProfiledRestClient : IRestClient
{
    readonly string clientName;
    readonly IRestClient wrappedClient;

    public ProfiledRestClient(string clientName, IRestClient wrappedClient)
    {
        this.clientName = clientName;
        this.wrappedClient = wrappedClient;
    }

    public TResponse Get<TResponse>(IReturn<TResponse> request)
    {
        using (Profiler.Current.Step("{0}: Get {1}".Fmt(clientName, request.GetType().Name)))
        {
            return wrappedClient.Get(request);
        }
    }

    public TResponse Post<TResponse>(IReturn<TResponse> request)
    {
        using (Profiler.Current.Step("{0}: Post {1}".Fmt(clientName, request.GetType().Name)))
        {
            return wrappedClient.Post(request);
        }
    }

    // etc. the same for all other methods of IRestClient interface
}

它正在工作,但感觉有点脏。有没有更好的方法呢?

感谢您的见解。

0 个答案:

没有答案