本文的内容是讲解如何创建一个在WSS上下文中使用的Web Service。
创建的步骤:
l 在vs2005中创建一个Asp.net Web Service应用程序.
l 在Web service程序创建一个类库,用于定义Web Service的逻辑处理功能
l 使用工具生成并编辑静态的.disco和.wsdl文件
l 将Web Service部署到_vti_bin目录
l 创建一个Windows应用程序测试创建的WebService是否可用
一:创建Asp.net Web Service应用程序和类库
注意,我这里创建的WebService工程中默认有一个Service.asmx,我就先用这个进行演示了,就不创建新的了,其中有一个方法HelloWorld。
打开vs2005,点击File,选择Asp.net Web Service,Location选择File System,语言选择C#,
创建一个类库,用于进行Web Service的逻辑处理。选择Web Service工程的解决方案,点击File,选择Class Library,输入合适的名字,如ServiceLib。选择ServiceLib,右健,选择AddReference,选择System.Web.Service,点击OK。
将WebService工程中App_Code文件夹下的Service.cs拖到ServiceLib工程中,然后删除WebService工程中的Service.cs。
给ServiceLib签名,右健ServiceLib,选择Properties,在Signing栏中选择Sign the assemble,在下拉框中选择新建一个密钥文件,如key.snk.
生成ServiceLib工程。
将ServiceLib.dll注册到GAC中去:
1. 选择“开始-程序-Visual Studio 2005-Visual Studio Tools”,选择Visual Studio 2005 Command Prompt;
2. 输入命令:gacutil –i dll文件的路径;
最后一步,修改WebService工程中的Service.asmx文件,打开该文件,将该文件由
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
修改为:
<%@ WebService Language="C#" Class="Service,ServiceLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=698a7d0279197ba0" %>
其中:Service,ServiceLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=698a7d0279197ba0
Service为ServiceLib工程中WebService中的类的名称,后面的内容可以借助Reflector获取。
二.生成.disco和.wsdl文件
因为WSS3.0将它的Urls虚拟化了,你不能使用Asp.net自动产生的.disco和.wsdl文件,因此我们需要手工创建并修改。
将webService文件拷贝到""Program Files"Common Files"Microsoft Shared"web server extensions"12"TEMPLATE"LAYOUTS目录下。
1. 选择“开始-程序-Visual Studio 2005-Visual Studio Tools”,选择Visual Studio 2005 Command Prompt;输入以下命令山生.disco和.wsdl文件,
2. disco http://MyServer/_layouts/Service.asmx;
3. 产生的文件在"Program Files"Microsoft Visual Studio 8"SDK"v2.0"Bin目录下;
开始修改 .disco文件和.wsdl文件:
1. 将两个文件中的<?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"; %>
2. 在.disco文件里,按照以下格式进行修改:
<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/" />
3. 在.wsdl文件里,采用查找替换的方法,将其中的location节点修改为如下的格式:
<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />
4. 将.disco和.wsdl文件分别命名为:Servicedisco.aspx和Servicewsdl.aspx;
三.部署WebService
_vti_bin文件夹映射到Local_Drive:"Program Files"Common Files"Microsoft Shared"Web Server Extensions"12"ISAPI目录,这个目录里面存放的是WSS使用的WebService文件。将Servicedisco.aspx和Servicewsdl.aspx,以及Service.asmx文件拷贝到ISAPI目录下。
为了让自定义的WebService文件能够和WSS自带的WebService文件一样能够正常地工作,需要修改"Program Files"Common Files"Microsoft Shared"Web Server Extensions"12"ISAPI路径下的spdisco.aspx文件,在其中增加以下代码:
<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/Service.asmx?wsdl"), Response.Output); %> docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/Service.asmx"), Response.Output); %> xmlns=" http://schemas.xmlsoap.org/disco/scl/ " />
<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/Service.asmx?disco"),Response.Output); %> xmlns="http://schemas.xmlsoap.org/disco/" />
四.测试WebService文件
好了,现在WebService文件已经做好了,现在我们新建一个Windows Application来测试我们自定义的WebService文件。添加Web Reference,在工程中进行测试,代码如下:
gswuxy.Service service = new TestWSSService.gswuxy.Service();
MessageBox.Show(service.HelloWorld());
好了,最终的结果如下:
OK,完成了。
五.后记
这个时候我们就可以继续改造我们的WebService使之完成更加复杂的功能,同时在修改的过程中,我们需要将dll文件重新注册到GAC中去,完成后,重新启动IIS,是新的dll生效;生成新版本的.disco和wsdl文件,并将他们拷贝到相应的目录下。
非常遗憾的是,文章中的图都没了!有兴趣的可以下载完整的版本,噢噢!
浙公网安备 33010602011771号