WCF: Returning objects in JSON format

Source: Internet
Author: User

1. Build a WCF Service first

Build a ServiceContract interface 1 [ServiceContract]

Public interface Ijsonwcfservice {//<summary>//Getjsonresult//     </summary>     // <param name= "name" ></param>//     <param name= "Address" ></param>//     <param name= " Phone "></param>     ///<remarks>//For JSON serialization, add attributes     ///[WebInvoke (Responseformat = Webmessageformat.json, bodystyle = webmessagebodystyle.wrapped)]//</remarks>//     <returns> </returns>     [OperationContract]     [WebInvoke (Responseformat = Webmessageformat.json, bodystyle = webmessagebodystyle.wrapped)]     jsonresult getjsonresult (string name, string address, String phone);}

Implement this interface

public class Jsonwcfservice:ijsonwcfservice {#region Ijsonwcfservice members///<summary>//     Implement the interface//</summary>//     <param name= "name" >Name</param>//     < param name= "Address" >Address</param>//     <param name= "Phone" >PhoneNumber</param>//     <returns>JsonResult</returns> public     Jsonresult getjsonresult (string name, string address, string Phone)     {         Jsonresult result = new Jsonresult (name, address, phone);         return result;     }     #endregion}

A return class of DataContract

[DataContract] public class Jsonresult {//<summary>//Construct//</summary> public Jso Nresult (string name, string address, String phone) {_name = string.         Format ("name:{0}", Name); _address = string.         Format ("address:{0}", Address); _PhoneNumber = string.     Format ("phonenubmer:{0}", phone);     } private string _name; <summary>///name//</summary> [DataMember] public string Name {get {Retu RN _name;     } set {_name = value;}     } private string _address; <summary>//address//</summary> [DataMember] public string Address {get         {return _address;}     set {_address = value;}     } private string _PhoneNumber;         <summary>//PhoneNumber//</summary> [DataMember] public string PhoneNumber {         get {return _phonenumber;} set {_PhoneNumber = value; }     }

2, in order to implement JSON serialization settings, we also have to add a webcontenttypemapper

(This class will eventually be used in the service's configuration file)

Using System.ServiceModel.Channels;  namespace Microsoft.Ajax.Samples {//<summary>//Jsoncontenttypemapper//     for use in configuration < Webmessageencoding webcontenttypemappertype= "Microsoft.Ajax.Samples.JsonContentTypeMapper, Jsoncontenttypemapper , version=1.0.0.0, Culture=neutral, publickeytoken=null ">///     </summary> public     class Jsoncontenttypemapper:webcontenttypemapper     {public         override Webcontentformat Getmessageformatforcontenttype (String contentType)         {             if (ContentType = = "Text/javascript")             {                 return webcontentformat.json;             }             else             {                 return webcontentformat.default;             }         }}}     

3, Add Svc file, easy to publish service

The Svc file is actually a very simple file, the following is the contents of the Svc file, you can add this file to the root of the Web site project, or it can be a subdirectory. There is not much demand for this.

<%@ ServiceHost language= "C #" debug= "true" service= "Jsonwcfservice"%>

4. Add the Web. config file

A considerable part of the knowledge in Wcfservice is about configuration

<?xml version= "1.0"?> <configuration> <appSettings/> <connectionStrings/> <system.web&gt    ; </system.web> <system.serviceModel> <behaviors> <endpointbehaviors > <behav IOR name= "Jsonwcfbehavior" > <webHttp/> </behavior> </endpointBehaviors> &            lt;/behaviors> <bindings> <customBinding> <binding name= "Jsonmapper" > <!--Here the configuration is quite important, using the Jsoncontenttypemapper class we wrote, the contract return value type is json--> <webmessageencoding webcontentty Pemappertype= "Microsoft.Ajax.Samples.JsonContentTypeMapper, Jsoncontenttypemapper, version=1.0.0.0, culture= Neutral, publickeytoken=null "> </webMessageEncoding> 

WCF: Returning objects in JSON format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.