Wcf Serialize List Of Interfaces

HTML clipboard Objective In this article, I will explain. How to work with collection in WCF?. How to work with concrete collection List?.

In fact, WCF won't even return the DataContract MyClass. WCF can't figure out how to deserialize the list of dogs and cats. The solution is to use the KnownTypes attribute on the DataContract making use of these subclassed objects. With the KnownType attribute, WCF knows which concrete objects to use. When I test the service on the client the method returns an object instead of ListIWatchable. Is it possible to return a List of IWatchable, since IWatchable is an interface with WCF? Method: public ListIWatchable GetWorkload( Guid nodeId, int maximum ) IWatchable.

How to work with interface IEnumerable?. How to convert Array at client side to collection Collection at service side and client side Since, COLLECTION is very much specific to.Net, so WCF does not expose it to the metadata of the service. But they are very useful, so WCF provides dedicated marshaling rules for collection. Collection will be exposed as array in service metadata Let us say, you are having a contract and service implementation as below, If you see carefully, I am returning a collection of the type student IService1.cs Service1.svc.cs Now when you add this service reference at the client side, you can see the collection is exposed as array. To see this, add the service reference in a client. I have created a console client and added the service reference of above service. When you click on ServiceReference1 in solution explorer, you can open the proxy code generated at the client side.

So click on object browser and you can see that array has been returned in metadata instead of collection. At the client side Working with List or Concrete collection Imagine a scenario, where return type is concrete collection not an interface.

Wcf Serialize List Of Interfaces

For example, we need to return List instead if IEnumerable As we know from.Net 3.5 all the classes are by default serialized. And collection we are returning is marked with Serializable not with DataContract. Signature of Add method should be, Let us say, Contract is, IService1.cs If you see above, we are returning List instead of IEnumerable.

So again at the client side, List would be exposed as Student in service metadata. Service implementation is as below, Service1.svc.cs And again after adding the service reference at the client side, you can see in the object browser List is exposed as Student So, to call the service at the client side, we need to get the student in array of student So if you see above code, I am taking the output of GetStudents method in array of student(Student ).

If you want to avoid that, you do not want to use array but instead you want to get collection at the client side. Follow the below steps, Step 1 While adding the service reference click on Advanced option Step 2 From the drop down of collection type tab, select System.Collections.Generic.List option.

Now at the client side, Now you can see at the client side proxy is having list as return not array. Conclusion I hope this article was useful to you.

Wcf Serialize List Of Interfaces

Thanks for reading. Happy Coding.

The missing ingredient is the Serializable modifier on the class (in my case, my fields class). So while a List may be 'serializable' when named as a DataMember of a DataContract the members of the class are not serialized inspite of the fact that they are themselves serializable, such as a 'string' is serializable. In my particular case the fact that the Serializable modifier causes all members to be serialized is of no real consequence since it is really a 'struct' with its sole purpose for existing is to hold the fields that I want serialized.

It is kind of an intermediate class to hold just those things that I want on the wire anyway. I think that this satisfies the purist to a large degree. I am a purist in theory, and a pragmist because I get paid when it works. Thank you everyone, this has been an experience. List is indeed serialized, but the you insert isn't, or at least not automatically. And your class kind of becomes a part of your List. When you mark a class as Serializble that class is suddenly.

Serialize

Well, serializable. It's just that now everything is serialized, also private attributes. The service orienated tenet that boundaries are explicit doesn't count anymore now, according to purists. Just use DataContract and DataMember on class and fields to specifiy what must be serialized. It uses a new data contract serializer that was introduced with.NET Framework 3.0/WCF. The missing ingredient is the Serializable modifier on the class (in my case, my fields class).

So while a List may be 'serializable' when named as a DataMember of a DataContract the members of the class are not serialized inspite of the fact that they are themselves serializable, such as a 'string' is serializable. In my particular case the fact that the Serializable modifier causes all members to be serialized is of no real consequence since it is really a 'struct' with its sole purpose for existing is to hold the fields that I want serialized. It is kind of an intermediate class to hold just those things that I want on the wire anyway. I think that this satisfies the purist to a large degree.

I am a purist in theory, and a pragmist because I get paid when it works. Thank you everyone, this has been an experience.