I have a case in which there are 3 hosts: A, B, and C. Host C periodically sends a heartbeat message to host B, which then sends a similar heartbeat message to A that contains all the information in the packet sent by C plus some additional stuff. Obviously, to enable this, A exposes a service to B, and B exposes a service to C.
The serializable class being passed to both is called NodeInfo. Basically just a simple collection of data members, NodeInfo is referenced by the service definitions for both A’s and B’s services.
Here’s the kicker: the Data Contract received by B is of type B.ServiceForC.NodeInfo. But the type that B needs to relay to A is of type A.ServiceForB.NodeInfo. So what I’m finding I need to do is write a lot of repetative code in B to copy data from the inbound NodeInfo‘s to some newly created outbound NodeInfo‘s.
My question is, is there a clever way to handle this? Is there a way to cast a B.ServiceForC.NodeInfo object to a A.ServiceForB.NodeInfo object so I don’t have to recreate all the objects and copy their data? Since they’re not related in any hereditary sense it doesn’t seem possible, but I don’t know.
I’ve done a lot of hunting but can’t seem to find anything that helps. Any suggestions, even if it’s a definitive “you’re outta luck,” would be helpful. Thanks.
By default WCF generates a client side copy of your service interfaces wrapped a namespace which are used in your proxy code. But you’re not forced to do it like this. Put your service interfaces in a lib/dll – delete the WCF generated interfaces from your WCF generated proxy code and use the interfaces from your lib namespace there instead.
That way everything can just use MyLibNamepace.NodeInfo interface that is defined in your lib/dll.
If you don’t already have an exisitng base lib/dll for common code then you could alternatively just compile in your own interface definitions directly into each application.