WCF service returns JSON with an anonymous object

For the simple API that I create, I use a couple of methods. First of all, this is not my first API that I create, but this is the first where I have a combination of WCF, json and anonymous objects.

I got the following interface for the WCF API:

[ServiceContract] public interface IAPI { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] ServiceResponse Authenticate(string username, string hashedPassword); [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] ServiceResponse GetProducts(); } 

All this is quite simple, only 2 methods that I want to have. The ServiceResponse class that you see there is also very simple, but it causes problems that I think:

 [DataContract] public class ServiceResponse { [DataMember] public ResponseCode Status { get; set; } [DataMember] public object Value { get; set; } } 

I created this class, so I can always send Status (Simple int enum) and an object, such as a string or a list of objects.

I created a small script using jQuery to test my service, and the Authenticate method works as it should, but this method returns a ServiceResponse object, just 0 in the Status field. The Value field is empty here.

The GetProducts method is the one where it gets complicated, an array of anonymous objects, for example:

 public ServiceResponse GetProducts() { DataClassesDataContext db = new DataClassesDataContext(); var results = from p in db.Products where p.UserID == 1 select new { ID = p.ID, Name = p.DecryptedName }; return ServiceResponse.OK(results.ToArray()); } 

I use an anonymous type object here because I don't want to create proxy classes for all the classes that I want to use in the API.

When I try this code, with my simple jQuery script, FireBug tells me that my request was aborted. I think this is due to a 500 error or something like that. When I set a breakpoint in my GetProducts method, it hits 7 times before Firefox says that it is interrupted and the script is fully running.

Here is an example jQuery script that I use to test my WCF service:

 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "/Handlers/API.svc/GetProducts", data: '{}', dataType: "json", success: function (response) { var test = inspect(response, 5); $("#output").html(test); }, error: function (message) { $("#output").html(inspect(message, 5)); } }); 

The inspect method that you see there is a small JS script that shows me the contents of an object.

I tried to do the following:

  • Replace anonymous objects with objects of type Product , but this did not succeed in the same way.
  • Return a List<T> instead of an array, but no luck there either.
  • Returning without binding to the Value property, it worked
  • By changing the Value property to dynamic , this also failed

I would really like it if I could use WCF to return some array of anonymous objects, because it allows me to create 30+ proxy classes.

NOTE The Product class is the class generated by LINQ. I use C # 4.0 for this.

+4
source share
2 answers

I may be mistaken, but I believe that WCF is quite strict and will not allow you to return an object the way you are trying. If you know that the data will be an array every time you can change your definition to reflect this (but it doesn't seem to work in your case). Otherwise, you may want to change your signature to a simple string and transfer the data to JSON first before returning it.

+2
source

Short answer: you cannot. Instead, you should use web services or, as you said, use a proxy class.

0
source

Source: https://habr.com/ru/post/1383982/


All Articles