Getting “Recursive collection data contract” when referencing a WCF service with a slightly complex method

心不动则不痛 提交于 2019-12-29 00:25:08

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12069176/getting-recursive-collection-data-contract-when-referencing-a-wcf-service-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!