Fork me on GitHub

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方面的知识。

 

posted @   木宛哥说编程  阅读(2411)  评论(3编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
multifunction lasers
访问人数
点击右上角即可分享
微信分享提示