views:

25

answers:

3

I've got a class which has properties referenced to couple of interfaces and classes.

Now I'm trying to transfer this one class via WCF (named pipes), what's the best way to deal with it?

Shall go into all referenced interfaces, class and mark stuff as <DataContract()>, <Serializable()> and <DataMember()>, which means at least 6-10 classes to change.

Is there any other practical way to solve this problem? These interfaces and classes all shared in the host as well as the client. They are using the very same DLLs.

+2  A: 

Don't use this complicated type - create a new data contract type that has no dependencies and send that over the wire. Then create an operation contract that accepts this data contract and maps the data contract to your existing type. Since you are sharing assemblies you could make sure that the logic for mapping from the data contract to the complicated type is in the shared assemblies so that both sides of the wire have access to it.

As a side note: are you sure that this is the best way to do this? WCF works best when services are decoupled from each other and it sounds like your services know too much about each other.

Andrew Hare
I was afraid that someone would say this! Too much to do :(
dr. evil
I know it seems like a lot of work but it will pay off in the long run. Any other approach will not scale easily.
Andrew Hare
A: 

You could call DataContractSerializer with parameters (without specifying anything in your class), or have an entry for those in your app configurations file -- see:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

http://msdn.microsoft.com/en-us/library/ms731073.aspx

http://msdn.microsoft.com/en-us/library/ms731809.aspx

I would, however, rather have the contract defined in the class level. That's a one time effort which will definitely pay off. If your class is really too complicated, you could derive another from it and there define the DataContracts and DataMembers you're interested in.

synhershko
+1  A: 

WCF does not "transfer classes" - it serializes your parameters into a (text-based) message and deserializes it back on the other end again.

Anything you do want to transfer has to be serializable and must be represented in a XML schema - you cannot transfer generics or interfaces - only concrete types.

I agree with Andrew Hare - create separate classes for your WCF service and map your current class into those transfer classes (using something like AutoMapper can make your life a whole lot easier).

Marc

marc_s