MOSS中如何自定义WebService
2017-04-12 20:19
274 查看
MOSS中如何自定义WebService
Posted on 2008-05-13 21:25 生鱼片 阅读(2326) 评论(3) 编辑 收藏 所属分类: SharepointMOSS中已经提供的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的逻辑部分。项目结构如下图:
为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[/code]
完成后会生成service.disco和service.wsdl文件2.将service.disco和service.wsdl文件中的<?xml version="1.0" encoding="utf-8"?>[code]该语句替换为以下语句:<%@ 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.
如下图:
六:客户端调用
我们建立一个window应用程序,添加该webservice的应用,然后在按钮的单击事件添加如下代码:carysun.Service se= new WindowsApplication1.carysun.Service(); se.UseDefaultCredentials = true; MessageBox.Show(se.GetSiteListCount());
se.UseDefaultCredentials = true;这句代码是设置信任的,否则会报没有权限的错误。
最后效果为:
作者:生鱼片
出处:http://carysun.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
相关文章推荐
- MOSS中如何自定义WebService
- MOSS2010中如何使用自定义WebService
- MOSS中如何自定义WebService
- MOSS中如何自定义WebService
- 如何自定义网站类别 - [MOSS 2007应用日记]
- MOSS中自定义WebService与客户端应用
- [MOSS 译]如何:在WEB内容查询部件中使用自定义的字段
- 利用webservice.htc如何使用Soap标头自定义身份验证和授权
- 【MOSS 2010】 如何开发一个自定义的WebPart来承载Silverlight应用程序
- MOSS 2007应用日记(8)――如何自定义网站类别
- Moss/Sharepoint 如何自定义显示界面以及字段
- MOSS中使用自定义WebService
- Moss自定义webservice
- MOSS中自定义WebService
- webservice如何传递复杂参数和自定义参数
- 【MOSS 2010】 如何开发一个自定义的WebPart来承载Silverlight应用程序
- 如何在视图状态中保存自定义的对象
- 如何使 FlashGet "正常合法" 下载 Session 中的自定义文件链接呢? JSP/Servlet 实现!
- 当中有介绍如何在DNN中添加自定义模块的方法
- java中如何自定义消息?