【发布时间】: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 上下文。
【问题讨论】: