| | | 通过Web Services提供内容服务 | | 2001-10-09·
·甘冀平··yesky
| 上一页 1 2 四、配置Web Service
配置一个Web Service就象配置其他的ASP.NET Web应用程序一样简单。对于上面所述的例子,我们要创建一个名为"horoservice"的虚拟目录,或者仅将程序文件上传到任何支持ASP.NET的服务器就可以。还请记住一点:将文件dailyhoro.xml放置到虚拟目录horoservice下的db子目录下面。
这样就配置好了Web Service。调用方式也很简单,在浏览器中输入以下地址:
| http://localhost/horoservice/wshoro.asmx | 五、应用Web Service
Web Service已经准备好,现在开始应用这个Web Service。
1、首先创建一个代理类(Proxy Class),它包含如何定位Web Service以及用户界面描述的信息。我们使用WebServiceUtil.exe来创建这个代理类:
WebServiceUtil /c:proxy /pa:http://localhost/csharp_test/oroscope/wshoro.asmx?SDL /n:horo_service | 这样就在程序运行目录下创建了文件"DailyHoro.cs"。
2、然后,将源代码进行编译,创建被客户端使用的库文件:
csc /target:library /r:System.dll;System.Web.Services.dll;System.Net.dll; System.IO.dll;System.Xml.Serialization.dll DailyHoro.cs | 命令执行后,将创建一个Dll文件"DailyHoro.dll"。
3、现在开始编写客户端的Web页面文件ClientHoro.aspx。
首先引入必需的名字空间,其中名字空间horo_service包含了前面步骤中创建的代理类:
<%@ Import namespace="System" %> <%@ Import namespace="horo_service" %> <%@ Page Language="C#" %> | 下面的代码中,gethoro_Click方法用于建立了DailyHoro类的一个实例,它包含在代理库文件中。接着调用DailyHoro类的GetHoro方法,Zodiac Sign作为输入参数被传递。返回的字符串信息被分割成日期和预言,单独地显示出来:
<html> <head> <script language="C#" runat="server"> private void gethoro_Click(object sender, EventArgs e) { file://Get the Selected Item from the DropDownList string sign = zodiac.SelectedItem.Text; file://Create a Instance of the Proxy Class DailyHoro dh = new DailyHoro(); file://Call the "GetHoro" method of the Web Service on the file://Proxy Object. The Proxy object in turn communicates file://to the Web Service. Remember the Proxy cannot do file://anything except act as a bridge between. Your Web file://Service and the client. It cannot replace the Web file://Service. string result =dh.GetHoro(sign); file://Extract the Date from the Result preddate.InnerHtml = "<b> Horoscope for " + sign + " as on " + result.Substring(0,10) + "</b>"; file://Display the Prediction predspace.InnerHtml=result.Substring(11); } </script> <title>Horoscope Service Client</title> </head> | 下面的代码用于创建下拉菜单项,其中包含了各种Zodiac Sign信息的名称。我们使用一个按钮来触发Web Service,使用2个DIV标记显示返回信息:
<body> <center> <form runat="server" > <table border="1" width="60%" cellpadding="1" cellspacing="2"> <tr> <td colspan=2> <b> Select your Zodiac Sign</b></th> </tr> <tr> <td> <asp:DropDownList id="zodiac" runat="server"> <asp:ListItem>aquarius</asp:ListItem> <asp:ListItem>pisces</asp:ListItem> <asp:ListItem>aries</asp:ListItem> <asp:ListItem>taurus</asp:ListItem> <asp:ListItem>gemini</asp:ListItem> <asp:ListItem>cancer</asp:ListItem> <asp:ListItem>leo</asp:ListItem> <asp:ListItem>virgo</asp:ListItem> <asp:ListItem>libra</asp:ListItem> <asp:ListItem>scorpio</asp:ListItem> <asp:ListItem>capricorn</asp:ListItem> <asp:ListItem>sagittarius</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td colspan="2" > <asp:Button onClick="gethoro_Click" Text="Fetch !" runat="server" /></td> </tr> <tr> <td colspan="2"><div id="preddate" runat="server" /> </td> </tr> <tr> <td colspan="2"><div id="predspace" runat="server" /> </td> </tr> </table> </form> </center> </body> </html> |
六、配置Web应用程序(Web Application)
现在我们通过另外一种方式Web Application来使用上面的Web Service。
1、首先将文件ClientHoro.aspx 拷贝到虚拟目录下,将代理Dll拷贝到虚拟目录下的Bin目录中。如果Bin目录不存在,就先创建它。
2、然后,编写客户端应用程序 HoroClient.cs:
/* Compilation csc /r:System.dll;System.Web.Services.dll;DailyHoro.dll HoroClient,cs */ using System ; using horo_service ; file://A class which consumes the Web Service public class HoroClient { public static void Main(string[] argv) { Console.WriteLine("Welcome to Horoscope Client"); Console.Write("Enter you Zodiac Sign:"); file://Read the Input from the user string sign = Console.ReadLine(); file://Create a instance of the Proxy Class DailyHoro dh = new DailyHoro(); file://Make a Call on the Web Service Method "GetHoro" and file://pass the Zodiac sign to it string result = dh.GetHoro(sign); Console.WriteLine("Horoscope for "+sign+" on " + result.Substring(0,10)); file://Print the Prediction Console.WriteLine(result.Substring(11)); Console.WriteLine("Press Enter to Exit"); Console.ReadLine(); } } |
上面的代码中包含了一个HoroClient类。类的方法Main从用户端接收输入信息Zodiac Sign,然后创建DailyHoro的实例对象,最后我们调用方法GetHorn得到预言信息。
3、最后,将HoroClient.cs进行编译:
csc /r:System.dll;System.Web.Services.dll;DailyHoro.dll HoroClient,cs |
编译成功后,生成可执行文件"HoroClient.exe",双击它开始应用Web Service。
上一页 1 2 | | | 感谢
访问天极网,如果您觉得该文章涉及版权问题,请看这里!
|
|