简体   繁体   English

WCF异步服务器端处理

[英]WCF async server side processing

I need to create a WCF service that accept client request and internally connect to a remote machine to do the job. 我需要创建一个接受客户端请求的WCF服务,并在内部连接到远程计算机来完成这项工作。 The remote machine has a very good processing capacity but not a good processing speed. 远程机器具有非常好的处理能力但不具有良好的处理速度。 This means that can process for example 1000 transactions per second but each transaction can take 1 second, so the only way is to have 1.000 concurrent transactions running in the same second. 这意味着可以处理例如每秒1000个事务,但每个事务可能需要1秒,因此唯一的方法是在同一秒内运行1.000个并发事务。

The remote machine handle very well this situation, but I am worried about WCF, if each transaction is internally (I dont care the client side model (sync or async)) waiting and blocking a thread inside the server for 1 or 2 seconds this could represent a 1.000 working threads live and that could be very dangerous, or maybe WCF use the thread pool and just put others request in a waiting state and that is bad too. 远程机器很好地处理这种情况,但是我担心WCF,如果每个事务都在内部(我不关心客户端模型(同步或异步))等待并阻塞服务器内的线程1或2秒这可能代表一个1.000工作线程实时,这可能是非常危险的,或者可能是WCF使用线程池,只是将其他请求置于等待状态,这也是不好的。

So, my question is about the possibility of processing the request asynchronously in the server side. 所以,我的问题是关于在服务器端异步处理请求的可能性。 So the transaction flow must be like this: 所以事务流程必须是这样的:

  1. Cliente initialize a request (in his side is a synchronous request) Cliente初始化请求(在他身边是同步请求)
  2. Server recive the request and put this request in a Transaction Queue and release the thread 服务器重新接收请求并将此请求放入事务队列并释放该线程
  3. When the task finish, the server complete the request sending HTTP 200 and result to the client. 任务完成后,服务器完成向客户端发送HTTP 200和结果的请求。

Thanks! 谢谢!

You can use the WCF async pattern to achieve this. 您可以使用WCF异步模式来实现此目的。 When you mark your operationContract with the async attribute, WCF uses the IO CompletionPort threads to process the request. 使用async属性标记operationContract时,WCF使用IO CompletionPort线程来处理请求。

so it works the following way. 所以它的工作方式如下。 Your request is handleded by a thread in IIS and once it reaches WCF it goes to sleep and then the IO CompletionPort thread takes the request process it and then sends back the response to the IIS thread that returns the response to the client. 您的请求由IIS中的线程处理,一旦到达WCF,它就进入休眠状态,然后IO CompletionPort线程接受请求进程,然后将响应发送回IIS线程,该响应将响应返回给客户端。

IO CompletionPort threads are much faster and also doesnt slow down your server in terms of performance or resources. IO CompletionPort线程更快,并且在性能或资源方面也不会降低服务器的速度。

Look at the following link for more information. 请查看以下链接以获取更多信息。

In the middle tier server, declare and implement your operation according to the async pattern: 在中间层服务器中,根据异步模式声明并实现您的操作:

[OperationContract(Action = "DoSomething", AsyncPattern = true)]
IAsyncResult BeginDoSomething(AsyncCallback asyncCallback, object asyncState);
void EndDoSomething(IAsyncResult iar);

The implementation of BeginDoSomething should send the request to the remote machine and return immediately. BeginDoSomething的实现应该将请求发送到远程机器并立即返回。 When you call DoSomething() on the client side, WCF in the middle tier will realize that this operation is implemented as an asynchronous Begin/End pair and invoke it appropriately. 当您在客户端调用DoSomething()时,中间层的WCF将意识到此操作是作为异步的Begin / End对实现的,并适当地调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM