servicestack 使用ServiceStack MiniProfiler评测所有服务客户端调用
上下文:我正在使用ServiceStack编写服务。此服务正在调用其他一些远程服务(使用ServiceStack
servicestack 使用ServiceStack MiniProfiler评测所有服务客户端调用,
servicestack,mvc-mini-profiler,
servicestack,Mvc Mini Profiler,上下文:我正在使用ServiceStack编写服务。此服务正在调用其他一些远程服务(使用ServiceStackJsonServiceClient) 要求:在MiniProfiler中作为一个步骤显示对远程服务的每个调用 问题:以通用方式实现这一点的最佳方式是什么 我的服务中的原始代码如下所示: // Registration of the serviceclient in Apphost.cs: // container.Register<IRestClient>(x =>
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 ...
我在现有客户机周围做了一个包装器,其中包含Profiler.Current.Step()
为IRestClient
接口的每个方法编写的代码
提及客户机的名称、方法和请求(类型)
//包装器的实现:
公共类ProfiledRestClient:IRestClient
{
只读字符串clientName;
只读IRestClient wrappedClient;
公共ProfiledRestClient(字符串clientName,IRestClient wrappedClient)
{
this.clientName=clientName;
this.wrappedClient=wrappedClient;
}
公共响应Get(IReturn请求)
{
使用(Profiler.Current.Step(“{0}:Get{1}”.Fmt(clientName,request.GetType().Name)))
{
返回wrappedClient.Get(请求);
}
}
公共响应邮件(IReturn请求)
{
使用(Profiler.Current.Step(“{0}:Post{1}”.Fmt(clientName,request.GetType().Name)))
{
返回wrappedClient.Post(请求);
}
}
//等。IRestClient接口的所有其他方法也是如此
}
它在工作,但感觉有点脏。有更好的方法吗
谢谢你的洞察力
// 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 ...
// 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
}