Error adding service reference: Type is a recursive collection data contract which is not supported
Asked Answered
R

4

15

I tried to add a service reference to a WCF service that resides in the same solution from an ASP.NET MVC 4 project but failed. I got a error saying:

Custom tool error: Failed to generate code for the service reference 'XXX'. Please check other error and warning messages for details. The root warning is:

Warning 9 Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter 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. XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='IXXX'] C:\Projects...\Reference.svcmap 1 1 pqrt.web

If I removed the data contracts from the service contract, it worked. I also tried to add the service reference to other projects like a library project or even an ASP.NET MVC 3 project, it all worked. I was wondering if this was an issue with ASP.NET MVC 4? I was using VS 2012 RC.

One workaround I can think of is to add the service reference to a library project and then call the library project from ASP.NET MVC 4, but I hate to do that since it's an extra step. Any suggestions?

Rizo answered 18/7, 2012 at 13:18 Comment(1)
My answer might be totally off base... can you include the data contract with your question?Pervasive
G
14

If you want to keep the reference to Newtonsoft.Json you could also leave Newtonsoft.Json out of the list of assemblies to check for reuse of datacontracts.

To do this: right click your service reference, then click Configure Service Reference...

Under "Reuse types in referenced assemblies" select the second option to specify in which assemblies to search for reused types and select all assemblies but uncheck Newtonsoft.Json

Glib answered 28/9, 2012 at 12:32 Comment(1)
What if you have a WCF service without a service reference? How would you configure which types to reuse?Calisaya
H
10

Try removing Newtonsoft.Json from your references and re-add your service reference.

Hedgcock answered 6/9, 2012 at 15:33 Comment(1)
Worked for me. In retrospect, it may also be sufficient to configure the Service Reference not to "Reuse types in referenced assemblies"Prudie
G
1

I had this error at compile time when trying to return a JObject as the endpoint result.

I got around it by making the endpoint return object and having this kind of code:

[WebGet(UriTemplate = "SomeRequest?form_request={form_request}", ResponseFormat = WebMessageFormat.Json)]
public object SomeRequest(string form_request)
{
    dynamic result = new JObject();
    // some other code
    result.status = "success";
    return JsonConvert.SerializeObject(result);
}

The jQuery consuming the service via jsonp e.g. $.getJSON('<?>.svc/SomeRequest', 'form_request=' + webform_as_json, request_callback); then unpacks the serialized object like so:

function request_callback(response) {
    var json = $.parseJSON(response);
    if (json.status == 'success') {
Grounds answered 1/10, 2013 at 11:38 Comment(0)
P
0

Do you really mean to return a node in an arbitrarily deep tree?

If so, then instead of returning a JToken, first convert it to a string to get the JSon text. On the client end, you can Jtoken.Parse(yourstring) back into a JToken.

If not, then consider passing back the Value<T> and letting the serialization deal with T.

Pervasive answered 18/7, 2012 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.