Exposing Inheritance Tree Via API

Topics: Web Api
Oct 18, 2011 at 5:48 AM

Hi,

I am trying to expose resource inheritance via WebGet but getting following error.

Service 'ContactsResource' implements multiple ServiceContract types, and no endpoints are defined in the configuration file.  The ServiceHost of type 'HttpServiceHost'  can set up default endpoints, but only if the service implements only a single ServiceContract. Either change the service to only implement a single ServiceContract, or else define endpoints for the service explicitly in the configuration file.

My config is as mentioned below

var config = new HttpConfiguration();
            RouteTable.Routes.MapServiceRoute<ContactsResource>("contacts", config);

 

 My class hierarchy/inhritance tree is as mentioned below

 

public class Contact
    {
    }

    public class contact1 : Contact
    {
        public string c1f1 { get; set; }
    }

    public class contact2 : Contact
    {
        public string c2f1 { get; set; }
        public string c2f2 { get; set; }
    }

 Finally, my Resource class in as mentioned below. 

private static List<Contact> contacts = new List<Contact>()
        {
            new contact1 {c1f1="c1f1"},
            new contact2 { c2f1="c2f1",c2f2="c2f2" }
           };

        [WebGet(UriTemplate = "")]
        public List<Contact> Get()
        {
            return contacts.ToList();
        }
 
 Can somebody suggest how to expose this inheritance structure through API?
Thanks.
Oct 18, 2011 at 7:42 AM
Edited Oct 18, 2011 at 7:51 AM

Is your ContactsResource class decorated with [ServiceContract]?  

Also, after you have made sure the ServcieContract is in place, you'll need to also decorate the Contact class with KnownType...

    [KnownType(typeof(contact1))]
    [KnownType(typeof(contact2))]
    public class Contact
    {
    } 

...if you are using the DataContractSerializer to serialize to XML.  I.e.

var config = new HttpConfiguration();
config.Formatters.XmlFormatter.UseDataContractSerializer = true;
RouteTable.Routes.MapServiceRoute<ContactsResource>("contacts", config);

Thanks,
Maggie Ying

Oct 18, 2011 at 9:02 AM

Thanks a ton!!

That's working..there is a small problem though.

In JSON response, I get "_type" attribute/field. How can it be suppressed?

[{"__type":"contact1:#IFAPI","c1f1":"c1f1"},{"__type":"contact2:#IFAPI","c2f1":"c2f1","c2f2":"c2f2"}]

 

Coordinator
Oct 18, 2011 at 4:22 PM

In .NET 4.5 we added a settings knob where you can turn off emitting the type information, but unfortunately this isn’t available on .NET 4. I think your options are to either live with the type hints, or switch to something like JsonValue or maybe a custom JSON.NET formatter.

Daniel Roth

From: maheshkshirsagar [email removed]
Sent: Tuesday, October 18, 2011 2:02 AM
To: Daniel Roth
Subject: Re: Exposing Inheritance Tree Via API [wcf:276230]

From: maheshkshirsagar

Thanks a ton!!

That's working..there is a small problem though.

In JSON response, I get "_type" attribute/field. How can it be suppressed?

[{"__type":"contact1:#IFAPI","c1f1":"c1f1"},{"__type":"contact2:#IFAPI","c2f1":"c2f1","c2f2":"c2f2"}]