您的位置:首页 > 其它

MOSS中如何自定义WebService

2010-09-03 11:34 309 查看
MOSS中已经提供的webservice都放在虚拟目录_vti_bin中,对应的物理目录为c:\ProgramFiles\CommonFiles\MicrosoftShared\WebServerExtensions\12\ISAPI。可能你会觉得这个目录_vti_bin名有点怪,这个名字来自该公司VermeerTechnologiesIncorporated。这个公司唯一的产品就是FrontPage,该公司在1996年被微软收购。

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

一:建立Webservice项目

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





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

[WebService(Namespace="http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
publicclassService:System.Web.Services.WebService
{
publicService()
{}

[WebMethod]
publicstringHelloWorld()
{
return"HelloWorld";
}

[WebMethod]
publicstringGetSiteListCount()
{
SPWebmyWeb=SPContext.Current.Web;
SPListCollectionlists=myWeb.Lists;
return(myWeb.Title+"contains"+lists.Count.ToString()+"Websites.");
}
}

二:将MOSSLibrary2类库添加到GAC中
有两种方法:
1.
将bin目录下的MOSSLibrary2.dll拖到
%windows%\assembly
文件夹下即可。
2.打开VS2005的命令行工具,用GACUI.exe工具,命令如下:
gacutil.exe-iF"<FullfilesystempathtoDLL>"
.
三:
修改service.asmx文件
<%@WebServiceLanguage="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:\ProgramFiles\CommonFiles\MicrosoftShared\WebServerExtensions\12\template\layouts目录下,然后打开VS2005的命令行工具,使用如下命令:
disco'target='_blank'>http://carysun/_layouts/Service.asmx[/code]完成后会生成service.disco和service.wsdl文件
2.将service.disco和service.wsdl文件中的<?xmlversion="1.0"encoding="utf-8"?>[code]该语句替换为以下语句:
<%@PageLanguage="C#"Inherits="System.Web.UI.Page"%>
<%@AssemblyName="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,
PublicKeyToken=71e9bce111e9429c"%>
<%@ImportNamespace="Microsoft.SharePoint.Utilities"%>
<%@ImportNamespace="Microsoft.SharePoint"%>
<%Response.ContentType="text/xml";%>

实际上就是把原来的纯xml变换成为一个page来解析。并且这个页面的解析是通过moss处理的。
3.将service.disco中的
<contractRefref=http://carysun/_layouts/service.asmx?wsdl
docRef="http://carysun/_layouts/service.asmx"xmlns="http://schemas.xmlsoap.org/disco/scl/"/>
<soapaddress="http://carysun/_layouts/service.asmx"xmlns:q1=http://tempuri.org/
binding="q1:ServiceSoap"xmlns="http://schemas.xmlsoap.org/disco/soap/"/>
<soapaddress="http://carysun/_layouts/service.asmx"xmlns:q2=http://tempuri.org/
binding="q2:ServiceSoap12"xmlns="http://schemas.xmlsoap.org/disco/soap/"/>

替换为:
<contractRefref=<%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/"/>
<soapaddress=<%SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),
Response.Output);%>xmlns:q1="http://tempuri.org/"binding="q1:HelloWorld"
xmlns="http://schemas.xmlsoap.org/disco/soap/"/>
<soapaddress=<%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:addresslocation="http://carysun/_layouts/service.asmx"/>和
<soap12:addresslocation="http://carysun/_layouts/service.asmx"/>

替换为:
<soap:addresslocation=<%SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl
(Request)),Response.Output);%>/>
和<soap12:addresslocation=<%SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl
(Request)),Response.Output);%>/>
对于contractRef还有soapaddress这两个节的更改,实际上是在页面里面重新编码了soap的查询url,这样做的目的也
是为了moss托管的webservice可以在运行时根据动态的请求来正确定位。

5.将service.disco和service.wsdl改名为servicedisco.aspx和servicewsdl.aspx
五:部署webservice
将servicedisco.aspx,servicewsdl.aspx和service.asmx三个文件拷贝到c:\ProgramFiles\CommonFiles\MicrosoftShared\WebServerExtensions\12\ISAPI目录中,然后我们就可以通过以下地址来检测我们部署是否成功了。
http://carysun/_vti_bin/Service.asmx
.
如下图:



六:客户端调用
我们建立一个window应用程序,添加该webservice的应用,然后在按钮的单击事件添加如下代码:
carysun.Servicese=newWindowsApplication1.carysun.Service();
se.UseDefaultCredentials=true;
MessageBox.Show(se.GetSiteListCount());

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: