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

泪湿孤枕 提交于 2019-12-19 05:11:10

问题


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?


回答1:


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




回答2:


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




回答3:


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') {



回答4:


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.



来源:https://stackoverflow.com/questions/11542371/error-adding-service-reference-type-is-a-recursive-collection-data-contract-whi

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