Parallel WCF calls to multiple servers
- by gregmac
I have a WCF service (the same one) running on multiple servers, and I'd like to call all instances in parallel from a single client. I'm using ChannelFactory and the interface (contract) to call the service. Each service has a local <endpoint> client defined in the .config file.
What I'm trying to do is build some kind of generic framework to avoid code duplication.
For example a synchronous call in a single thread looks something like this:
Dim remoteName As String = "endpointName1"
Dim svcProxy As ChannelFactory(Of IMyService) = New ChannelFactory(Of IMyService)(remoteName)
Try
svcProxy.Open()
Dim svc As IMyService = svcProxy.CreateChannel()
nodeResult = svc.TestRemote("foo")
Finally
svcProxy.Close()
End Try
The part I'm having difficulty with is how to specify and actually invoke the actual remote method (eg "TestRemote") without having to duplicate the above code, and all the thread-related stuff that invokes that, for each method.
In the end, I'd like to be able to write code along the lines of (consider this psuedo code):
Dim results as Dictionary(Of Node, ExpectedReturnType)
results = ParallelInvoke(IMyService.SomeMethod, parameter1, parameter2)
where ParallelInvoke() will take the method as an argument, as well as the parameters (paramArray or object() .. whatever) and then go run the request on each remote node, block until they all return an answer or timeout, and then return the results into a Dictionary with the key as the node, and the value as whatever value it returned.
I can then (depending on the method) pick out the single value I need, or aggregate all the values from each server together, etc.
I'm pretty sure I can do this using reflection and InvokeMember(), but that requires passing the method as a string (which can lead to errors like calling a non-existing method that can't be caught at compile time), so I'd like to see if there is a cleaner way to do this.
Thanks