6

If I use only simple methods in my WCF service, everything works fine. However, if I include the following method, a problem occurs:

[OperationContract]
public List<KeyValuePair<string, string>> GetAllAccounts()
{
    return AccountBusiness.GetAllAccounts();
}

I get this error when referencing the service from another project:

Warning 5 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='NodeService'] C:\inetpub\wwwroot\MyclientProject\Service References\NodeService\Reference.svcmap 1 1 MyClientNode

If I remove the method above, recompile and update the reference, then it works fine. But when I add the above method, I consistently get this warning.

One suggestion I found was to uninstall Visual Studio 2012 and repair Visual Studio 2010. I don't want to do that.

Another suggested to uncheck "Reuse types in all referenced assemblys", but this messes up the method signatures and is not usable.

Community
  • 1
  • 1
Niels Brinch
  • 3,033
  • 9
  • 48
  • 75

3 Answers3

7

As it turned out, the error was caused by the fact that I had a Microsoft Web API package installed from NuGet, which included Json.NET. After uninstalling this, it works fine.

This may be the answer for you - if not, look at which other packages you have installed (that you don't need) and remove them.

Niels Brinch
  • 3,033
  • 9
  • 48
  • 75
  • 12
    You don't have to remove the packages, you can just now use them in the service: http://nirlevy.blogspot.co.il/2012/08/cannot-import-wsdl-porttypedetail.html – Nir Levy Aug 23 '12 at 12:17
  • 1
    Nir, this was amazingly helpful. Thank you! – benjy Dec 20 '12 at 20:57
1

The NewtonSoft.Json library that is installed on the Server is not the same one that is installed on the client from NuGet. This is why the service reference is unable to reuse it.

In this case just click on 'Reuse types in specified referenced assemblies', check everything off but the Newtonsoft.Json assembly and re-generate.

The Service Reference will now generate without errors.

dirthdog
  • 11
  • 1
0

You cannot return a type JObject or JToken, directly in a WCF REST Service, as you get the dreaded error "Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract".

I fixed this by serializing the JSON Objects into type Stream before returning them...

[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedResponse,
UriTemplate = "/api/myGetRequest")]
public Stream myFunction(string x, string y)
{ 
  WebOperationContext webcontext = WebOperationContext.Current;
  webcontext.OutgoingResponse.Headers[HttpResponseHeader.ContentType] = "application/json";

  string json = JsonConvert.SerializeObject(myJsonObject, Newtonsoft.Json.Formatting.Indented);`

  return new MemoryStream(Encoding.UTF8.GetBytes(json ?? ""));
}
Robert Barrueco
  • 758
  • 5
  • 5