0

I've created a WCF service which accepts and returns json data.

I have tested it using Fiddler and it correctly returns string arrays in json, but returns a System.Data.DataSet in XML.

Am I being naive expecting an implicit conversion of the dataset to json?

[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json, 
    Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
DataSet GetDataset()
John
  • 5,672
  • 7
  • 34
  • 52

1 Answers1

0

I literally just converted my WCF service to use json from xml, (so I could use javascript to call it more easily). This all worked for me, if you can glean anything from it, I wish you well.

This is in my interface (it returns a string with the latest version to comparison)

[OperationContract]
[FaultContract(typeof(ExceptionFault))]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetLatestVersion();

Then I wrote a JS class to handle WCF calls

//
// Handles WCF calls and returns
//
function WCF(url) {

    // Makes a call against a WCF service
    //
    // id: is a unique key to specify this service
    //
    // action: is the name of the method in the WCF service to call
    //
    // arguments: is a json object of the named argument values
    //  Example:
    //   { firstname: "John", lastname: "Albert" }
    //
    // completed: a function that will be called on successful completion of the call
    //   the result will be deserialized into a json object
    //  Format:
    //   function(jsonReturnObject)
    //
    // errored: a function that will be called on unsuccessful completion of the call
    //   the resulting status and error text will be sent
    //  Format:
    //   function(status, statusText)
    this.call = function (id, action, arguments, completed, errored) {

        // save key
        this.keys[id] = true;

        // Create HTTP request
        var xmlHttp;
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            alert("This sample only works in browsers with AJAX support");
        }

        var self = this;

        // Create result handler 
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                    // Success!
                    if (typeof completed == 'function') {
                        if (self.keys[id]) {
                            completed(JSON.parse(xmlHttp.responseText)[action + "Result"]);
                        }
                    }
                } else {
                    // Failed
                    if (typeof errored == 'function') {
                        if (self.keys[id]) {
                            errored(xmlHttp.status, xmlHttp.statusText);
                        }
                    }
                }

                // on termination wipe the key
                delete self.keys[id];
            }
        }

        // Build the operation URL
        var requestUrl = this.url + "\\" + action;

        // Build the body of the JSON message
        var body = JSON.stringify(arguments);

        // Send the HTTP request
        xmlHttp.open("POST", requestUrl, true);
        xmlHttp.setRequestHeader("Content-type", "application/json");
        xmlHttp.send(body);
    };

    // cancelds a specific existing call
    this.cancel = function (id) {
        if (this.keys[id] !== undefined) {
            this.keys[id] = false;
        }
    }

    //
    // CONSTRUCTOR
    //
    this.url = url;
    this.keys = new Object();

    return this;
};

This may be more than you need, the requirements were that I could cancel a call (due to win8 app framework was swapping out my html under me).

    MyService = new WCF('http://localhost:12345/MyService.svc');

to make a call into the service

    HealthAppService.call('unique_id', 'GetLatestVersion', {}, function (response) {

        // If server version is newer, display upgrade notification
        var bits = response.split('.');
        var ver = new Array(version.major, version.minor, version.revision, version.build);
        var needUpdate = false;
        for (var i = 0; i < 3; i++) {
            if (ver[i] > parseInt[i]) break;
            if (ver[i] < parseInt[i]) { needUpdate = true; break; }
        }

        if (needUpdate) {
            // handle update
        }
    }, function (code, message) {
        // Error
    });
ohmusama
  • 4,159
  • 5
  • 24
  • 44