Effective pattern for getting progress info about

2019-02-22 18:11发布

问题:

I want to get progress updates about a method called on WCF.

For example I run 1000 queries and want to know the current status.

回答1:

If a duplex contract is not workable in your environment, you will have to resort to polling. Your initial method could return an identifier (a GUID perhaps) and then you could make subsequent calls to another method to check the progress, and pass in the identifier.

This will obviously require you to store the progress information somewhere (like a session or a database), which is not great.



回答2:

Yes - use a duplex contract and report the progress every so often by using callbacks.



回答3:

That depends quite a bit on the services you are calling and how long you expect the operations to take.

If you are kicking off 1000 queries on a single service, you will likely get hit by the service throttling before all the calls to the service can be received.

There is a similar phenomenon on the client side. WCF will only allow so many concurrent calls at a time. This is configurable to some extent, but I would be surprised if 1000 concurrent calls would work without a hick-up or two.

If the calls end up being more or less synchronous, I would put all the queries in a queue and process each call in turn. You can then monitor the queue from your UI to update progress as calls to the service are completed.

If your architecture supports 1000 concurrent calls, then the duplex binding will be a good fit. You can just poll for completion.

Alternatively, you can create a pub / sub service that the target service updates as queries are completed. Your client would just catch events from the pub / sub service as the results from the queries become available.