Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:08:01+00:00 2026-05-27T05:08:01+00:00

For a simple API I am building I am using a couple of techniques.

  • 0

For a simple API I am building I am using a couple of techniques. First of all, this is not my first API I am creating, but it is the first where I have 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();
}

It’s all pretty simple, just 2 methods I want to have working. The ServiceResponse class you see there is also really simple, but causing the problems I think:

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

    [DataMember]
    public object Value { get; set; }
}

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

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

The GetProducts method is the one where it gets tricky, an array of anonymous objects like this:

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 am using an anonymous type object here, because I do not want to create proxy classes for all the classes I want to use in the API.

When I try out this code, with my simple jQuery script, FireBug tells me that my request has been aborted. I guess this is because of an Error 500 or something. When I put a breakpoint in my GetProducts method, it gets hit 7 times before Firefox says it is aborted and the script is fully run.

Here is the jQuery script I use to test out 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 you see in there, is just a small JS script that shows me the contents of an object.

I tried the following things to get it moving:

  • Replace the anonymous objects with objects of the type Product, but this failed in the same way
  • Return a List<T> instead of an array, but no luck there either.
  • Returning without anything assigned to the Value property, this worked
  • Changing the property Value to the dynamic type, this failed also

I would love it if I could use WCF to return some sort of array of anonymous objects, because that saves me creating 30+ proxy classes.

NOTE: The Product class is a class generated by LINQ. I am using C# 4.0 for this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:08:01+00:00Added an answer on May 27, 2026 at 5:08 am

    I may be wrong, but I believe that WCF is fairly strict and will not allow you to return an object the way you are trying to. If you know that the data is going to be an array every time you could change your definition to reflect that (but it sounds like this is not going to work in your case). Otherwise, you might want to consider changing your signature to a simple string and serializing the data into JSON first before returning it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a simple appliacation using the Google Maps API and I'm trying to
I'm building a simple JSON API using Rails 3.2.1 and Jbuilder on Ruby 1.8.7
I'm looking at building a simple web app that will expose an API that
I'm creating a simple API that creates typed classes based on JSON data that
I am new to Restful concept and have to design a simple API for
I'm not sure what the best api for simple 2d graphics with Java is.
I am building a simple API that downloads files off a thirdparty CDN (Cloud
I am building my first chrome extension, for that I want to have some
Building an app using android.support.v4.app , targeting API level 8. Everything's working as planned
Building a sample ASP.NET MVC app. Using the Membership API for authentication. For whatever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.