I am working on a project where our server communicate with several servers exposing the same interface (as in clustering). We use round-robin to select which server to send our request to. So far, we have been using Expression to call our ClusterManager, but this results in some ugly code. It also makes unit tests messy, and a bit difficult to verify method parameters. It also seem to result in some issues with Moq with random exceptions (not topic for this post).
I currently call the server and unit test as documented in the post Moq Setup for interface with action or function arguments
On to the point:
I currently have the following approach:
public interface IServerAdapter
{
void CallServer(Expression<Action<IServerExposingToClient>> methodToCall, out bool serverCalled);
}
private void DoSomething()
{
MainViewModel.ServerAdapter.CallServer(server => server.SaveServerSettings(ServerSettings));
}
I want to change this to be the following:
public interface IServerAdapter
{
IServerExposingToClient ServerProxy { get; }
}
private void DoSomething()
{
MainViewModel.ServerAdapter.ServerProxy.SaveServerSettings(ServerSettings);
}
ServerProxy is a RealProxy, and I need to translate this call to ClusterManager to perform the actual call to the server. Presently, ClusterManager has the same signatures for CallServer, which is a bit redundant for the existing implementation. For the translation of the call in Invoke(IMessage), I have two ideas.
Idea 1 Translate IMethodCallMessage to a lamda expression (which I haven't been able to figure out how to do) and pass it on to ClusterManager. Except for the issue I have with not knowing how to create the expression, and then again handle the return value. Some methods returns a value, and others don't, so this involves keeping multiple overloads of ClusterManager.CallServer. I also need to create IMessage to return from Invoke.
Idea 2 Get the RealProxy of the TransparentProxy of the WCF connection I currently use to call the server, and directly call Invoke with the message I have. In my trial (using an integration test), I am seemingly able to call the server successfully, but looking through the ReturnMessage, there is no return value, and the Exception property is set to ArithmetricException when ServiceChannelProxy tries to get message data. This could either be because I did something wrong in my test, or that I can't use the same LogicalCallContext (or something else) on two separate proxies.
Any pointers on how to deal with this? I prefer Idea 2 as it seems the simplest, and hopefully results in the least amount of code. Implementing all the methods in IServerExposingToClient in a class to call on the correct server is not something I would like to do.
Any pointers on how to deal with this?
Using the information found here, I have been able to solve the issue. In the code below, proxy is the ICommunicationObject for the channel to the WCF service. Seems to work quite well.
public override IMessage Invoke(IMessage msg)
{
var methodCall = (IMethodCallMessage)msg;
var proxy = FindNextProxy();
try
{
MethodBase methodBase = methodCall.MethodBase;
object[] args = methodCall.Args;
object returnValue = methodBase.Invoke(proxy, args);
return CreateReturnMessage(returnValue, methodCall);
}
catch (TargetInvocationException ex)
{
return CreateReturnMessage(ex.InnerException, methodCall);
}
}