Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a List<Interface> over WCF?

Tags:

interface

wcf

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 )
like image 676
Denny Ferrassoli Avatar asked Aug 03 '09 05:08

Denny Ferrassoli


1 Answers

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.

like image 147
John Saunders Avatar answered Nov 04 '22 19:11

John Saunders