Serialize collection with WCF

Posts   
 
    
kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 06-Jun-2013 13:35:41   

Hi, I've followed the documentation to serialize a collection of a known type in WCF. This works just fine


public IEntityCollection2 GetCustomers()
    {
        EntityCollection toReturn = new EntityCollection(new CustomerEntityFactory());
        using(DataAccessAdapter adapter = new DataAccessAdapter())
        {
            adapter.FetchEntityCollection(toReturn, null);
        }
        return toReturn;
    }

But I can't figure out how to do the same when the entity is not known, something like


public IEntityCollection2 GetEntityCollectionByEntityName(string name)
    {
        var factory = // Create factory from parameter name
        EntityCollection toReturn = new EntityCollection(factory);
        ...

Can you help?

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Jun-2013 19:47:25   

You can define KnownTypes for all entity types, or for those which can be returned from the service.

kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 07-Jun-2013 09:20:47   

Hi Walaa

I tried that, but doesn't quiet work at least directly. Changed the code to this


        public IEntityCollection2 GetEntityCollectionByEntityName(string entityName)
        {
            var collectionToFill = EntityCollection.Create(entityName);

            using (var adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(collectionToFill, null, 1000);
            }

            return collectionToFill;
        }

and added the KnownType


        [OperationContract]
        [ServiceKnownType(typeof(EntityCollection<MyClientEntity>))]

That works fine on the WCF side, but on the client side a hash is added to the name like 'MyClientEntityH765HBHG' and can't deserialize it

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 07-Jun-2013 09:40:49   

WCF isn't a generic system, it has to know what types are returned by given methods because it has to deserialize the data into known types on the client. If the types returned by a given method are unknown, it can't know at runtime into which type to deserialize the data.

If a method returns IEntityCollection2, the type of the entity inside the collection has to be known, so you should add known type definitions for IEntityCollection2, and the entity type returned by the method (or types). So a ServiceKnownType(typeof(MyClientEntity)), but be sure that MyClientEntity is a type which is returned by the method in the collection.

Frans Bouma | Lead developer LLBLGen Pro
kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 07-Jun-2013 10:26:24   

Hi Otis, thanks, that's correct.

I fix this with a workaround, not sure is best practice but it works.

Updated the code to: (note the commented code)


        public IEntityCollection2 GetEntityCollectionByEntityName(string entityName)
        {
            var myEntity = CreateEntity(entityName);
            var entityFactory = myEntity.GetEntityFactory();
            //var collectionToFill = entityFactory.CreateEntityCollection(); //Returns EntityCollection<T>. Doesn't work!
            var collectionToFill = new EntityCollection(entityFactory); // Returns EntityCollection<EntityBase2>
            using (var adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(collectionToFill, null, 1000);
            }

            return collectionToFill;
        }

Then on WCF I only need to add


[ServiceContract]
[ServiceKnownType(typeof(EntityCollection))]

where CreateEntity is


private IEntity2 CreateEntity(string entityName)
        {
            return GeneralEntityFactory.Create((EntityType) Enum.Parse(typeof(EntityType), entityName));
        }

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Jun-2013 11:11:51   

Seems good. Thanks for the feedback.