user1112324
user1112324

Reputation: 643

WCF REST Service throwing Cannot serialize parameter of type error

I know that something similar to this has been answered before but its not exactly what im getting.

I have a LINQ to Entity model consisting of a number of dynamically generated classes such as Shows, Users etc

I have a WCF REST service which returns various objects. For instance, i have the following method:

[WebGet(UriTemplate = "GetShow/{showid}")]
    public Ent.Shows GetShow(string showid)
    {
        using (var context = new Ent.choobEntities())
        {
            int showID = Convert.ToInt32(showid);
            List<Shows> shows = (from Shows in context.Shows
                          where Shows.Id == showID
                          select Shows).ToList();
            if (shows.Count > 0)
                return (Ent.Shows)shows.First();
            else
                return new Ent.Shows();
        }
    }

In the case above where a particular show doesnt get returned i create a new empty one and when testing with my REST client i get:

    <Shows xmlns="http://schemas.datacontract.org/2004/07/Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Id>0</Id>
<ShowDescription i:nil="true"/>
<ShowImage i:nil="true"/>
<ShowTitle i:nil="true"/>
</Shows>

Thats perfect. However when i get a match i get the most irritating error, the

Cannot serialize parameter of type 'System.Data.Entity.DynamicProxies.Shows because it is not the exact type 'Entities.Shows' in the method signature and is not in the known types collection. In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute

Ive found suggestions to put [ServiceKnownType(typeof(<Shows>))] everywhere, above [ServiceContract] and KnownType etc in the class itself. NOTHING works. Surely there is a way without having to recreate a new instance of Shows everytime and copy the data over from the returned Entity into a new instance? i don't believe that is the only way.

Upvotes: 1

Views: 588

Answers (1)

Jared Shaver
Jared Shaver

Reputation: 1339

Have you tried disabling dynamic proxies with:

context.ContextOptions.ProxyCreationEnabled = false;

This is discussed under the "Serializing POCO Proxies" section here: http://msdn.microsoft.com/en-us/library/dd456853.aspx

Upvotes: 2

Related Questions