0

客户端调用我的 Web WCF 服务的方法“Foo()” - 并接收大字节数组作为响应:

public byte[] Foo()
{
    return new byte[10000000];
}

显然,当客户端读取所有数据时,HTTP 连接关闭——如果我知道这是什么时候发生的,我可以跟踪“传输”的总持续时间。我知道跟踪等 - 但我需要以编程方式获取这些数据,以便我可以将其显示给用户。我怎样才能跟踪这个?

4

2 回答 2

0

我有两种解决方案,性能计数器和自定义行为,但我不确定它们是否能准确回答您的问题,因为我没有考虑网络延迟。

性能计数器第一个解决方案使用接近您要求
的内置性能计数器。基本上,您希望为您的服务启用 performanceCounters,然后在您的服务中获取其中之一。确切的持续时间不可用,但Calls per second在其他计数器中。

确保将其添加到您的服务配置中:

<system.serviceModel>
    <diagnostics performanceCounters="All" />
</system.serviceModel>

在您的服务中有一个静态类来保存您的性能计数器。在我的示例中,我将静态实例添加到服务中,实际上我会将其移至另一个类。

public class Service1 : IService1
{
     // in an ideal world thisd how instancename would look like
    //ServiceName.ContractName.OperationName@first endpoint listener address

    private static PerformanceCounter pc = new PerformanceCounter();

     // our static constructor
    static Service1()
    {
        // naming of the instance is garbeld due to length restrictions...
        var cat = new PerformanceCounterCategory("ServiceModelOperation 4.0.0.0");
        foreach (var instance in cat.GetInstanceNames())
        {
            Trace.WriteLine(instance); // determine the instancename and copy over :-)
        }
        pc.CategoryName = "ServiceModelOperation 4.0.0.0";
        pc.CounterName = "Calls Per Second";
        pc.InstanceName = "Service1.IServ84.GetDataUsingD31@00:||LOCALHOST:2806|SERVICE1.SVC";

    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        // do interesting stuff here

        // here I have the value (in the service call but you can call this from anywhere, 
        // even from another thread. 
        // or use perfmon.exe to obtain or make a graph of the value over time...
        Trace.WriteLine(pc.NextValue().ToString()); 

        return composite;
    }
}

自定义行为
此自定义行为拦截服务方法的调用,从而可以启动和停止计时器并存储结果。

添加以下类:

// custom self timing for any wcf operation
    public class Timing :Attribute, IOperationBehavior,  IOperationInvoker 
    { 
        IOperationInvoker innerOperationInvoker;
        private string operName = "";

        public Timing()
        {
        }

        public Timing(IOperationInvoker innerOperationInvoker, string name)
        {
            this.innerOperationInvoker = innerOperationInvoker;
            operName = name;
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {
            dispatchOperation.Invoker = new Timing(dispatchOperation.Invoker, operationDescription.Name);
        }

        public object Invoke(object instance, object[] inputs, out object[] outputs)
        {
            object value;

            var sw = new Stopwatch();
            sw.Start();
            value = innerOperationInvoker.Invoke( instance, inputs, out outputs);
            sw.Stop();
            // do what you need with the value...
            Trace.WriteLine(String.Format("{0}: {1} ms", operName,  sw.ElapsedMilliseconds));
            return value;
        }


        // boring required interface stuff

        public object[] AllocateInputs()
        {
            return innerOperationInvoker.AllocateInputs();
        }

        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
            return innerOperationInvoker.InvokeBegin(instance, inputs, callback, state);
        }

        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
        {
            return innerOperationInvoker.InvokeEnd(instance, out outputs, result);
        }

        public bool IsSynchronous
        {
            get { return innerOperationInvoker.IsSynchronous; }
        }

        public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            // throw new NotImplementedException();
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
            // throw new NotImplementedException();
        }

        public void Validate(OperationDescription operationDescription)
        {
            // throw new NotImplementedException();
        }
    }

现在在您的界面中装饰您要使用 Timing 属性存储其时间的操作:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [Timing]   // Timing for this Operation!
    CompositeType GetDataUsingDataContract(CompositeType composite);
}
于 2013-09-01T13:41:33.217 回答
-1

您可以使用 StopWatch 类找出持续时间,请参阅此 msdn 链接:

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsed.aspx

        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        // your wcf functionalities
        stopWatch.Stop();
        long duration = stopWatch.ElapsedMilliseconds;
于 2013-09-01T09:20:25.177 回答