专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »办公软件 » 自定义桌面web:MOSS中如何自定义WebService »正文

自定义桌面web:MOSS中如何自定义WebService

来源: 发布时间:星期日, 2009年2月8日 浏览:42次 评论:0
  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逻辑部分项目结构如下图:

MOSS中如何自定义WebService

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

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


   2:将MOSSLibrary2类库添加到GAC中

  有两种思路方法:

  1. 将bin目录下MOSSLibrary2.dll拖到%windows%assembly文件夹下即可

  2. 打开VS2005命令行工具用GACUI.exe工具命令如下:

  gacutil.exe -iF "<Full file system path to DLL>".

   3:修改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中代码

   4:生成静态发现文件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"?>该语句替换为以下语句:

<%@ Page Language="C#" Inherits=".Web.UI.Page" %>
<%@ Assembly Name="Microsoft.SharePo, Version=12.0.0.0, Culture=neutral,
                             PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePo.Utilities" %>
<%@ Import Namespace="Microsoft.SharePo" %>
<% 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

   5:部署webservice

  将servicedisco.aspxservicewsdl.aspx和service.asmx 3个文件拷贝到c:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI目录中然后我们就可以通过以下地址来检测我们部署是否成功了http://carysun/_vti_bin/Service.asmx.

  如下图:

MOSS中如何自定义WebService

   6:客户端

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

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


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

  最后效果为:



MOSS中如何自定义WebService



0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: