青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

huangyi5209

  C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
  0 Posts :: 13 Stories :: 0 Comments :: 0 Trackbacks

常用鏈接

留言簿

我參與的團隊

搜索

  •  

最新評論

Sunday, October 10, 2010

Passing a JSON array to a WCF service

While the following information is available on the Internet, I had to piece it together from various sources, so for my own documentation and hopefully to help someone else, here is a simple (I hope) example of passing a JSON array to a WCF service, all in one place. It can be easily generalized to passing any JSON-serialized object to WCF, but that is better documented than arrays.

Note: This sample uses jQuery and Douglas Crockford’s JSON2. It also uses some features of HTML 5, including placeholder text, autofocus fields and offline storage, but these aren’t essential for the solution.

First, let’s look at the HTML, because it’s probably the simplest part of the solution:

index.html

<!DOCTYPE html>

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link rel="stylesheet" href="styles/branding.css" />

    <title>Send Array to WCF Service</title>

</head>

<body>

    <div id="content">

        <h3>User</h3>

        <form id="demographics_form">

            <span>Last Name: </span>

            <input id="last_name" name="last_name" type="text" placeholder="Last name" autofocus required />

            <br />

            <span>First Name: </span>

            <input id="first_name" name="first_name" type="text" placeholder="First name" />

            <br />

            <span>Email: </span>

            <input id="email_address" name="email_address" type="email" placeholder="someone@somewhere.com" />

            <br />

            <input id="save_button" type="button" value="Save" />

        </form>

    </div>

    <p id="app_status">

    </p>

    <script src="scripts/json2.js" type="text/ecmascript"></script>

    <script src="scripts/jquery-1.4.2.min.js" type="text/ecmascript"></script>

    <script src="scripts/main.js" type="text/ecmascript"></script>

</body>

</html>

Pretty much a simple form with three fields and a button. Pay attention to the input elements name attributes – we will be using those in various places. Note that the form doesn’t have action or method attributes. We will assign an event handler to the button in a bit. There is also a p element with an id of app_status which will be used to display status messages.

With that out of the way, let’s look at the JavaScript:

main.js

var UserApp = {

    docReady: function () {

        UserApp.loadForm();

        $("#save_button").bind("click", UserApp.sendFormData);

    },

 

    // Load form from saved data. Presumes that each input element's id is the same as its name.

    loadForm: function () {

        var formDataString = localStorage.getItem("user_data");

 

        if (formDataString) {

            var formData = JSON.parse(formDataString);

 

            for (var i in formData) {

                var fieldId = "#" + formData[i].name;

                $(fieldId).val(formData[i].value);

            }

        }

    },

 

    // Save data from a form. Presumes there is only one form element on a page.

    saveFormData: function () {

        var formData = $("form").serializeArray();

 

        localStorage.setItem("user_data", JSON.stringify(formData));

        return { nvps: formData };

    },

 

    sendFormData: function () {

        var formData = UserApp.saveFormData();

        $.ajax({

            type: "POST",

            url: "MyProject/UserSvc.svc/SaveUserData",

            data: JSON.stringify(formData),

            contentType: "application/json; charset=utf-8",

            dataType: "json",

            success: function (data) {

                if (data && data.d && data.d.Message) {

                    $("#app_status").html(data.d.Message);

                }

            },

            error: function (req, status, error) {

                if (req && req.responseText) {

                    var ex = JSON.parse(req.responseText);

 

                    if (ex && ex.Message) {

                        $("#app_status").html(ex.Message);

                    }

                    else {

                        $("#app_status").html("An error occurred saving the loan application data.");

                    }

                }

            }

        });

    }

};

 

$(document).ready(UserApp.docReady);

On the document becoming ready we execute the docReady function. It calls the loadForm function which loads the form values from local storage (if they exist), and also attaches the sendFormData function as the event handler for the “Save” button. We are storing the data in local storage using “user_data” as a key and a JSON-serialized array for the form elements (we will see how we create that array in the SaveFormData function). After parsing the JSON array we iterate over it to fill in the form input fields.

The saveFormData function is the opposite of the loadForm function in that it serializes the form’s input values using jQuery’s serializeArray function, which is one of the reasons I chose this approach. It does one other thing, which is to wrap the (unserialized) array in an object with a single member, nvps (which stands for “name-value pairs”), that holds the array, and returns this object. Again, we’ll see why later. To be clear, we are changing the bare array, which WCF will not accept as a method parameter:

[ { name: “last_name”, value: “Smith”}, {name: “first_name”, value: “John”}, {name: “email_address”, value: “john.smith@xyz.com”} ]

…to a wrapped object, which WCF will take:

{ nvps:  [ { name: “last_name”, value: “Smith”}, {name: “first_name”, value: “John”}, {name: “email_address”, value: “john.smith@xyz.com”} ] }

Finally there is the sendFormData function, the event handler for the “Save” button. It consists of a single AJAX call to the WCF service, sending up the JSON-serialized wrapped object as the sole parameter for the SaveUserData method, and setting the status on the HTML page based on the success or failure of that call. Two things to note about that. First, on success the data coming down (a simple object with a single public property in it called Message) will be wrapped within a JavaScript object called data in a member called d. So any access to data returned on success is via data.d.xyz, in our case data.d.Message. This use of data.d as a (successful) response wrapper appears to be hold for all JSON-enabled WCF services.

Second, if there was a server-side exception it will come down to the error callback as the JSON-serialized responseText member of the (XMLHttpRequest) request parameter. After parsing the JSON you can get to various exception members, although in our case we are just interested in the exception’s Message member. When debugging both success and failures it is very helpful to use Fiddler or similar tools to see what is getting passed back and forth with each HTTP request and response.

Now it’s time to see the server-side implementation. Let’s look at the WCF interface definition first:

IUserSvc.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

 

namespace MyProject

{

    [ServiceContract]

    public interface IUserSvc

    {

        [OperationContract]

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

        ResultMessage SaveUserData(NameValuePairs nvps);

    }

 

    [DataContract]

    public class NameValuePair

    {

        [DataMember]

        public string name { get; set; }

        [DataMember]

        public string value { get; set; }

    }

 

    [DataContract]

    public class ResultMessage

    {

        [DataMember]

        public string Message { get; set; }

    }

 

    [CollectionDataContract(Namespace = "")]

    public class NameValuePairs : List<NameValuePair>

    {

    }

}

The interface is pretty simple. There is a single SaveUserData method which takes in a collection of name-value pairs and returns a simple object that holds a result message. There are three things to notice here, and all are important. The first are the declarative attributes decorating the method. The following is where the real wiring for receiving and responding in JSON happens:

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

The Method can be either POST or GET (as long as your JavaScript code sends the request in the appropriate format). The BodyStyle and ResponseFormat parameters are required to get JSON in and out of the method. Any attempt to use a different BodyStyle (such as WebMessageBodyStyle.Bare) will result in a run-time exception because of settings in the web.config file, which we’ll see later.

Second, the parameter name to the method is nvps. Note this is the same name as the single member returned in the wrapper object on the array of form input field values in the JavaScript saveFormData function. This is some of the “magic” you just have to trust, but as far as I can tell, the parameter name to the WCF method must match the member name in the JavaScript wrapper object. In other words, it appears that when dealing with JSON, WCF is using name-matching between the JSON-serialized names and names in the managed code, probably via reflection.

Finally, while you don’t see it explicitly in the JavaScript code above, the jQuery serializeArray function returns an array of objects, one object for each input element in the form. Each object in the array contains two members - name, whose value matches the name attribute on the input element, and value, which contains the actual field’s value. Note that the NameValuePair class in the interface also uses name and value as member names. Again, these must match (as far as I can tell) the JavaScript member names to make all this work.

Here is the WCF implementation:

UserSvc.svc.cs

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Activation;

using System.ServiceModel.Web;

using System.Text;

 

namespace MyProject

{

