在上一篇(WCF学习之旅—实现REST服务(二十二))作品中概括介绍了弹指间RestFul与WCF协理RestFul所提供的主意,本文讲解一下咋样成立一个支撑REST的WCF服务端程序。

 

四、在WCF中创建REST服务

  1. 在SCF.Contracts
    在创建一个服务契约IBookRestService.

      
这里提供多少个点子,分别使用GET和POST模式访问。

      
大家能够看到,与常见WCF服务契约不同的是,需要额外用WebGet或者WebInvoke指定REST访问的主意。另外还要指定信息包装样式和消息格式,默认的音信请求和响应格式为XML,若拔取JSON需要显式注解。 

       
UriTemplate用来将艺术映射到实际的Uri上,但万一不指定映射,将映射到默认的Uri。比如动用Get访问的GetBook方法,默认映射是:/
GetBook?BookId={BookId}。

        
在编写代码的历程中,会并发如下图中1的谬误,请引用下图2处的

         
XML 1

 

using SCF.Model;

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks; 

namespace SCF.Contracts
{

    [DataContractFormat]
    [ServiceContract]
    public interface IBookRestService
    {

        //[WebGet(UriTemplate = "/Books/Get/{BookId}/{categroy}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
        [WebGet(UriTemplate = "/Books/Get/{BookId}", BodyStyle = WebMessageBodyStyle.Bare)]
          [OperationContract]
          List<Books> GetBook(string BookId);

        //[WebInvoke(Method = "POST", UriTemplate = "/Books/Create", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        [WebInvoke(Method = "POST", UriTemplate = "/Books/Add", BodyStyle = WebMessageBodyStyle.Bare)]
         [OperationContract]
         Result AddBook(Books book);

     }


}

 

 

     2.
在品种SCF.Model中开创一个实体对象Books与一个回到对象Result,用作多少传输的载体,下边是Books.cs的始末

namespace SCF.Model
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;
using System.Runtime.Serialization;
 ///DataContract 数据契约:服务端和客户端之间要传送的自定义数据类型  
    [DataContract(Namespace = "http://tempuri.org/")]
    public partial class Books

{

 /// <summary>  
/// 在数据传送过程中,只有成员变量可以被传送而成员方法不可以。  
/// 并且只有当成员变量加上DataMember时才可以被序列进行数据传输,  
/// 如果不加DataMember,客户端将无法获得该属性的任何信息  
/// </summary>  

        [DataMember]
        [Key]
        public int BookID { get; set; }

        [DataMember]
        [Required]
        public string Category { get; set; }

        [DataMember]
        [Required]
        public string Name { get; set; }

        [DataMember]
        public int Numberofcopies { get; set; }

        [DataMember]
        public int AuthorID { get; set; }

        [DataMember]
        public decimal Price { get; set; }
        [DataMember]
        public DateTime PublishDate { get; set; }

        [StringLength(5)]
        [DataMember]
        public string Rating { get; set; }
    }

}










using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;


namespace SCF.Model
{

    [DataContract(Namespace = "http://tempuri.org/")]
    public class Result
    {

        [DataMember]
        public string Message
        { get; set; }
    }

}

 

 

     3.
在SCF.Wcf瑟维斯(Service)(Service)项目中落实在SCF.Contracts项目中定义的劳动契约。此地最简易的实现GetBook和AddBook六个方法的逻辑。

using SCF.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using SCF.Model;
using SCF.Common;


namespace SCF.WcfService
{

    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“BookRestService”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 BookRestService.svc 或 BookRestService.svc.cs,然后开始调试。
    public class BookRestService : IBookRestService
    { 

        Entities db = new Entities();
        public Result AddBook(Books book)
        {

            Result result = new Result();
            try
            {               

                db.Books.Add(book);
                db.SaveChanges();
                result.Message = string.Format("书名:{0} 已经添加!",book.Name);


            }
            catch (Exception ex)
            {
                result.Message =ex.Message;
            }

            return result;
        }



        public List<Books> GetBook(string BookId)
        {

            var cateLst = new List<string>();

            var cateQry = from d in db.Books
                          orderby d.Category
                          select d.Category;
            cateLst.AddRange(cateQry.Distinct()); 

            var books = from m in db.Books
                        select m;


            if (!String.IsNullOrEmpty(BookId))
            {
                books = books.Where(s => s.Name.Contains(BookId));
            }

            List<Books> list = null;
              list = books.ToList<Books>();
            return list;        

        } 

    }
}

 

  

   

4.
在安排文件在中布局大家的Rest服务,必须利用WebHttpBehavior对劳动的终结点进行布局。

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, 
Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>

  </entityFramework> 

  <system.serviceModel>  

    <bindings>
       <webHttpBinding>
         <binding name="RestWebBinding">
         </binding>
       </webHttpBinding>
     </bindings>


    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">

          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8888/BookService/metadata" />
          <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>

          <behavior name="RestServiceBehavior">
         </behavior>
      </serviceBehaviors>
       <endpointBehaviors>
         <behavior name="RestWebBehavior">
           <!--这里必须设置-->
           <webHttp />
         </behavior>
       </endpointBehaviors> 

    </behaviors> 

    <services>
      <service behaviorConfiguration="metadataBehavior" name="SCF.WcfService.BookService">
        <endpoint address="http://127.0.0.1:8888/BookService" binding="wsHttpBinding"
        contract="SCF.Contracts.IBookService" />
      </service>

      <service name="SCF.WcfService.BookRestService" behaviorConfiguration="RestServiceBehavior">
         <endpoint address="http://127.0.0.1:8888/" behaviorConfiguration="RestWebBehavior"
                   binding="webHttpBinding" bindingConfiguration="RestWebBinding" contract="SCF.Contracts.IBookRestService">
         </endpoint>
       </service>



    </services>
  </system.serviceModel> 

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <connectionStrings>


    <add name="Entities" connectionString="metadata=res://*/BookModel.csdl|res://*/BookModel.ssdl|res://*/BookModel.msl;
provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=Test;
integrated security=SSPI;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings> 

</configuration>

 

相关文章

网站地图xml地图