Share via


演练:创建自定义 ASP.NET Web 服务

上次修改时间: 2010年7月7日

适用范围: SharePoint Foundation 2010

本文内容
创建 ASP.NET Web 服务的基本步骤
在 Visual Studio 中创建 ASP.NET Web 服务
生成和修改静态发现文件和 WSDL 文件
将 Web 服务文件部署到 _vti_bin 目录中
在 spdisco.aspx 中列出新 Web 服务
创建 Windows 应用程序以使用 Web 服务
实现 SharePoint Foundation 对象模型

此编程任务概述了如何创建可在 Microsoft SharePoint Foundation 上下文中运行的自定义 Web 服务。此任务逐步介绍在 Microsoft Visual Studio 2010 中创建一个简单的"Hello World"Web 服务的过程,然后演示如何修改该服务,以使其能够实现 SharePoint Foundation 服务器端对象模型,从而返回网站和列表数据。

重要注释重要信息

自定义 ASP.NET Web 服务将在 SharePoint Foundation 2010 中运行,但建议您改为创建自定义 WCF 服务。有关 WCF 服务的详细信息,请参阅 SharePoint Foundation 2010 中的 WCF 服务。有关演示如何创建自定义 WCF 服务的演练,请参阅演练:在 SharePoint Foundation 中创建和实现自定义 WCF 服务

创建 ASP.NET Web 服务的基本步骤

  • 在 Microsoft Visual Studio 中创建 Microsoft ASP.NET Web 服务。

  • 生成并编辑静态发现文件和 Web Services 描述语言 (WSDL) 文件。

  • 将 Web 服务文件部署到 _vti_bin 目录。

  • 通过在 Spdisco.aspx 中列出 Web 服务使其成为可检测到的

  • 创建客户端应用程序以使用 Web 服务。

  • 在定义 Web 服务的编程逻辑的 Web 服务中创建类库。

在 Visual Studio 中创建 ASP.NET Web 服务

首先在 Visual Studio 中创建一个 Microsoft ASP.NET Web 服务网站。

创建 ASP.NET Web 服务

  1. 在 Visual Studio 中,单击"文件",指向"新建",然后选择"网站"。

  2. 在"新建网站"对话框中,选择".NET Framework 3.5"作为目标框架,选择"Visual Basic"或"Visual C#"作为编程语言,在"已安装的模板"下选择"ASP.NET Web 服务"模板,在"Web 位置"框中选择"文件系统",指定项目的位置,然后单击"确定"。

  3. 在新的 Web 服务解决方案中,创建一个单独的类库项目以包含 Web 服务逻辑。若要创建该项目,请单击"文件",指向"新建",然后选择"项目"。

  4. 在"新建项目"对话框中,展开"Visual Basic"或"Visual C#",在"已安装的模板"下选择"类库"作为模板,提供项目的名称和位置,在"解决方案"框中选择"添加到解决方案",然后单击"确定"。

  5. 在该类库项目中添加对 System.Web.Services 命名空间的引用。在"解决方案资源管理器"中右键单击该项目,单击"添加引用",在"添加引用"对话框的".NET"选项卡上选择"System.Web.Services",然后单击"确定"。

  6. 使用 Visual Studio 在 Web 服务的"App_Code"文件夹中提供的默认服务类文件替换类库项目中的默认类文件。

    使用服务类文件替换类文件

    1. 在"解决方案资源管理器"中,将 Service.cs 或 Service.vb 文件拖至类库项目中的顶级节点。

    2. 删除 Class1.cs 或 Class1.vb 文件,并删除保留在"App_Code"文件夹中的 Service.cs 或 Service.vb 文件。

  7. 为类库创建一个强名称:

    1. 在"解决方案资源管理器"中,右键单击该类库项目,然后单击"属性"。

    2. 在"属性"对话框中,单击"签名",选择"为程序集签名",然后在"选择强名称密钥文件"列表中选择"<新建>"。

    3. 在"创建强名称密钥"对话框,为该密钥提供一个文件名,清除"使用密码保护密钥文件"复选框,再单击"确定"。

  8. 若要仅生成类库项目,请在"解决方案资源管理器"中,右键单击该项目,然后单击"生成"。

  9. 若要将程序集添加到全局程序集缓存 (GAC) 中,您可以使用 Windows 资源管理器的两个实例将程序集拖到 %windows%\assembly 目录中,或使用随 Microsoft .NET Framework 2.0 软件开发工具包一起安装的命令行实用工具 gacutil.exe

    使用 gacutil.exe 将类库 DLL 复制到 GAC 中

    1. 若要打开 Visual Studio 命令提示符,请依次单击"开始"、"所有程序"、"Microsoft Visual Studio 2010"、"Visual Studio 工具",右键单击"Visual Studio 命令提示(2010)",然后单击"以管理员身份运行"。

    2. 在命令提示符处,按下列格式键入命令,然后按 Enter。

      gacutil.exe -if "<Full file system path to DLL>"。

  10. 现在,您即可使用 GAC 中有关 DLL 的信息修改 Web 服务的默认 Service.asmx 文件中的程序集信息。若要从 GAC 中获取信息,请在 Windows 资源管理器中打开 %windows%\assembly 目录,右键单击您的程序集,再单击"属性"。

  11. 若要在"解决方案资源管理器"中打开 Service.asmx,请右键单击该文件,然后单击"打开"。

  12. 从 Service.asmx 的页指令中移除 CodeBehind 特性,并修改 Class 属性的内容,以便指令与以下格式相匹配,其中程序集名称"MyServiceAssembly"和公钥标记是在步骤 10 中打开的"属性"对话框中指定的值:

    <%@ WebService Language="C#" Class="Service, MyServiceAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8f2dca3c0f2d0131" %>
    

    在 Visual Basic 中,包括用于标识类的命名空间,例如 Class="MyServiceNamespace.Service, MyServiceAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8f2dca3c0f2d0131"。

  13. 为 .asmx 文件进行相应的重命名,然后保存所做的更改。

