SharePoint 2013創建WCF REST Service



SharePoint 2013為
開發者提供了豐富的REST API,方便了我們在客戶端操作List中的數據。當然我們也可以在SharePoint 2013中創建自定義的REST Service,比如通過REST Service去操作數據庫。本篇博客將介紹怎樣在SharePoint 2013創建WCF REST Service。

SharePoint 中 創建WCF Service

因為無法在SharePoint 2013 Project中添加WCF Service Template,所以預先創建一個WCF Service Application , 在把契約接口和svc服務拖到SharePoint Project中。所以你需要以下步驟:

  • 1.創建 WCF Service Application
  • 2.在SharePoint Project中創建SharePoint Mapped Folder ISAPI,因為SharePoint 2013中能訪問的服務(.svc)存在:C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\isapi 文件夾中,對應於IIS中虛擬目錄_vti_bin。
  • 3.把WCF Service Application的svc拖到 ISAPI文件夾中,如下所示:

  • 4.修改Namespace,並添加程序集引用,如下所示:

  • 6.修改svc
<%@ ServiceHost Language="C#" Debug="true" Service="Eyes.CustomRestService.Service1,Eyes.CustomRestService,Version=1.0.0.0,Culture=neutral,PublicKeyToken= bf65dbaa17f24124" CodeBehind="Service1.svc.cs" %>
  • 7.為了測試WCF Service是否成功部署,需要實現契約接口:

 創建用於測試的契約接口:

  [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebGet(ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped,UriTemplate="GetData/{value}")]
        string GetData(string value);


        // TODO: Add your service operations here
    }

 接着實現契約接口,也就是我們的服務:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
  • 8.最后,修改Config文件
<system.serviceModel>
       <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
       <behaviors>
        <serviceBehaviors>
            <behavior name="Service1ServiceBehavior">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="jsonBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
        </behaviors>
        <services>
            <service name="Eyes.CustomRestService.Service1" behaviorConfiguration="Service1ServiceBehavior">
                <endpoint address="" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="Eyes.CustomRestService.IService1" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
  </system.serviceModel>
  • 9.客戶端訪問REST Service

小結

SharePoint 2013的REST API 十分強大,有時間再分享SharePoint 2013 REST API方面的知識。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM