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.
You could use C# delegates:
More on this: http://msdn.microsoft.com/en-us/library/ms173171.aspx
You can pass functions with parameters this way:
The first line declares the function
strategy()
accepting a functionf
; That function return the typeR
and takes two parameters of typeT1
andT2
.The second line defines a function that returns a
bool
and accepts twostring
.The third line invokes the strategy passing it the predicate as a parameter.
Use delegates and pass them in a list.
The C#
Func<T>
delegate is used when a return value is needed.( 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.In response to some comments:
This code compiles and is all equivalent:
Result is:
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.
Then you call the method like:
Where DoSpecificStuff is a class that implements the interface and allows you to do specific calls with the proxy:
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.
Not sure to understand what you're trying to achieve, but basically if your service exposes a
GetData(int)
method and aGetData(string)
method as well as an async proxy, you should call both asynchronously using something like: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.