生成和修改静态发现文件和 WSDL 文件

若要提供自定义 Web 服务的发现和说明,必须创建 .disco 文件和 .wsdl 文件。由于 SharePoint Foundation 将虚拟化其 URL(例如,http://MyServer/MySite/MySubsite 将变为 http://MyServer),因此不能使用由 ASP.NET 自动生成的 .disco 和 .wsdl 文件,而必须创建可提供必需的重定向并保持虚拟化的 .disco 页和 .wsdl ASPX 页。

可以使用 ASP.NET 通过在虚拟目录(如 /_layouts)中临时承载 Web 服务来生成 .disco 和 .wsdl 文件,然后可以使用 Microsoft .NET Framework Web 服务发现工具 (Disco.exe) 获取生成的文件。

生成和编辑静态发现文件和 WSDL 文件

  1. 在 Windows 资源管理器中,将 Web 服务的 .asmx 文件复制到 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS 中。

  2. 在 LAYOUTS 目录的命令提示符处运行 Disco.exe 以生成 .disco 和 .wsdl 文件。按下列格式运行命令,以便在 \LAYOUTS 中生成这两个文件:

    disco http://MyServer/_layouts/MyCustomWebService.asmx

  3. 若要注册 SharePoint Foundation 对象模型的命名空间,请打开 .disco 和 .wsdl 文件,并将开放式 XML 处理指令 (<?xml version="1.0" encoding="utf-8"?>) 替换为下列指令。

    <%@ Page Language="C#" Inherits="System.Web.UI.Page" %> 
    <%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
    <%@ Import Namespace="Microsoft.SharePoint.Utilities" %> 
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <% Response.ContentType = "text/xml"; %>
    
  4. 在 .disco 文件中,像以下示例中一样修改协定引用和 SOAP 地址标记,通过使用 Microsoft.SharePoint.Utilities.SPHttpUtility 类将文本路径替换为代码生成的路径,并替换在 binding 属性中指定的方法名称。

    <contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request) + "?wsdl"),Response.Output); %> 
    docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> 
    xmlns="https://schemas.xmlsoap.org/disco/scl/" />
    <soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> 
    xmlns:q1="http://tempuri.org/" binding="q1:HelloWorld" xmlns="https://schemas.xmlsoap.org/disco/soap/" />
    <soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> 
    xmlns:q2="http://tempuri.org/" binding="q2:ServiceSoap12" xmlns="https://schemas.xmlsoap.org/disco/soap/" />
    
  5. 在 .wsdl 文件中,对指定的 SOAP 地址进行以下类似的替换。

    <soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />
    
  6. 重命名格式分别为 MyCustomWebServicedisco.aspx 和 MyCustomWebServicewsdl.aspx 的两个文件,以便可以通过 SharePoint Foundation 检测到您的服务。

将 Web 服务文件部署到 _vti_bin 目录中

_vti_bin 虚拟目录以物理方式映射到 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI 目录,该目录包含在 SharePoint Foundation 中使用的默认 Web 服务文件。将新的 MyCustomWebServicewsdl.aspx 和 MyCustomWebServicedisco.aspx 文件以及 MyCustomWebService.asmx 文件复制到 ISAPI 文件夹中。

在 _vti_bin 目录中,Web 服务可为添加服务的 Web 引用时指定的网站提供其功能。

若要验证您的自定义 Web 服务是否可以被检测到,请导航到 http://MyServer/_vti_bin/MyCustomWebService.asmx。

在 spdisco.aspx 中列出新 Web 服务

若要使您的 Web 服务作为与默认的 SharePoint Foundation Web 服务并列的 Web 服务在 Visual Studio 中成为可检测到的,请打开位于 %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\ISAPI 中的 spdisco.aspx 文件,并添加下面的代码,为您的 Web 服务指定 .asmx 文件。

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/MyCustomWebService.asmx?wsdl"), Response.Output); %> 
docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/MyCustomWebService.asmx"), Response.Output); %> 
xmlns=" https://schemas.xmlsoap.org/disco/scl/ " />
<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/MyCustomWebService.asmx?disco"),Response.Output); %> 
xmlns="https://schemas.xmlsoap.org/disco/" />

创建 Windows 应用程序以使用 Web 服务

将 Web 服务文件复制到 _vti_bin 目录后,接下来即可创建 Windows 应用程序以使用 Web 服务。

创建使用 Web 服务的 Windows 应用程序

  1. 打开 Visual Studio,在"文件"菜单上,指向"新建",然后单击"项目"。

  2. 在"新建项目"对话框中,选择"Visual C#"或"Visual Basic",然后选择"Windows 窗体应用程序"模板。

  3. 在"名称"框中键入应用程序的名称,在"位置"框中指定该项目文件的位置,再单击"确定"。

  4. 在"解决方案资源管理器"中,右键单击该项目,再单击"添加服务引用"。

  5. 在"添加服务引用"对话框中单击"高级",然后在"服务引用设置"框中单击"添加 Web 引用"。

  6. 在"添加 Web 引用"浏览器的地址栏中,键入要应用该服务的网站的 URL,如下所示:

    https://Server_Name/[sites/][Site_Name/]_vti_bin/MyCustomWebService.asmx

    然后按 Enter。

  7. 单击"添加引用"以下载该 Web 服务的服务合同。

  8. 在设计视图中打开 Form1,显示"工具箱",然后将按钮拖到该表单上。

  9. 双击"Form1"上的"Button1"控件,以在代码编辑器中显示代码隐藏文件,并添加可调用自定义方法的下列代码。

    Dim MyCustomService As New Web_Reference_Folder.MyServiceClass()
    MyCustomService.UseDefaultCredentials = True
    MessageBox.Show(MyCustomService.HelloWorld())
    
    Web_Reference_Folder.MyServiceClass MyCustomService = new Web_Reference_Folder.MyServiceClass();
    MyCustomService.UseDefaultCredentials = true;
    MessageBox.Show(MyCustomService.HelloWorld());
    
  10. 按 F5 编译并运行该项目,然后会看到一个其中显示"Hello World"的消息框。

实现 SharePoint Foundation 对象模型

现在,即可尝试使用 Web 服务的类库中的 SharePoint Foundation 对象模型的类型和成员。

实现 SharePoint Foundation 对象模型

  1. 添加对 Microsoft.SharePoint 程序集的引用。在"解决方案资源管理器"中,右键单击该类库项目,单击"添加引用",选择"SharePoint Foundation",然后单击"确定"。

  2. 在 Service.cs 或 Service.vb 项目文件中,必须在对象模型中导入相应的命名空间。例如,若要使用 Microsoft.SharePointMicrosoft.SharePoint.Utilities 命名空间的类型和成员,请添加以下指令。

    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Utilities
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    
  3. 重命名 Service.cs 或 Service.vb 中的方法。例如,将名称从 HelloWorld 更改为 GetSiteListCount。

  4. 添加以下代码以显示网站的名称及其所包含的列表数。

    Dim myWebSite As SPWeb = SPContext.Current.Web
    Dim lists As SPListCollection = myWebSite.Lists
    
    Return myWebSite.Title + " contains " + lists.Count.ToString() + 
    " lists."
    
    SPWeb myWebSite = SPContext.Current.Web;
    SPListCollection lists = myWebSite.Lists;
    
    return (myWebSite.Title + " contains " + lists.Count.ToString() + 
    " lists.");
    

    备注

    如果您的代码在 HttpGet 请求期间以某种方式修改 SharePoint Foundation 数据,则可能需要允许在网站上进行不安全的更新,可通过设置 AllowUnsafeUpdates 属性来完成此操作。

  5. 若要重新生成类库 DLL 并将其复制到 GAC,请重复"创建 ASP.NET Web 服务"中的步骤 8 和步骤 9。

  6. 重置 Internet Information Services (IIS) 以使 DLL 中的更改生效。

  7. 若要生成新版本的 .disco 和 .wsdl 文件,请重复"生成静态发现文件和 WSDL 文件"中的步骤,但应在适当的情况下更改 .disco 文件中的方法绑定名称(例如,更改为 GetSiteListCount)。

  8. 将新版本的 MyCustomWebServicedisco.aspx 和 MyCustomWebServicewsdl.aspx 文件复制到 ISAPI 文件夹中。

  9. 打开以前创建的 Windows 应用程序,删除前面对 Hello World 示例的 Web 服务引用,根据需要更改方法名称,然后添加对修改后的 Web 服务的新 Web 引用。

  10. 按 F5 编译并运行该项目,然后会看到一个其中显示网站名称及其所包含的列表数的消息框。