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:
Try removing Newtonsoft.Json from your references and re-add your service reference.
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
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') {
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.