Self Hosted WCF Rest service ERROR : Type 'New

2019-05-10 11:59发布

问题:

I have a WCF Rest service that's self hosted. It works fine until I decided I have to use JToken (Newtonsoft.Json.Linq.JToken). I then got the error :

Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.

I've seen all the answers from this post, this one and others. The fact is, I'm hosting my services within an internal program (in the company where I work, can't show it :-( ). I'm doing it exactly like it's self hosted, but the implementations and the interfaces of my services aren't as a WCF Service Library, just Class Library, so I can't access the menu they're talking about in the answers (where there's : "Reuse types in specified referenced assemblies").

I couldn't find any solution and I would like to use JToken.

Here's my service contract :

[ServiceContract]
    public interface ISearchService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "search/")]
        List<Client> SearchClient(JToken jsonBody);
    }

My hosting is done like in this post : How do I create WCF EndPointBehaviors in Code rather than the configuration? (I had to do it without a configuration file).

Hope my question is clear. Thank you for your help