【问题标题】:sharepoint web services in subwebs子网站中的共享点 Web 服务
【发布时间】:2009-05-05 01:38:22
【问题描述】:

我正在尝试构建一个对子网站上下文敏感的 Web 服务(即,它公开了一个需要能够触摸特定子网站中的列表的 WebMethod)。

我已经使用以下文件部署了 Web 应用程序:

ISAPI/MyService.asmx
ISAPI/MyServiceWsdl.aspx
ISAPI/MyServiceDisco.aspx

代码隐藏:

[WebService]
public class MyService : System.Web.Services.WebService
{
    [WebMethod]
    public ListSettings GetListSettings(string listName)
    {
        SPWeb site = SPControl.GetContextWeb(this.Context);
        SPList list = site.Lists[listName];

        return new ListSettings(list);
    }

    [WebMethod]
    public void UpdateListSettings(string listName, ListSettings settings)
    {
        SPWeb site = SPControl.GetContextWeb(this.Context);
        SPList list = site.Lists[listName];

        list.EnableFolderCreation = settings.EnableFolders;
        list.Update();
    }
}

public class ListSettings
{
    public ListSettings() { }

    internal ListSettings(SPList list)
    {
        EnableFolders = list.EnableFolderCreation;
    }

    public bool EnableFolders { get; set; }
}

这似乎在我的网站集的根网站上运行良好。但是,当我向http://moss/subweb/_vti_bin/MyService.asmx 发出请求,并在断点处停止Web 方法以检查HttpContext 时,我发现已向http://moss/_vti_bin/MyService.asmx 发出请求。

我阅读了一些资源,这些资源描述了 WSDL/disco 文件和 ISAPI 中的 spdisco.aspx 文件中所需的调整,并进行了我应该做的调整。像这样:

在 MyServiceDisco.aspx 中:

<%@ Page Inherits="System.Web.UI.Page" Language="C#" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<% Response.ContentType = "text/xml"; %>

<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/">
  <contractRef ref=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request) + "?wsdl", '"'); %> 
            docRef=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> 
            xmlns="http://schemas.xmlsoap.org/disco/scl/" />
  <soap address=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> 
        xmlns:q1="http://tempuri.org/" binding="q1:MyServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
  <soap address=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> 
        xmlns:q2="http://tempuri.org/" binding="q2:MyServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
</discovery>

在 MyServiceWsdl.aspx 的顶部:

<%@ Page Inherits="System.Web.UI.Page" Language="C#" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<% Response.ContentType = "text/xml"; %>

在底部:

<wsdl:service name="MyService">
    <wsdl:port name="MyServiceSoap" binding="tns:MyServiceSoap">
      <soap:address location=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> />
    </wsdl:port>
    <wsdl:port name="MyServiceSoap12" binding="tns:MyServiceSoap12">
      <soap12:address location=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> />
    </wsdl:port>
  </wsdl:service>

最后,对 spdisco.aspx 的补充:

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

最后,我以为我已经完成了所有必要的工作,以使该服务在网站集中的每个网站上都能正常工作,但是,唉,这似乎并不正确。我忘了做什么?

更新:另一个信息是尝试获取发现 XML 会导致“未找到”共享点错误。换句话说,访问 http://moss/mysubsite/_vti_bin/MyService.asmx?disco 会导致 Sharepoint“找不到文件”错误。

更新:原来在 MyServiceDisco.aspx 中有一个小错字导致“找不到文件”错误。但是,我解决了这个问题,现在服务完全崩溃了。使用 WCF 测试客户端,我收到一条错误消息“服务器无法处理请求。---> 对象引用未设置为对象的实例”,然后是堆栈跟踪。从我正在处理的应用程序中,当我调用生成的代理类“MyServiceSoapClient”时,响应是“远程服务器返回错误:NotFound”。

Arrrgggh!

更新:好的,“对象引用未设置...”消息是我的一个愚蠢错误的结果。现在似乎一切正常,除了 SPContext.Current.HttpRequestContext.Request.Path 属性始终显示根网站而不是当前网站下的服务的 url。我使用 Wireshark 来确保客户端发布到正确的 url(它是:http://moss/subweb/_vti_bin/MyService.asmx),我使用了 ASP.NET 跟踪工具并且没有发现任何异常(请求显示在使用正确的 url 进行跟踪),并且我使用了 IIS 跟踪实用程序 (logman.exe),它没有显示任何意外。一旦我在服务中遇到断点,唯一看起来不正确的是 HttpRequest 上下文。

【问题讨论】:

    标签: web-services sharepoint


    【解决方案1】:

    Duuude... 我在各种 SharePoint 中遇到了足够多的此类问题,因此我完全确信它仍然是一个测试版产品。

    最后,我发现使用 SPContext 而不是 SPControl 来获取当前网络可以满足我的需求。所有 MSDN 文档都说要像这样获取 Web:

    SPControl.GetContextWeb(this.Context);
    

    而且,如果你反汇编内置 web 服务,你会发现这也是内置 web 服务获取当前 web 的方式。对于我的服务,它不起作用,但确实如此:

    SPContext.Current.Site.OpenWeb();
    

    为什么????为什么,微软,为什么???!!!!!!

    【讨论】:

    • 您一定遇到过一些过时的文档。 MS对此并不满意。博客是 SharePoint 最佳实践的最佳来源。
    【解决方案2】:

    你所拥有的对我来说看起来不错。我之前用过几次都没有问题。

    我注意到的一个区别是我获取 SPWeb 的代码略有不同(并且更简单):

            // Get the current site based on our context
            // Note that this approach does _not_ require a Dispose of the SPWeb
            SPWeb site = SPContext.Current.Web;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      相关资源
      最近更新 更多