gpt4 book ai didi

wcf - 测量服务器上的 WCF 请求时间

转载 作者:行者123 更新时间:2023-12-04 19:37:43 25 4
gpt4 key购买 nike

我想计算两个特定时刻之间的时间:

- start moment would be call to method IDispatchMessageInspector
.AfterReceiveRequest
- end moment would be call to method IDispatchMessageInspector.BeforeSendReply

事实上,我想计算执行服务调用用户代码所需的时间。我认为 IDispatchMessageInspector 的这两种方法是 Hook 的好地方。但不幸的是,我不知道如何将消息的 AfterReceiveRequest 与相应的 BeforeSendReply 调用相关联。

谢谢帕维尔。

最佳答案

这是我曾经编写的一个参数检查器,用于测量我的 WCF 服务方法的性能。请注意,StopwatchBeforeCall 方法中启动并返回,该方法允许您在 AfterCall 方法中将其作为 correlationState 参数检索:

public class PerformanceCountersInspector : IParameterInspector
{
public object BeforeCall(string operationName, object[] inputs)
{
return Stopwatch.StartNew();
}

public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
var watch = (Stopwatch)correlationState;
watch.Stop();
var time = watch.ElapsedMilliseconds;
// Do something with the result
}
}

这里的区别在于使用参数检查器不会考虑序列化输入/输出参数所花费的时间。它只考虑操作时间。如果要包括序列化时间,可以使用 IDispatchMessageInspector . BeforeSendReply方法也有一个 correlationState 工作方式相同。


更新:

您可以通过编写行为扩展来配置 web.config 中的参数检查器:

public class PerformanceCountersBehaviorExtension : BehaviorExtensionElement, IServiceBehavior
{
public override Type BehaviorType
{
get { return typeof(PerformanceCountersBehaviorExtension); }
}

protected override object CreateBehavior()
{
return this;
}

void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}

void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
{
foreach (var endpoint in channelDispatcher.Endpoints)
{
foreach (var operation in endpoint.DispatchRuntime.Operations)
{
var inspector = new PerformanceCountersInspector();
operation.ParameterInspectors.Add(inspector);
}
}
}
}

void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}

然后在你的配置文件中注册扩展:

<services>
<service name="MyAssembly.MyService" behaviorConfiguration="returnFaults">
<endpoint address="" binding="basicHttpBinding" contract="MyAssembly.IMyServiceContract"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="returnFaults">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
<perfCounters />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="perfCounters" type="MyAssembly.PerformanceCountersBehaviorExtension, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>

关于wcf - 测量服务器上的 WCF 请求时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1825257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com