[英]how do i know how many clients are calling my WCF service function
i am writing a program to test WCF service performance in high concurrency circumstance. 我正在编写一个程序来在高并发情况下测试WCF服务性能。
On client side, i start many threads to call a WCF service function which returns a long list of data object. 在客户端,我启动了许多线程来调用WCF服务函数,该函数返回一长串数据对象。
On server side, in that function called by my client, i need to know the number of clients calling the function. 在服务器端,在我的客户端调用的函数中,我需要知道调用该函数的客户端数量。
For doing that, i set a counter variable. 为此,我设置了一个计数器变量。 In the beginning of the function, i add the counter by 1, but how can i decrease it after the function has returned the result ?
在函数的开头,我将计数器加1,但是在函数返回结果后如何减少它呢?
int clientCount=0;
public DataObject[] GetData()
{
Interlocked.Increment(ref clientCount);
List<DataObject> result = MockDb.GetData();
return result.ToArray();
Interlocked.Decrement(ref clientCount); //can't run to here...
}
i have seen a way in c++. 我已经在C ++中看到了一种方法。
Create a new class named counter. 创建一个名为counter的新类。
In the constructor of the counter class, increase the variable. 在计数器类的构造函数中,增加变量。 And decrease it in the destructor.
并在析构函数中减少它。
In the function, make a counter object so that its constructor will be called. 在函数中,创建一个计数器对象,以便调用其构造函数。 And after the function returns, its destructor will be called.
在函数返回后,将调用其析构函数。
Like this: 像这样:
class counter
{
public:
counter(){++clientCount; /* not simply like this, need to be atomic*/}
~counter(){--clientCount; /* not simply like this, need to be atomic*/}
};
...
myfunction()
{
counter c;
//do something
return something;
}
In c# i think i can do so with the following codes, but not for sure. 在c#中,我认为我可以使用以下代码来执行此操作,但不能确定。
public class Service1 : IService1
{
static int clientCount = 0;
private class ClientCounter : IDisposable
{
public ClientCounter()
{
Interlocked.Increment(ref clientCount);
}
public void Dispose()
{
Interlocked.Decrement(ref clientCount);
}
}
public DataObject[] GetData()
{
using (ClientCounter counter = new ClientCounter())
{
List<DataObject> result = MockDb.GetData();
return result.ToArray();
}
}
}
i write a counter class implement the IDisposable interface. 我写一个计数器类实现IDisposable接口。 And put my function codes into a using block .
并将我的功能代码放入using块中 。 But it seems that it doesn't work so good.
但似乎效果不佳。 No matter how many threads i start, the clientCount variable is far less than the threads number.
无论我启动多少个线程,clientCount变量都远远小于线程数。
Any advise would be appreciated. 任何建议将不胜感激。
Have a look at the various layers WCF uses. 看一下WCF使用的各个层。 You can plug into one of them.
您可以插入其中之一。
For example add a IDispatchMessageInspector to your EndpointBehavior: 例如,将一个IDispatchMessageInspector添加到您的EndpointBehavior:
public class ConsoleOutputMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine("Starting call");
// count++ here
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
// count-- here
Console.WriteLine("Returning");
}
}
See more here: http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx 在此处查看更多信息: http : //weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx
Have a look how to extend WCF here: http://msdn.microsoft.com/en-us/magazine/cc163302.aspx#S6 看看如何在此处扩展WCF: http : //msdn.microsoft.com/zh-cn/magazine/cc163302.aspx#S6
Have you tried WCF's built-in performance counters: 您是否尝试过WCF的内置性能计数器:
http://msdn.microsoft.com/en-us/library/ms735098.aspx http://msdn.microsoft.com/en-us/library/ms735098.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.