MOSS2007自定义WebService(1)-----自定义Webservice的步骤介绍

来源:互联网 发布:软件新技术有哪些 编辑:程序博客网 时间:2024/04/18 12:51

MOSS2007自定义WebService

 

提到WebService,我们会想到asmx文件,那我们就从这个asmx文件开始:

1.    新建一个asmx文件,例如叫MyService.asmx,其内容如下:

<%@ WebService Language="C#" Class="MyServiceClass, MyServiceAssembly, Version=1.0.0.0,  Culture=neutral, PublicKeyToken=722dca430e2d0190" %>

那么就从这个里面引出了一个GACassembly出来,那么我们进行下一步

2.    使用Visual Studio创建一个ClassLibrary的项目,并且将这个项目进行强命名,为什么要进行强命名呢,就是因为我们会将生成的dll部署到GAC中供WebService引用,然后如果要部署到GAC中就需要对项目进行强命名。那么我们就讲述一下如果对项目进行强命名。

3.    强命名主要有两种方式:

(1)  项目右键---属性---签名---为程序集签名",不使用密码

(2)  使用VSTool 命令行,使用sk –k c:/Service.snk,然后使用这个snk文件对项目进行强命名即可

4.    这一步我们主要是讲一些这个Webservice的项目内容,如何类编写

   using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.SharePoint.Utility;
using Microsoft.SharePoint;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyService : System.Web.Services.WebService
{
    public MyService ()     {    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

这里需要指出的就是继承WebService类,方法是用[WebMethod]特性即可。

5.    生成静态发现文件service.discoWebservice的描述文件service.wsdl

(1)service.asmx拷贝到c:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/12/template/layouts目录下,然后打开VS2005的命令行工具,使用如下命令:

disco http://chris/_layouts/Service.asmx 生成service.discoservice.wsdl文件

(2)service.discoservice.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" %>
<%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<% Response.ContentType = "text/xml"; %>

实际上就是把原来的纯xml变换成为一个page来解析。并且这个页面的解析是通过moss来自动处理的,是不需要我们关心的,也就是说对我们是透明的。 

(3)service.disco中的

<contractRef ref=http://chris/_layouts/service.asmx?wsdl 
docRef="http://carysun/_layouts/service.asmx" xmlns="http://schemas.xmlsoap.org/disco/scl/" />
<soap address="http://chris/_layouts/service.asmx" xmlns:q1=http://tempuri.org/ 
                    binding="q1:ServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
<soap address=http://chris/_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://schemas.microsoft.com/sharepoint/soap/" binding="q1:HostServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

  <soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> xmlns:q2="http://schemas.microsoft.com/sharepoint/soap/" binding="q2:HostServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
(4)
service.wsdl中的
<soap:address location="http://chris/_layouts/service.asmx" />

<soap12:address location="http://chris/_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可以在运行时根据动态的请求来定位。 

(1)service.discoservice.wsdl改名为servicedisco.aspxservicewsdl.aspx

6.  部署WebService

主要是将service.asmxservicedisco.aspxservicewsdl.aspx拷贝到C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/12/ISAPI这个目录中,然后将我们项目生成的dll部署到GAC中即可

7.  调用WebService

使用浏览器访问http://chris/_vti_bin/serivce.asmx 即可看到 Service中提供的方法,然后可以进行调用。

以上就是我们在MOSS2007中自定义WebService的过程,希望对大家有所帮助。

原创粉丝点击