    [AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    public class UserSvc : IUserSvc

    {

        public ResultMessage SaveUserData(NameValuePairs nvps)

        {

            var columns = new StringBuilder();

            var parameters = new StringBuilder();

            var rm = new ResultMessage() { Message = "User saved successfully." };

 

            using (var conn = new SqlConnection(Properties.Settings.Default.AppDb))

            using (var cmd = new SqlCommand() { CommandType = CommandType.Text, Connection = conn })

            {

                var paramCollection = cmd.Parameters;

                var rows = 0;

 

                try

                {

                    if (nvps == null || nvps.Count == 0)

                    {

                        throw new ArgumentNullException("nvps");

                    }

 

                    foreach (NameValuePair nvp in nvps)

                    {

                        columns.Append(nvp.name).Append(",");

                        parameters.Append("@").Append(nvp.name).Append(",");

                        paramCollection.Add(new SqlParameter("@" + nvp.name, nvp.value));

                    }

 

                    // KLUDGE: Remove trailing commas.

                    columns.Remove(columns.Length - 1, 1);

                    parameters.Remove(parameters.Length - 1, 1);

                    cmd.CommandText = String.Format(Properties.Settings.Default.InsertSql, columns.ToString(), parameters.ToString());

                    conn.Open();

                    rows = cmd.ExecuteNonQuery();

                    rm.Message = String.Format(Properties.Settings.Default.SuccessMessage, rows, cmd.CommandText);

                }

                catch (Exception ex)

                {

                    rm.Message = ex.Message;

                }

            }

 

            return rm;

        }

    }

}

There isn’t really much going on here. As noted, the method parameter nvps must match the member within the JSON-serialized wrapper object around the array being sent to the method. As the code iterates through the array (collection), note also the use of name and value, again, matching the object member namess that are within the array. The code basically builds a dynamic, parameterized SQL INSERT statement (see the web.config) that assumes the underlying table column names are the same as the input elements name attributes. Cheesy, I know, but it shows we’re actually “doing something” on the server. I don’t claim this to be production-quality! The interesting thing about the code as written, however, is that by simply changing the form to add more input elements and altering the table to add more columns, additional data could be accommodated with no change in the JavaScript or managed code, which gives a bit of flexibility that I like.

Finally, we have the web.config file:

web.config

<?xml version="1.0"?>

<configuration> 

  <configSections>

    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

      <section name="LoanAppDataRouter.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

    </sectionGroup>

  </configSections>

  <system.web>

    <compilation debug="true" targetFramework="4.0" />

  </system.web>

  <system.serviceModel>

    <bindings>

      <webHttpBinding>

        <binding name="MyBinding">

          <security mode="TransportCredentialOnly">

            <transport clientCredentialType="Windows"/>

          </security>

        </binding>

      </webHttpBinding>

    </bindings>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

    <services>

      <service name="MyProject.UserSvc" behaviorConfiguration="JsonBehavior">

        <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyBinding" behaviorConfiguration="JsonBehavior" contract="MyProject.IUserSvc">

          <identity>

            <dns value="mywebserver"/>

          </identity>

        </endpoint>

      </service>

    </services>

    <behaviors>

      <endpointBehaviors>

        <behavior name="JsonBehavior">

          <enableWebScript/>

        </behavior>

      </endpointBehaviors>

      <serviceBehaviors>

        <behavior name="JsonBehavior">

          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

<system.webServer>

    <modules runAllManagedModulesForAllRequests="true"/>

  </system.webServer>

<applicationSettings>

  <MyProject.Properties.Settings>

   <setting name="InsertSql" serializeAs="String">

    <value>INSERT INTO Users ({0}) VALUES({1})</value>

   </setting>

   <setting name="AppDb" serializeAs="String">

    <value>server=.;initial catalog=ApplicationDB;integrated security=sspi;</value>

   </setting>

   <setting name="SuccessMessage" serializeAs="String">

    <value>Inserted {0} rows using the following statement: {1}</value>

   </setting>

  </MyProject.Properties.Settings>

</applicationSettings>

</configuration>

I won’t go over all of this in detail, but simply call out the pieces that make the JSON work with WCF. You can also see the three properties being stored for access by the managed code at the end of the file. The important part is contained in the service element. Note first the binding attribute of the endpoint element is set to webHttpBinding. This is required for JSON to work. The endpoint also points via the behaviorConfiguration attribute to “JsonBehavior.” If you look at the “JsonBehavior” behavior element under endpointBehaviors, you will see an empty enableWebScript element. This is also required for JSON.

The serviceBehaviors element is basically set up to ease debugging. It enables GET requests (so you could do a URL-encoded request if you chose, even from a browser’s address bar) as well as turning on some debugging sugar. You won’t want that to go into production, however. The rest of the web.config is fairly self-evident. I’ve called out the things you need to pay attention to specifically for WCF services to interact with a JSON client, which could be JavaScript, or anything else that eats and excretes JSON (the list of which is fairly large).

Hopefully this post will help save someone some time by pulling all the required pieces into one place and calling out some of the more obscure aspects of JSON-to-WCF interaction. As always, I appreciate comments, corrections and questions.



http://ednortonengineeringsociety.blogspot.com/2010/10/passing-json-array-to-wcf-service.html
posted on 2011-04-21 22:14 huangyi5209 閱讀(1307) 評論(0)  編輯 收藏 引用 所屬分類: JavascriptC#

只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美电影免费观看大全| 伊人久久噜噜噜躁狠狠躁| 一区二区久久| 亚洲精品日韩在线观看| 亚洲欧洲精品一区二区精品久久久 | 国产欧美日韩综合| 国模精品一区二区三区| 伊人成年综合电影网| 亚洲高清不卡一区| 在线性视频日韩欧美| 午夜日韩在线观看| 免费观看成人网| 亚洲精品国精品久久99热一| 一本一道久久综合狠狠老精东影业 | 亚洲国产精品www| 99在线观看免费视频精品观看| 亚洲一级免费视频| 久久精品欧美| 欧美午夜不卡视频| 精品999日本| 亚洲性图久久| 欧美电影免费网站| 亚洲欧美精品一区| 欧美激情国产日韩| 国内精品久久久久久久果冻传媒| 亚洲精品网址在线观看| 欧美在线综合视频| 亚洲国产综合视频在线观看| 欧美一级片久久久久久久| 欧美激情成人在线视频| 黑人操亚洲美女惩罚| 亚洲宅男天堂在线观看无病毒| 另类亚洲自拍| 午夜精品福利一区二区三区av| 欧美福利视频| 在线成人亚洲| 久久狠狠婷婷| 亚洲小少妇裸体bbw| 欧美看片网站| 亚洲经典一区| 久久综合久色欧美综合狠狠| 亚洲午夜久久久久久久久电影网| 欧美11—12娇小xxxx| 国内成+人亚洲| 欧美一区二区日韩一区二区| 99pao成人国产永久免费视频| 久久综合色天天久久综合图片| 国产日本欧美一区二区| 欧美日韩国产在线看| 亚洲高清一区二| 久久久久国色av免费观看性色| 这里只有精品在线播放| 欧美日韩不卡在线| 亚洲精品美女久久7777777| 久久香蕉国产线看观看av| 午夜日韩在线| 国产日韩视频一区二区三区| 香蕉国产精品偷在线观看不卡| 亚洲精品一线二线三线无人区| 久久综合婷婷| 亚洲高清资源| 91久久香蕉国产日韩欧美9色| 欧美jizzhd精品欧美喷水| 影音先锋中文字幕一区| 免费观看成人| 欧美大片在线观看一区| 亚洲七七久久综合桃花剧情介绍| 欧美电影免费观看高清| 欧美成在线观看| 99在线精品观看| 一本色道久久加勒比精品| 国产精品久久久久9999高清| 亚洲欧美美女| 欧美一区二区精美| 国产一区日韩一区| 麻豆av福利av久久av| 欧美jizzhd精品欧美巨大免费| 99视频超级精品| 一区二区三区高清| 亚洲视频在线免费观看| 国产一区视频在线观看免费| 欧美成人中文| 欧美丝袜一区二区| 久久久久久久久久码影片| 狂野欧美激情性xxxx欧美| 在线综合+亚洲+欧美中文字幕| 亚洲香蕉伊综合在人在线视看| 国产一区二区三区在线观看免费视频| 久久先锋影音| 欧美三级精品| 麻豆av一区二区三区久久| 欧美日本中文| 玖玖国产精品视频| 欧美日韩情趣电影| 六月婷婷久久| 国产精品系列在线播放| 亚洲成人在线视频播放| 国产毛片精品国产一区二区三区| 欧美一区二区精美| 免费成人毛片| 欧美一区影院| 欧美成人午夜| 久久精品官网| 欧美日韩成人综合在线一区二区 | 久久久噜噜噜久久中文字免| 欧美成人激情视频免费观看| 午夜亚洲激情| 欧美国产精品专区| 久久久激情视频| 91久久极品少妇xxxxⅹ软件| 国产精品视频一二| 欧美激情视频一区二区三区在线播放| 国产精品国产三级国产| 欧美www在线| 国产一区二区中文| 在线亚洲欧美视频| 亚洲九九爱视频| 久久久久这里只有精品| 久久国产精品一区二区三区四区| 欧美精品日韩| 亚洲第一页自拍| 亚洲电影免费观看高清完整版在线观看 | 国产一级揄自揄精品视频| 亚洲欧洲在线看| 亚洲狠狠丁香婷婷综合久久久| 欧美一级视频精品观看| 午夜宅男久久久| 国产精品久久二区| 一本色道久久加勒比88综合| 在线性视频日韩欧美| 欧美日本视频在线| 亚洲日韩欧美视频| 日韩午夜黄色| 欧美日韩三级视频| 99人久久精品视频最新地址| 夜夜嗨av一区二区三区网站四季av| 免费在线一区二区| 亚洲国产精品久久久久久女王| 亚洲三级影片| 欧美日韩视频一区二区| 一本色道88久久加勒比精品| 亚洲视频成人| 国产精品人人做人人爽人人添| 亚洲性图久久| 亚洲欧美日韩精品久久| 国产欧美精品一区二区三区介绍| 亚洲欧美日韩另类精品一区二区三区| 久久www免费人成看片高清| 国产一区二区精品丝袜| 久久一日本道色综合久久| 亚洲第一黄网| 亚洲在线视频网站| 国产在线日韩| 久久亚洲视频| 亚洲精品免费电影| 欧美一区二区三区精品| 国内外成人免费视频| 久久精品国产99国产精品澳门| 欧美福利视频在线| 亚洲一二三四区| 国模精品一区二区三区| 欧美国产一区二区在线观看| 正在播放亚洲| 免费亚洲一区二区| 亚洲一区国产精品| 狠狠操狠狠色综合网| 欧美噜噜久久久xxx| 性久久久久久| 亚洲精品在线视频观看| 久久精品国产2020观看福利| 91久久精品国产91性色| 欧美亚洲综合另类| 欧美成人激情视频| 亚洲在线播放电影| 亚洲电影第三页| 欧美日韩一区二区三区四区在线观看 | 欧美大片在线影院| 亚洲视频在线观看免费| 国语自产精品视频在线看8查询8| 美国成人直播| 香港久久久电影| 9色精品在线| 欧美成人激情在线| 欧美一区二区视频在线| 亚洲每日更新| 伊人夜夜躁av伊人久久| 欧美日韩一视频区二区| 美女性感视频久久久| 欧美一区成人| 亚洲女人天堂av| 99re成人精品视频| 亚洲高清三级视频| 久久一区二区精品| 午夜欧美大片免费观看| 99香蕉国产精品偷在线观看| 在线看日韩av| 樱桃国产成人精品视频| 国产区日韩欧美| 国产欧美成人| 国产欧美精品一区二区色综合|