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

前端 未结 4 1858
你的背包
你的背包 2021-01-11 09:05

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:

相关标签:
4条回答
  • 2021-01-11 09:39

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

    0 讨论(0)
  • 2021-01-11 09:49

    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

    0 讨论(0)
  • 2021-01-11 09:54

    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') {
    
    0 讨论(0)
  • 2021-01-11 10:03

    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.

    0 讨论(0)
提交回复
热议问题