开发者

Returning a List<SuperType> on a WCF service

开发者 https://www.devze.com 2022-12-13 08:52 出处:网络
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 l

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 beli开发者_运维技巧eve 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


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

Sorry for the bother

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消