【问题标题】:WCF Service Converting DataSet to Json [duplicate]WCF服务将数据集转换为Json [重复]
【发布时间】:2013-12-06 02:26:18
【问题描述】:

我创建了一个 WCF 服务,它接受并返回 json 数据。

我已经使用 Fiddler 对其进行了测试,它在 json 中正确返回字符串数组,但在 XML 中返回 System.Data.DataSet。

我是否天真地期望将数据集隐式转换为 json?

[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json, 
    Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
DataSet GetDataset()

【问题讨论】:

    标签: c# json wcf


    【解决方案1】:

    我实际上只是将我的 WCF 服务转换为使用来自 xml 的 json,(因此我可以使用 javascript 更轻松地调用它)。这一切都对我有用,如果你能从中收集到任何东西,我祝你好运。

    这是在我的界面中(它返回一个带有最新版本的字符串进行比较)

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

    然后我写了一个 JS 类来处理 WCF 调用

    //
    // 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;
    };
    

    这可能比你需要的多,要求是我可以取消呼叫(由于 win8 应用程序框架正在交换我下面的 html)。

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

    调用服务

        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
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 2014-05-02
      • 2019-09-21
      • 2018-08-19
      • 1970-01-01
      相关资源
      最近更新 更多