zoukankan      html  css  js  c++  java
  • WCF: 以Json格式返回对象

    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>
  • 相关阅读:
    [BZOJ 4034][HAOI2015]树上操作(欧拉序列+线段树)
    [BZOJ 4318]OSU!(期望dp)
    [Codeforces Round #146 (Div. 1) B]Let's Play Osu!(期望Dp)
    [Codeforces Round #261 (Div. 2) E]Pashmak and Graph(Dp)
    [Codeforces Round #301 (Div. 2) D]Bad Luck Island(概率Dp)
    [Codeforces Round #284 (Div. 1) B]Name That Tune(概率Dp)
    [UVALive 7143]Room Assignment(Dp)
    [BZOJ 1076][SCOI2008]奖励关(期望+状压Dp)
    【DBMS HKUST slides8】范式及分解算法 知识点总结
    【DBMS HKUST slides1~6】数据库管理系统 知识点总结
  • 原文地址:https://www.cnblogs.com/sjqq/p/6786444.html
Copyright © 2011-2022 走看看