views:

150

answers:

2

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

+1  A: 

One way to do this is to use:

System.Threading.ThreadPool.QueueUserWorkItem( ... )

Look here for the VB syntax: http://msdn.microsoft.com/en-us/library/4yd16hza.aspx

Put the aysnc code in the WaitCallback.

Chris O
That would be inside of the ParallelInvoke() method in my above example -- but still doesn't help with choosing which method (eg, TestRemote()) without having to duplicate all the surrounding code for each method.
gregmac
Yes, you're right, I didn't answer all of your question. See Cheeso's comment above for the actual answer.
Chris O
+2  A: 
 public static TResult MakeCall<TService, TResult>(Func<TService, TResult> method,  string remoteName)
        {
            // try not to create the factory object for each call, it can be a singleton
            var factory = GetFactory<TService>(remoteName);
            TService proxy = default(TService);
            TResult result = default(TResult);
            try
            {
                proxy = factory.CreateChannel();
                result = method(proxy);
            }
            finally
            {
                // proxy disposal is a bit tricky in WCF and you might need to read more about Close/Abort
                 Close(proxy);                    

            }
            return result;
        }

which lets you call any method on any interface without having to duplicate the plumbing code.

public interface IMyService
        {
            int Sum(int a, int b);
            int Max(int a, int b);
        }

public interface IMyService2 { bool Test(string value); }

   static void Main(string[] args)
        {
            var a = 12;
            var b = 12;
            var r1 = MakeCall((IMyService proxy) => proxy.Sum(a, b),  "endpoint1");
            var a1 = 12;
            var b1 = 14;
            var r2 = MakeCall((IMyService proxy) => proxy.Max(a1, b1), "endpoint1");
            var r3 = MakeCall((IMyService2 proxy) => proxy.Test("test"), "endpoint2");
        }

You can use MakeCall as a building block for your end solution.

Pawel Pabich