在MOSS2007中开发并部署自定义WebService
开发WebService开发WebService的过程与平时相仿,新建WebService项目并直接在code-behind中完成相关代码即可,编译后生成Ceair.Union.Web.dll,如下例: 为WebService进行强命名在WebService项目上“右键”→“属性”中选择“签名”选项卡,勾选“为程序集签名”选项,并在下方的下拉列表中新建一个snk文件,密码可以为空,过程如下图所示:? ? ? 生成wsdl和dicso文件这时候,按理说我们可以在VS中直接选择在浏览器中打开,查看刚刚写好的WebService,如下图: 接下来,我们可以用两种方法来获取这个Service的wsdl文件和disco文件: A方法:用VisualStudio的命令行工具执行:
disco?http:
//
localhost:
6949
/
B2EWidgetService.asmx
?然后就可以在VisualStudio的根目录下找到生成的B2EWidgetService.wsdl和B2EWidgetService.disco文件。 B方法:直接在浏览器里输入:
?http:
//
localhost:
6949
/
B2EWidgetService.asmx
?
wsdl
?http: // localhost: 6949 / B2EWidgetService.asmx ? disco 并将结果保存为B2EWidgetService.wsdl和B2EWidgetService.disco文件即可。 修改wsdl和disco文件我们知道wsdl文档和disco文件都是以XML格式对WebService进行描述的,一般情况下可以直接访问到(上节B方法)。而在MOSS中的WebService(包括moss自带的一些,如lists.asmx、alerts.asmx等)其wsdl和disco文件要通过ASP.NET程序化的方式来动态生成并输出。 在这里,我们的自定义WebService也要仿照“C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI”里MOSS自带WebService的方法进行部署,步骤如下: 步骤一分别将B2EWidgetService.wsdl和B2EWidgetService.disco重命名为B2EWidgetServicewsdl.aspx和B2EWidgetServicedisco.aspx。 步骤二打开B2EWidgetServicedisco.aspx,将里面的代码(注意高亮部分的代码是下面需要改动的,斜体部分为此例子中特定的):
<?
xml?version="1.0"?encoding="utf-8"
?>
< discovery? xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" ?xmlns:xsd ="http://www.w3.org/2001/XMLSchema" ?xmlns ="http://schemas.xmlsoap.org/disco/" > ?? < contractRef? ref =" http://localhost:6949/B2EWidgetService.asmx?wsdl" ?docRef =" http://localhost:6949/B2EWidgetService.asmx" ?xmlns ="http://schemas.xmlsoap.org/disco/scl/" ? /> ?? < soap? address =" http://localhost:6949/B2EWidgetService.asmx" ?xmlns:q1 ="http://sharepoint.microsoft.com/samples/" ?binding ="q1: B2EWidgetServiceSoap" ?xmlns ="http://schemas.xmlsoap.org/disco/soap/" ? /> ?? < soap? address ="http://localhost:6949/B2EWidgetService.asmx" ?xmlns:q2 ="http://sharepoint.microsoft.com/samples/" ?binding ="q2:B2EWidgetServiceSoap12" ?xmlns ="http://schemas.xmlsoap.org/disco/soap/" ? /> </ discovery > 修改为如下所示:
<%
@?Page?Language
=
"
C#
"
?Inherits
=
"
System.Web.UI.Page
"
%>
<% @?Assembly?Name = " Microsoft.SharePoint,?Version=11.0.0.0,?Culture=neutral,?PublicKeyToken=71e9bce111e9429c " ? %> ? <% @?Import?Namespace = " Microsoft.SharePoint.Utilities " ? %> ? <% @?Import?Namespace = " Microsoft.SharePoint " ? %> <% ?Response.ContentType? = ? " text/xml " ;? %> < discovery? xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" ?xmlns:xsd ="http://www.w3.org/2001/XMLSchema" ?xmlns ="http://schemas.xmlsoap.org/disco/" > ?? < contractRef? ref =<%? SPEncode.WriteHtmlEncodeWithQuote(Response,?SPWeb.OriginalBaseUrl(Request)?+?"?wsdl",?'"');?% > ?docRef= <% ?SPEncode.WriteHtmlEncodeWithQuote(Response,?SPWeb.OriginalBaseUrl(Request),? ' "');?%>??xmlns="http://schemas.xmlsoap.org/disco/scl/"?/> ?? < soap?address =< %?SPEncode.WriteHtmlEncodeWithQuote(Response,? ' "');?%>?xmlns:q1="http://tempuri.org/"?binding="q1:B2EWidgetServiceSoap"?xmlns="http://schemas.xmlsoap.org/disco/soap/"?/> </ discovery > 步骤三打开B2EWidgetServicewsdl.aspx,将里面的代码(注意高亮部分的代码是下面需要改动的,斜体部分为此例子中特定的):
<?
xml?version="1.0"?encoding="utf-8"?
?>
< wsdl:definitions? xmlns:soap ="http://schemas.xmlsoap.org/wsdl/soap/" ?xmlns:tm ="http://microsoft.com/wsdl/mime/textMatching/" ????xmlns:soapenc ="http://schemas.xmlsoap.org/soap/encoding/" ?xmlns:mime ="http://schemas.xmlsoap.org/wsdl/mime/" ????xmlns:tns ="http://sharepoint.microsoft.com/samples/" ?xmlns:s ="http://www.w3.org/2001/XMLSchema" ????xmlns:soap12 ="http://schemas.xmlsoap.org/wsdl/soap12/" ?xmlns:http ="http://schemas.xmlsoap.org/wsdl/http/" ????targetnamespace ="http://sharepoint.microsoft.com/samples/" ?xmlns:wsdl ="http://schemas.xmlsoap.org/wsdl/" > ???? < wsdl:types > ???????? < s:schema? elementformdefault ="qualified" ?targetnamespace ="http://sharepoint.microsoft.com/samples/" > ???????????? < s:element? name ="HelloWorld" > ???????????????? < s:complextype? /> ?。。。。。。。 ???? </ wsdl:binding > ???? < wsdl:service? name ="B2EWidgetService" > ???????? < wsdl:port? name ="B2EWidgetServiceSoap" ?binding ="tns:B2EWidgetServiceSoap" > ???????????? < soap:address? location =" http://localhost:6949/B2EWidgetService.asmx" ? /> ???????? </ wsdl:port > ???????? < wsdl:port? name ="B2EWidgetServiceSoap12" ?binding ="tns:B2EWidgetServiceSoap12" > ???????????? < soap12:address? location ="http://localhost:6949/B2EWidgetService.asmx" ? /> ???????? </ wsdl:port > ???? </ wsdl:service > </ wsdl:definitions > 修改为: ?
<%
@?Page?Language
=
"
C#
"
?Inherits
=
"
System.Web.UI.Page
"
?
%>
<% @?Assembly?Name = " Microsoft.SharePoint,?PublicKeyToken=71e9bce111e9429c " ? %> <% @?Import?Namespace = " Microsoft.SharePoint.Utilities " ? %> <% @?Import?Namespace = " Microsoft.SharePoint " ? %> <% ?Response.ContentType? = ? " text/xml " ;? %> < wsdl:definitions? xmlns:s ="http://www.w3.org/2001/XMLSchema" ?xmlns:soap12 ="http://schemas.xmlsoap.org/wsdl/soap12/" ?xmlns:mime ="http://schemas.xmlsoap.org/wsdl/mime/" ?xmlns:tns ="http://sharepoint.microsoft.com/samples/" ?xmlns:soap ="http://schemas.xmlsoap.org/wsdl/soap/" ?xmlns:tm ="http://microsoft.com/wsdl/mime/textMatching/" ?xmlns:http ="http://schemas.xmlsoap.org/wsdl/http/" ?xmlns:soapenc ="http://schemas.xmlsoap.org/soap/encoding/" ?targetNamespace ="http://sharepoint.microsoft.com/samples/" ?xmlns:wsdl ="http://schemas.xmlsoap.org/wsdl/" > ?? < wsdl:types > ???? < s:schema? elementFormDefault ="qualified" ?targetNamespace ="http://sharepoint.microsoft.com/samples/" > ?????? < s:element? name ="HelloWorld" > ???????? < s:complexType? /> 。。 。。。。。。。 ?? </ wsdl:binding > ?? < wsdl:service? name ="B2EWidgetService" > ???? < wsdl:port? name ="B2EWidgetServiceSoap" ?binding ="tns:B2EWidgetServiceSoap" > ?????? < soap:address? location =<%? SPEncode.WriteHtmlEncodeWithQuote(Response,SPWeb.OriginalBaseUrl(Request),?'"');?% > ?/> ???? </ wsdl:port > ?? </ wsdl:service > </ wsdl:definitions > 步骤四打开“C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI”下的"spdisco.aspx"文件,可以看到MOSS自带的WebService都注册于此,我们也需要把自己的自定义WebService加进来(在该文件下面直接附加以下两行):
??
<
contractRef??
ref
=<%?
SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url?+?"/_vti_bin/B2EWidgetService.asmx?wsdl"),Response.Output);?%
>
?docRef=
<
%?SPHttpUtility.AddQuote
(SPHttpUtility.HtmlEncode(spWeb.Url?+?"/_vti_bin/B2EWidgetService.asmx"),Response.Output);?%
>
?xmlns="http://schemas.xmlsoap.org/disco/scl/"?/>
?? < discoveryRef? ref =<%? SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url?+?"/_vti_bin/B2EWidgetService.asmx?disco"),Response.Output);?% > ?xmlns="http://schemas.xmlsoap.org/disco/"?/> ? 部署WebService 完成上述步骤后将B2EWidgetService.asmx以及B2EWidgetServicewsdl.aspx和B2EWidgetServicedisco.aspx拷贝到“C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI”下,并将WebService编译后的Ceair.Union.Web.dll拷贝到MOSS站点的Bin目录下即可完成部署。 ? 查看结果完成部署后,即可在MOSS站点的“_vti_bin/”路径下看到刚刚部署的WebService。 ? 注意事项?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |