MOSS中自定义WebService
(轉http://tech.ddvip.com/2008-10/122527111786701.html,更詳細見msdn:http://msdn.microsoft.com/zh-cn/library/ms464040.aspx)
?
MOSS中已經提供的webservice都放在虛擬目錄_vti_bin中,對應的物理目錄為c:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI。可能你會覺得這個目錄_vti_bin名有點怪,這個名字來自該公司Vermeer Technologies Incorporated。這個公司唯一的產品就是FrontPage,該公司在1996年被微軟收購。
下面我們就自己實現一個webservice,需要以下幾步:
一:建立Webservice項目
1.使用vs2005建立一個webserivce項目來實現我們的webservice,然后我在填加一個類庫用于實現webservice的邏輯部分。項目結構如下圖:
為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中 (如果放在bin下就省略以下操作)
有兩種方法:
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 FilesCommon FilesMicrosoft SharedWeb Server Extensions12templatelayouts目錄下,然后打開VS2005的命令行工具,使用如下命令:
disco http://carysun/_layouts/Service.asmx
完成后會生成service.disco和service.wsdl文件
2.將service.disco和service.wsdl文件中的<?xml version="1.0" encoding="utf-8"?>該語句替換為以下語句: (這些語句在isapi目錄下的頁面也可以找到)
<%@ 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 FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI目錄中,然后我們就可以通過以下地址來檢測我們部署是否成功了。http://carysun/_vti_bin/Service.asmx.
如下圖:
六:客戶端調用 (在最后面我調用服務操作列表庫時出現一個“請求因 HTTP 狀態 401 失敗:Unauthorized”的錯誤,那是因為IIS沒啟匿名訪問)
我們建立一個web站點,添加該webservice的應用,然后在按鈕的單擊事件添加如下代碼:
carysun.Service se= new WindowsApplication1.carysun.Service();
se.UseDefaultCredentials = true;
MessageBox.Show(se.GetSiteListCount());
se.UseDefaultCredentials = true;這句代碼是設置信任的,否則會報沒有權限的錯誤。
?
?
?
?
總結
以上是生活随笔為你收集整理的MOSS中自定义WebService的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到自己会开小车什么预兆
- 下一篇: 梦到买鞭炮放鞭炮什么意思