tags:

views:

504

answers:

1

I have a WCF service where I am trying to return a List (where IWatchable is a custom interface I have built) in one of my operation contracts. When I test the service on the client the method returns an object[] instead of List<IWatchable>. Is it possible to return a List of IWatchable, since IWatchable is an interface with WCF?

Method:

public List<IWatchable> GetWorkload( Guid nodeId, int maximum )

IWatchable:

public interface IWatchable
{
 string ActionName { get; set; }
 Guid ActionReference { get; set; }
}

Hopefully a bit more info will be helpful...

I have a derived interface:

public interface IAMRAWatchable: IWatchable

And three concrete implementations from IAMRAWatchable:

public class InstrumentationWatch: IAMRAWatchable
public class OutputWatch: IAMRAWatchable
etc...

In my WCF method that returns List<IWatchable> I want to send an InstrumentationWatch and an OutputWatch to the client... Is this possible or am I going about this the wrong way?


Resolved

Thanks to John I found my solution. KnownType wasn't working since I was using List<IWatchable> - So I wrapped my list into a new class and added the attributes to it. I'll need to re-factor my code but for others who are interested here is the class:

[DataContract]
[KnownType( typeof( InstrumentationWatch ) )]
[KnownType( typeof( OutputWatch ) )]
public class WorkInfo
{
 [DataMember]
 public List<IWatchable> WorkQueue { get; set; }
}

and my WCF method:

public WorkInfo GetWorkload( Guid nodeId, int maximum )
+5  A: 

An interface can never be serialized. It is only a description of behavior.

You can serialize objects which implement the interface, but you must tell WCF what their types are. See Data Contract Known Types.

John Saunders
I tried the KnownType attributes but I still couldn't get around my issue. Perhaps the extra information I provided will help clear up my exact issue. Thanks
DennyDotNet