WCF:为 SharePoint 2010 Business Connectivity Services 构建 WCF Web 服务(第 1 部分,共 4 部分)

转:http://msdn.microsoft.com/zh-cn/library/gg318615.aspx

摘要:通过此系列文章(共四部分)了解如何在 Microsoft SharePoint Foundation 2010 中对用户进行身份验证。创建可将其用作 Microsoft Business Connectivity Services (BCS) 中外部内容类型的 Windows Communication Foundation (WCF) Web 服务。

在 SharePoint Online 中提供

适用范围: Microsoft SharePoint Foundation 2010

供稿人: Eric White(该链接可能指向英文页面),Microsoft Corporation | Saji Varkey,Microsoft Corporation | Bin Zhang,Microsoft Corporation

目录

本文是介绍如何通过 Business Connectivity Services 创建和使用声明感知 Web 服务的系列文章的第一部分(共四部分)。查看该系列中的其他文章:

简介

本文演示如何使用 WCF 创建非常简单的 Web 服务。我在本文中介绍的 Web 服务有一个特征:尽管它非常简单,但可以用作 Business Connectivity Services 中的外部内容类型。此外,本文中提供的过程还介绍如何使用 Internet Information Services (IIS) 承载该 Web 服务。

该 Web 服务只包含两种方法:查找工具 方法(用于检索项目集合)和特定查找工具 方法(用于检索单个项目)。集合背后的数据是一个初始化列表。这个小型数据库的架构非常简单。它是一个由两个字段组成的平面表:一个整型字段 CustomerID 和一个字符串字段 CustomerNameCustomerID 是唯一 ID。下面的代码段演示如何初始化包含由 Web 服务提供服务的数据的列表。

return new List<Customer>()
{
    new Customer
    {
        CustomerID = 1,
        CustomerName = "Bob",
    },
    new Customer
    {
        CustomerID = 2,
        CustomerName = "Bill",
    },
    new Customer
    {
        CustomerID = 3,
        CustomerName = "Cheryl",
    },
};

在构建和配置该 Web 服务后,您可以使用 SharePoint Designer 2010 通过该服务创建外部内容类型,然后在 SharePoint 列表中查看数据。下图显示了 SharePoint 外部列表。



图 1. SharePoint 2010 外部列表

SharePoint 2010 外部列表

您可能已经知道,SharePoint Foundation 2010 和 SharePoint Server 2010 中的 Business Connectivity Services 是可读/写的。如果您提供其他方法来创建、更新和删除项目,则可以完全保留列表中的数据。我希望该 Web 服务尽可能简单。因此这是一个只读实现。我只关注安全性和身份标识问题。存储数据库的方式、存储数据库的位置或架构详细信息与本主题无关。

此处说明的过程针对 Windows Server 2008 R2 和 Windows Server 2008。您可以使用 Microsoft Visual Studio 2010 或 Visual Studio 2008 构建该 Web 服务。

您可以在运行 Internet Information Services 的任何开发计算机上构建、运行和测试本文介绍的 Web 服务。但是,如果您希望使用 Business Connectivity Services 作为外部内容类型连接到该 Web 服务,请在 SharePoint 2010 开发环境中生成此示例,以便 SharePoint Foundation 2010 或 SharePoint Server 2010 以及 Web 服务示例能够在同一台计算机上运行。可以将该 Web 服务置于其他服务器上,并将其用作外部内容类型。但在这种情况下,您必须取消安全设置,或者必须创建声明感知 Web 服务。

如前所述,我在此处介绍的过程演示如何在 IIS 下承载 Web 服务。这是此类 Web 服务的大多数实现者希望承载该服务的方式。作为 IIS 的服务承载它具有诸多好处,例如可以进行进程回收、监视进程运行状况以及基于消息的激活。

先决条件

此过程针对 Windows Server 2008 或 Windows Server 2008 R2。在这两种情况下,我都从装有经过修补的全新操作系统的服务器开始操作。接下来,我将安装必需角色和功能以便安装和运行 IIS。此外,本文介绍的过程还需要 .NET Framework 3.5.1 以及 Visual Studio 2008 或 Visual Studio 2010。

安装 Windows Communication Foundation (WCF) 和 Internet Information Services

使用服务器管理器安装 Web 服务器 (IIS) 角色和 .NET Framework 3.5.1 功能。

若要运行此示例,必需的唯一角色是 Web 服务器 (IIS)。下图显示了添加角色向导中的这一对话框。



图 2. 在添加角色向导中选择“Web 服务器”角色

在添加角色向导中选择 Web 服务器

添加 Web 服务器 (IIS) 角色后,向导会要求您选择“角色服务”。选择“应用程序开发”。下图显示了添加角色向导中的这一对话框。



图 3. 在添加角色向导中选择“应用程序开发”

在添加角色向导中选择应用程序开发

若要安装 .NET Framework,请添加“.NET Framework 3.5.1 功能”。下图显示了添加角色向导中的这一对话框。



图 4. 在添加角色向导中选择“.NET Framework 3.5.1 功能”

选择 .NET Framework 3.5.1

在 Windows Server 2008(非 R2)上安装此功能时,并在选择“.NET Framework 3.0 功能”后,选择“WCF 激活”。这是 Windows Server 2008 R2 中的默认选项。下图显示了添加角色向导中的这一对话框。



图 5. 选择“WCF 激活”

在 Windows Server 2008 中选择“WCF 激活”

必须同时安装 WCF 和 IIS,IIS 承载的 WCF 服务才能正常运行。安装 WCF(作为 .NET Framework 3.0 的一部分)和 IIS 的过程取决于您所使用的操作系统。如果要在除 Windows Server 2008 或 Windows Server 2008 R2 之外的操作系统上进行安装,请参阅 Microsoft .NET Framework 3.0 可再发行组件包以便下载并安装 .NET Framework 3.0。请参阅安装 IIS(该链接可能指向英文页面)

注册服务模型

要运行此示例,您必须注册该版本的 WCF 并在 IIS 元数据库根下更新脚本映射。

注册服务模型

  1. 启动 Visual Studio 命令提示符。以administrator身份运行。下图演示以管理员身份运行命令提示符。



    图 6. 以管理员身份运行 Visual Studio Command Prompt

    以管理员身份运行命令提示符

    依次单击“开始”、“所有程序”、“Visual Studio 2010”和“Visual Studio 工具”,右键单击“Visual Studio 命令提示”,然后单击“以管理员身份运行”

    对于 Visual Studio 2008,Visual Studio 命令提示符位于类似的位置。

  2. 将目录切换到以下位置:

    c:WindowsMicrosoft.NETFrameworkv3.0Windows Communication Foundation

  3. 在命令提示符中,输入:

    ServiceModelReg -i

    下图演示运行 ServiceModelReg。



    图 7. 运行 ServiceModelReg

    运行 ServiceModelReg
    Gg318615.note(zh-cn,office.14).gif注释:

    这不必是 Visual Studio 命令提示符。但是,下面的步骤需要 Visual Studio 命令提示符。因此,为了提高效率,我在此步骤中创建了一个。

更新脚本映射

更新脚本映射

  1. 在命令提示符中,输入:

    aspnet_regiis.exe -i

    下图显示了更新脚本映射。



    图 8. 更新脚本映射

    运行 aspnet_regiis

创建 Web 服务

创建 Web 服务

  1. 创建一个目录 C:MyWebService 来包含 Web 服务。如果您使用其他目录,则必须相应地更改这些过程。

  2. 启动 Visual Studio 2010(或 Visual Studio 2008)。

  3. 依次单击“文件”、“新建”和“项目”。对于安装的模板类别,请选择“WCF”。对于模板,请选择“WCF 服务应用程序”。找到“.NET Framework 3.5”。对于项目位置,请浏览到在步骤 1 中创建的目录。将此项目命名为“CustomersService”。不要为解决方案创建目录。下图显示了“新建项目”对话框的外观。



    图 9. “新建项目”对话框

    “新建项目”对话框
    Gg318615.note(zh-cn,office.14).gif注释:

    请记住将目标更改为 .NET Framework 3.5。默认情况下,Visual Studio 指向 .NET Framework 4,必须将其更改为 .NET Framework 3.5,此处呈现的过程才有效。

  4. 在项目中,将“IService1.cs”重命名为“ICustomers.cs”。Visual Studio 2010 可重命名对此项目中代码元素 IService1 的所有引用。单击“是”。实际上,我们将替换所有模块中的所有代码,因此无论单击“是”还是“否”都无影响。

  5. 在项目中,将“Service1.svc”重命名为“Customers.svc”。下图显示了重命名这些项目后解决方案资源管理器的外观。



    图 10. 重命名项目后的解决方案资源管理器

    重命名项后的解决方案资源管理器
  6. 将“Customers.svc”替换为以下标记行。右键单击“Customers.svc”,然后选择“查看标记”。从本文复制此标记,将其粘贴到 Visual Studio 中并保存。

    <%@ ServiceHost Language="C#" Debug="true" Service="CustomersService.Customers" CodeBehind="Customers.svc.cs" %>
    
  7. 将“Customers.svc.cs”替换为以下代码。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace CustomersService
    {
        public class Customers : ICustomers
        {
            // Finder
            public List<Customer> GetAllCustomers()
            {
                return new List<Customer>()
                {
                    new Customer
                    {
                        CustomerID = 1,
                        CustomerName = "Bob",
                    },
                    new Customer
                    {
                        CustomerID = 2,
                        CustomerName = "Bill",
                    },
                    new Customer
                    {
                        CustomerID = 3,
                        CustomerName = "Cheryl",
                    },
                };
            }
    
            // Specific finder
            public Customer GetCustomerByID(int CustomerID)
            {
                return GetAllCustomers().FirstOrDefault(c => c.CustomerID == CustomerID);
            }
        }
    }
    
  8. 将“ICustomers.cs”替换为以下代码。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    
    namespace CustomersService
    {
        [ServiceContract]
        public interface ICustomers
        {
            [OperationContract] //finder
            List<Customer> GetAllCustomers();
    
            [OperationContract] //specificFinder
            Customer GetCustomerByID(int CustomerID);
        }
    
        [DataContract]
        public class Customer
        {
            [DataMember]
            public int CustomerID { get; set; }
    
            [DataMember]
            public string CustomerName { get; set; }
        }
    }
    
  9. 将“Web.config”替换为以下标记。在解决方案资源管理器中,右键单击“Web.config”并选择“编辑”。复制、粘贴并保存。

    Gg318615.Important(zh-cn,office.14).gif重要信息:

    这是配置此特定 Web 服务的 Web.config 文件。它位于 C:MyWebServiceCustomersService。

    <?xml version="1.0"?>
    <configuration>
      <system.serviceModel>
        <services>
          <service behaviorConfiguration="CustomersService.Service1Behavior"
            name="CustomersService.Customers">
            <endpoint address="" binding="wsHttpBinding" contract="CustomersService.ICustomers">
              <identity>
                <dns value="localhost" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="CustomersService.Service1Behavior">
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deploying the solution. -->
              <serviceMetadata httpGetEnabled="true"/>
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deploying the solution to avoid disclosing exception information. -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    
  10. 生成应用程序。

  11. 将应用程序添加到默认网站中。启动“Internet Information Services (IIS) 管理器”。依次单击“开始”、“所有程序”和“管理工具”,然后单击“Internet Information Services (IIS) 管理器”。在“Internet Information Services (IIS) 管理器”中,展开“网站”,右键单击“默认网站”,然后选择“添加应用程序”。下图演示如何在“Internet Information Services (IIS) 管理器”中添加应用程序。



    图 11. Internet Information Services 管理器

    Internet Information Services 管理器

    在“添加应用程序”对话框的“别名”字段中,键入 Customers。在“物理路径”字段中,浏览到 C:MyWebServiceCustomersService。单击“确定”。下图显示了包含 Web 服务的目录。



    图 12. 包含 Web 服务的目录

    浏览到包含 Web 服务的目录
  12. 验证 Web 服务是否正在运行。启动 Internet Explorer,并浏览到 http://localhost/Customers/Customers.svc。如果 Web 服务正在运行,您将看到以下内容:



    图 13. Internet Explorer 中的 CustomersService Web 服务

    在 Internet Explorer 中运行的 Web 服务

验证 Web 服务

测试 Web 服务的另一种方法是使用 WCF 测试客户端。

使用 WCF 测试客户端验证 Web 服务

  1. 启动 Visual Studio 命令提示符(或使用从前面过程的末尾打开的命令提示符)。

  2. 键入 wcftestclient 以运行 WCF 测试客户端。单击“文件”,然后单击“添加服务”

  3. 键入 http://localhost/Customers/Customers.svc 作为终结点地址,然后单击“确定”

    下图演示向 WCF 测试客户端中输入终结点。



    图 14. WCF 测试客户端

    WCF 测试客户端

    如果成功添加了服务,则会看到该服务公开的方法。下图显示了 WCF 测试客户端中的 Web 服务方法。



    图 15. 显示方法的 WCF 测试客户端

    显示方法的 WCF 测试客户端
  4. 双击“GetAllCustomers”。这会打开一个允许您配置请求和启动请求的窗口。

  5. 单击“调用”以查看 Web 服务的响应。下图显示了调用 GetAllCustomers 的结果。



    图 16. 显示调用 GetAllCustomers 的结果的 WCF 测试客户端

    显示数据的 WCF 测试客户端

结论

在本文中,您完成了创建声明感知 Web 服务的四个主要步骤中的第一步,并将其与 Business Connectivity Services 配合使用。您创建了 Web 服务,对其进行编译,并使用 IIS 承载它。在下一篇文章 WCF:在 WCF Web 服务内确定调用方身份(第 2 部分,共 4 部分)中,您将对此示例进行改进,以使用 Windows Identity Foundation (WIF) 中的功能来确定 Web 服务调用程序的身份。

原文地址:https://www.cnblogs.com/jackljf/p/3588840.html