WCF: 以Json格式返回對象

來源:互聯網
上載者:User

標籤:get   ring   copy   point   mapper   調用   項目   簡單   mod   

1、先建一個WCF Service

建一個ServiceContract介面 1 [ServiceContract]

 

public interface IJsonWCFService {     /// <summary>     /// GetJsonResult     /// </summary>     /// <param name="name"></param>     /// <param name="address"></param>     /// <param name="phone"></param>     /// <remarks>     /// 為實現Json序列化,添加屬性     /// [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); }

 

實現這個介面

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 }

一個返回的DataContract類

[DataContract] public class JsonResult {     /// <summary>     /// Construct     /// </summary>     public JsonResult(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 { return _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、為實現Json序列化設定,我們還得添加一個WebContentTypeMapper

(此類最終會用在Service的設定檔中)

using System.ServiceModel.Channels;  namespace Microsoft.Ajax.Samples {     /// <summary>     /// JsonContentTypeMapper     /// 用在配置中<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、添加svc檔案,便於發布Service

svc檔案其實是十分簡單的一個檔案,以下是SVC檔案中的內容,可以將此檔案添加在網站項目的根目錄,也可以是一個子目錄。對此沒有太多的要求。

 <%@ ServiceHost Language="C#" Debug="true" Service="JsonWCFService" %>

4、添加web.config檔案

WCFService中相當一部分知識是關於配置的

 

<?xml version="1.0"?> <configuration>   <appSettings/>   <connectionStrings/>   <system.web>    </system.web>   <system.serviceModel>     <behaviors>       <endpointBehaviors >         <behavior name="jsonWcfBehavior">           <webHttp/>         </behavior>       </endpointBehaviors>     </behaviors>     <bindings>             <customBinding>                 <binding name="JsonMapper">           <!--此處配置相當重要,使用了我們編寫的JsonContentTypeMapper類,約定傳回值類型是Json-->           <webMessageEncoding webContentTypeMapperType="Microsoft.Ajax.Samples.JsonContentTypeMapper, JsonContentTypeMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">           </webMessageEncoding>           <httpTransport manualAddressing="true"/>         </binding>       </customBinding>     </bindings>     <services>             <service name="JsonWCFService" >         <!--注意此處的endpoint配置,address和contract兩個屬性,在用戶端Js調用時會用的上-->         <endpoint address="ajaxEndpoint" behaviorConfiguration="jsonWcfBehavior"                   binding="customBinding"                   bindingConfiguration="JsonMapper" contract="IJsonWCFService">         </endpoint>       </service>     </services>   </system.serviceModel> </configuration>

WCF: 以Json格式返回對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.