博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

MOSS中如何自定义WebService

Posted on 2008-05-13 21:25  生鱼片  阅读(4169)  评论(5编辑  收藏  举报

MOSS中已经提供的webservice都放在虚拟目录_vti_bin中,对应的物理目录为c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI。可能你会觉得这个目录_vti_bin名有点怪,这个名字来自该公司Vermeer Technologies Incorporated。这个公司唯一的产品就是FrontPage,该公司在1996年被微软收购。

下面我们就自己实现一个webservice,需要以下几步:

一:建立Webservice项目

1.使用vs2005建立一个webserivce项目来实现我们的webservice,然后我在填加一个类库用于实现webservice的逻辑部分。项目结构如下图:

01

为MOSSLibrary2类库签名,项目“右键---属性---签名---为程序集签名",不使用密码。Service.cs是现实Webservice逻辑的地方,代码如下:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () 
{ } [WebMethod] public string HelloWorld()
{ return "Hello World"; } [WebMethod] public string GetSiteListCount() { SPWeb myWeb=SPContext.Current.Web; SPListCollection lists=myWeb.Lists; return (myWeb.Title + " contains " + lists.Count.ToString() + " Web sites."); } }

二:将MOSSLibrary2类库添加到GAC中

有两种方法:

1. 将bin目录下的MOSSLibrary2.dll拖到%windows%\assembly文件夹下即可。
2. 打开VS2005的命令行工具,用GACUI.exe工具,命令如下:
gacutil.exe -iF "<Full file system path to DLL>".

三:修改service.asmx文件

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

其中的相关信息可以到%windows%\assembly文件夹下找到MOSSLibrary2.dll,右键查看其属性获得,该修改主要指定service.asmx的逻辑文件使用的是MOSSLibrary2项目中的service.cs中的代码。

四:生成静态发现文件service.disco和Webservice的描述文件service.wsdl

1.将service.asmx拷贝到c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\template\layouts目录下,然后打开VS2005的命令行工具,使用如下命令:

disco http://carysun/_layouts/Service.asmx

完成后会生成service.disco和service.wsdl文件

2.将service.disco和service.wsdl文件中的<?xml version="1.0" encoding="utf-8"?>该语句替换为以下语句:
<%@ Page Language="C#" Inherits="System.Web.UI.Page" %>
<%@ 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"; %>

实际上就是把原来的纯xml变换成为一个page来解析。并且这个页面的解析是通过moss处理的。 

3.将service.disco中的

<contractRef ref=http://carysun/_layouts/service.asmx?wsdl 
docRef="http://carysun/_layouts/service.asmx" xmlns="http://schemas.xmlsoap.org/disco/scl/" /> <soap address="http://carysun/_layouts/service.asmx" xmlns:q1=http://tempuri.org/
binding="q1:ServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" /> <soap address="http://carysun/_layouts/service.asmx" xmlns:q2=http://tempuri.org/
binding="q2:ServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

替换为:

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl
(Request) + "?wsdl"),Response.Output); %>
docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),
Response.Output); %> xmlns="http://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="http://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="http://schemas.xmlsoap.org/disco/soap/" />












4.将service.wsdl中的



















<
soap:address location="http://carysun/_layouts/service.asmx" /><soap12:address location="http://carysun/_layouts/service.asmx" /> 

替换为:
<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl
                                                          (Request)),Response.Output); %> />
<soap12:address location=<%SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl
                                                          (Request)),Response.Output); %> />

对于contractRef 还有soap address这两个节的更改,实际上是在页面里面重新编码了soap的查询url,这样做的目的也
是为了moss托管的web service可以在运行时根据动态的请求来正确定位。
 

5.将service.disco和service.wsdl改名为servicedisco.aspx和servicewsdl.aspx

五:部署webservice

将servicedisco.aspx,servicewsdl.aspx和service.asmx三个文件拷贝到c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI目录中,然后我们就可以通过以下地址来检测我们部署是否成功了。http://carysun/_vti_bin/Service.asmx.

如下图:

02

六:客户端调用

我们建立一个window应用程序,添加该webservice的应用,然后在按钮的单击事件添加如下代码:

   carysun.Service se= new WindowsApplication1.carysun.Service();
se.UseDefaultCredentials = true;
MessageBox.Show(se.GetSiteListCount());

se.UseDefaultCredentials = true;这句代码是设置信任的,否则会报没有权限的错误。

最后效果为:

03