zoukankan      html  css  js  c++  java
  • MOSS中如何自定义WebService

    MOSS中如何自定义WebService

    Posted on 2008-05-13 21:25 生鱼片 阅读(2326) 评论(3) 编辑 收藏 所属分类: Sharepoint

    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

    作者:生鱼片
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    网络爬虫学习软件篇-Python(一)下载安装(超详细教程,傻瓜式说明)
    Vue.js+BootStrap+.Net Core开发后台管理系统 初次接触学习记录(11-7)
    形参 ref 到底是做什么用的?
    SweetAlert拒绝单一的弹出警告框
    轻松学习C语言编程之函数知识详解
    C语言的这个小知识点,竟然连开发多年的老司机都了解的不完全
    「C语言」编程学习—控制语句goto语句解析!
    数学思维+C语言画小猪佩奇,来试试?
    抖音很火的告白编程程序,C语言一样也能做
    世界最强的编程语言:C语言
  • 原文地址:https://www.cnblogs.com/gill/p/1783577.html
Copyright © 2011-2022 走看看