1

Suppose I have the following WCF code:

  try
  {
       ServiceClient proxy = new ServiceClient();
       proxy.ClientCredentials.UserName.UserName = "user";
       proxy.ClientCredentials.UserName.Password = "password";
       proxy.GetData(2);
       if (proxy.State = CommunicationState.Opened)
       {
           proxy.GetData("data");
       }
       proxy.Close();
  }
  catch (FaultException ex)
  {
      // handle the exception      
  }

And since I notice that the try...catch and other logic is repetitive, not to mention that setting up a WCF call is expensive, I want to send many "methods and parameters" to this function.

In essence pass GetData(2) and GetData("data") as a method array, and have the results return either asynchronously or synchronously.

How would I accomplish this?

I suppose I could have two 'ref' objects to handle the results[] and a shared lock to the results[]. However I'm not sure how to pass "methods with parameters" as a parameter to another function.

Perhaps another way of looking at this might be an array of function pointers, to the same function with different params.

Can anyone nudge me into the right way of doing this?

More info:

I am asking this question so I can optimize this approach to handling WCF exceptions and retries but so I don't have to always open/close the client after each call.

Community
  • 1
  • 1
makerofthings7
  • 60,103
  • 53
  • 215
  • 448

6 Answers6

7

Use delegates and pass them in a list.

The C# Func<T> delegate is used when a return value is needed.

List<Func<Data>> funcList = new List<Func<Data>>();
funcList.Add( () => GetData(2) );

// You can use any condition as you otherwise would to add to the list.
if (proxy.State = CommunicationState.Opened)
{
   funcList.Add( () => GetData("data") );
}

List<Data> ProcessFuncs(List<Func<Data>> funcDatas)
{
    List<Data> returnList = new List<Data>();
    foreach(var func in funcDatas)
    {
        returnList.Add(func());
    }
}

( as long as the return types are identical, this will work )

This is just an example of course; if your methods don't return anything, you can use the C# Action delegate, which just executes an action and doesn't return any value.

List<Action> actionList = new List<Action>();
actionList.Add( () => ProcessData("data")); // ProcessData is a void with no return type
actionList.Add( () => ProcessData(2));

public void ProcessActions(List<Action> actions)
{
    foreach(var action in actions)
    {
        action();
    }
}

In response to some comments:

This code compiles and is all equivalent:

class Program
{
    public static string GetData(string item) { return item; }
    public static string GetData(int item) { return item.ToString(); }

    static void Main(string[] args)
    {
        string someLocalVar = "what is it?";
        int someLocalValueType = 3;

        Func<string> test = () =>
        {
            return GetData(someLocalVar);
        };

        Func<string> test2 = () => GetData(someLocalValueType);
        someLocalValueType = 5;

        List<Func<string>> testList = new List<Func<string>>();

        testList.Add(() => GetData(someLocalVar));
        testList.Add(() => GetData(2));
        testList.Add(test);
        testList.Add(test2);

        someLocalVar = "something else";

        foreach(var func in testList)
        {
            Console.WriteLine(func());
        }

        Console.ReadKey();
    }
}

Result is:

enter image description here

Dmitriy Khaykin
  • 5,238
  • 1
  • 20
  • 32
  • You can even use the Action version if they *do* return something. You can include the assignment in the lambda. It can be dangerous, but sometimes it might be what you want. – Magus Jan 28 '14 at 18:05
  • Yes, or you can also have a { } body for your Action instead of a lambda and do whatever in it and then not return anything from the Action. Lots of possibilities. – Dmitriy Khaykin Jan 28 '14 at 18:06
  • Am I missing something because I don't think that code even compiles... I get "Delegate 'System.Func' does not take 1 arguments". I'm pretty sure you can't just add delegates to a list with the arguments being passed as if you're invoking the method... – evanmcdonnal Jan 28 '14 at 18:08
  • I don't think delegates are a good solution for this. This is quite complicated and as you point out the return types have to be the same, and how do you handle the if (proxy.State = CommunicationState.Opened) condition in the question? – Weyland Yutani Jan 28 '14 at 18:08
  • This doesn't even compile. Your list only takes `Func` objects with `Data` as the return type and no arguments then you try to add a `Func` and `Func` which gives a compiler error. The basic concept isn't even possible because the methods he wants to use have different signatures and you're declaration of the list makes it so only a single signature is accepted, one with no args and a return type of `Data`. – evanmcdonnal Jan 28 '14 at 18:14
  • @WeylandYutani OP asked how to pass multiple methods as a parameter; this is how you can do it. – Dmitriy Khaykin Jan 28 '14 at 18:14
  • although actually I suppose this IS what the question is asking (from the title). I just don't think delegates are a good solution for the problem in the code. – Weyland Yutani Jan 28 '14 at 18:15
  • @David Khaykin yes my bad – Weyland Yutani Jan 28 '14 at 18:15
  • @DavidKhaykin ok, I think that version works. I'm not sure why... I'd expect it to not compile for similar reasons but I'm guessing the lambda creates a closure which makes it work. Still I think this is a case where `DynamicInvoke` should be used... If you set up your delegates like I've shown they can be invoked anywhere with no knowledge of what the args are since it's just another param you're passing along. What happens if I use your code with `() => GetDate(localVar)` then the value of `localVar` changes before invocation? I would guess it uses the old value when the delegate is executed – evanmcdonnal Jan 28 '14 at 18:16
  • I am checking it. I wrote it from memory at first hence the mistakes – Dmitriy Khaykin Jan 28 '14 at 18:18
  • @evanmcdonnal: Not sure when that value changing would matter, and I can't think why yours would protect against that. – Magus Jan 28 '14 at 18:21
  • @Magus it creates a closure. I'll show an example in my answer. – evanmcdonnal Jan 28 '14 at 18:22
  • There are ways around the changing value in the off chance that it even matters. If it isn't needed, you don't need to bother. – Magus Jan 28 '14 at 18:28
  • It does compile (now, hehe) I've added some clarification code. Also yes if you use a variable and change it's value before executing the delegate, the changed value will be used. – Dmitriy Khaykin Jan 28 '14 at 18:33
  • Yes if protecting the value is needed then create a copy. – Dmitriy Khaykin Jan 28 '14 at 18:35
  • @DavidKhaykin no if you change the value the updated value will not be used. Look at this question http://stackoverflow.com/questions/9591476/are-lambda-expressions-in-c-sharp-closures a closure is created which means the value you used at the time of definition is the value the method will execute with, no matter what happens between the time of definition and the time of invocation. This may still be the right answer (perhaps the OP is fine with a closure) but it is fundamentally different than using `DynamicInvoke`, do not confuse the two. – evanmcdonnal Jan 28 '14 at 18:44
  • Run my test code :) The changed value is used even with the value type - I used a string and an int, changed both before executing the Funcs, and the result is the updated values are used. – Dmitriy Khaykin Jan 28 '14 at 18:44
  • Thank you, I really like your approach. I [have code here that I want to integrate with this](http://stackoverflow.com/a/9489503/328397), but it uses a delegate... and therefore requires a custom delegate per return type. Do you know how I could combine approaches? – makerofthings7 Jan 28 '14 at 18:57
  • @makerofthings7 let me ponder on that. You should be able to pass a List> instead of List. This would work for each proxy of Type T – Dmitriy Khaykin Jan 28 '14 at 19:00
  • Actually, the problem I'm having is that UseServiceDelegate has a defined return type that I can't change (or don't know how). It would be nice if I could create an abstraction somehow – makerofthings7 Jan 29 '14 at 01:01
  • I'm not following -- in your other example it's a void which should make it easy to work with. You may need to try and combine this answer and your other code and post a new question if you run into an issue; it's difficult to ascertain exactly how to overlay UseServiceDelegate to the current question, here. – Dmitriy Khaykin Jan 29 '14 at 01:06
1

Bellow is an example of how to make a collection of delegates and their arguments then invoke them later on without knowing the methods definition. As far as I know if you want to invoke methods with different definitions in a single general call you have to do something like this.

   List<Tuple<delegate, object[]>> delegates = new List<Tuple<delegate, object[]>>();

   delegates.Add(new Tuple<delegate, object[]>(new Func<Arg1Type, Arg2Type, ReturnType>(MyFunctionName), new object[] { arg1, arg2 });

   foreach (Tuple<delegate, object[]> d in delegates)
   {
        d.Item1.DynamicInvoke(d.Item2);
   }
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • I think I'd prefer to just have a collection of `Action`, and pass the arguments and function in the lambda. Might be cleaner. – Magus Jan 28 '14 at 18:04
  • @Magus my code is a little sloppy, those have to be `Func`'s because they have a return type, I'm just not assigning it anywhere. I don't know about that code passing args in the lambda, as far as I can tell it doesn't compile. Actually, 100% it doesn't compile. When you say `List>` a and b are the args and c is the return type. He declares those funcs with return type `Data` then tries to pass them random args that he didn't say they would take... You have to use an object array and `DynamicInvoke` to get that kind of behavior. – evanmcdonnal Jan 28 '14 at 18:10
  • I was meaning along the lines of `string b; Action a = () => b = SomeFunction(param1, param2);` where b should probably be a field. It's quite clean, and does the same thing. – Magus Jan 28 '14 at 18:12
  • @Magus it's not the same thing at all. I'm pretty confident that lambda creates a closure which imo completely defeats the purpose because I can't change the value of the arguments before invoking the function. If I'm doing that I may as well just call the method normally right there because that's pretty much all I'm getting. – evanmcdonnal Jan 28 '14 at 18:21
  • You have a point, but since C# passes by value, changing the local variable after assigning it to a member of the tuple will have an identical result. – Magus Jan 28 '14 at 18:34
  • @Magus that's not true. Yes C# passes by value by default but what happens when I have a reference type? That is more analogous to passing a pointer into a method in C++. Everything passed into a delegate using my code will be a reference, it will not behave the same. On top of that, when my method is executed, the args are passed to it at the time of invocation, in the lambda examples, the method is executed with the value of the args at the time of declaration (that's whole closure part, the values are closed under the method, not the case with my code). – evanmcdonnal Jan 28 '14 at 18:40
1

I wouldn't use delegates here because then you are constrained by types and to solve that it becomes horrible and over-complicated. I would just have a callback that gives you free reign over the ServiceClient once it has been set up. I think this is a pattern that has a name but I don't know.

interface IProxyActionCallback
{
    void DoProxyStuff(ServiceClient proxy);
}

void MyMethod(IProxyActionCallback callback)
{
    try
    {
        ServiceClient proxy = new ServiceClient();
        proxy.ClientCredentials.UserName.UserName = "user";
        proxy.ClientCredentials.UserName.Password = "password";

        callback.DoProxyStuff(proxy);

        proxy.Close();
    }
    catch (FaultException ex)
    {
        // handle the exception      
    }
}

Then you call the method like:

MyMethod(new DoSpecificStuff());

Where DoSpecificStuff is a class that implements the interface and allows you to do specific calls with the proxy:

class DoSpecificStuff : IProxyActionCallback
{
    public void DoProxyStuff(ServiceClient proxy)
    {
        proxy.GetData(2);
        if (proxy.State = CommunicationState.Opened)
        {
            proxy.GetData("data");
        }
    }
}

So you'd have tons of classes that implement the interface, and they all "share" the same try-catch boiler-plate proxy stuff which is in one place.

Weyland Yutani
  • 4,682
  • 1
  • 22
  • 28
0

You could use C# delegates:

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance. Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs. The following example shows a delegate declaration:

More on this: http://msdn.microsoft.com/en-us/library/ms173171.aspx

Paweł Bejger
  • 6,176
  • 21
  • 26
0

You can pass functions with parameters this way:

public void strategy<R, T1, T2>(Func<R, T1, T2> f);

public bool predicate(string a, string b);

strategy<bool, string, string>(predicate);

The first line declares the function strategy() accepting a function f; That function return the type R and takes two parameters of type T1 and T2.

The second line defines a function that returns a bool and accepts two string.

The third line invokes the strategy passing it the predicate as a parameter.

pid
  • 11,472
  • 6
  • 34
  • 63
0

Not sure to understand what you're trying to achieve, but basically if your service exposes a GetData(int) method and a GetData(string) method as well as an async proxy, you should call both asynchronously using something like:

var getData = proxy.GetDataAsync(2);
var getData2 = proxy.GetDataAsync("data");

await Task.WhenAll(getData, getData2);

// Gets the result using getData.Result...etc.
ken2k
  • 48,145
  • 10
  • 116
  • 176