views:

358

answers:

1

Hey,

I am trying to get a WCF service to return a List that contains instances of classes that inherit from A but am getting "The underlying connection unexpectedly closed" when the service returns the list.

I have the following

  [DataContract]
  [Serializable]
  public class A { ... }

  [DataContract]
  [Serializable]
  public class B : A { ... }

  [DataContract]
  [Serializable]
  public class C : A { ... }

I then have a service defined as

[ServiceContract( NameSpace = "Name.Space" )]
public interface I
{
    [OperationContract]
    List<A> GetList();
}

If I attempt to return a list as follows

  List<A> list = new List<A>()
  {
    new B(),
    new C()
  }

I get the above error in the proxy class.

If a change the service to return List<B> and just add Bs to it it returns fine, so this leads me to believe it is related to WCF having difficulty with the subtypes.

I have tried adding

  [ServiceKnownType( typeof( B ) )]
  [ServiceKnownType( typeof( C ) )]

To the class defintion of A, to no avail.

So, is it possible to return a list of subtypes in the same list through WCF?

If so, what am I missing/doing wrong?

Thanks in advance for any assistance you can provide

+3  A: 

Ah, it seems I should have been using KnownType not ServiceKnownType

Sorry for the bother

Regards

Adam Fox
Agreed...you can either use the ServiceKnownType on your contract (i.e. interface) or use KnownType on the individual types themselves.
MattK