Web Services Guide: Some examples of Web services

Source: Internet
Author: User

Based on a Web service architecture, we create the following two components as part of a Web service implementation:
Service provider or publisher it is the provider of the Web service. The service provider implements the Web service and allows it to be accessed over the Internet or intranet.
Next we will use the. NET SDK to write and publish a simple Web service.
Service requester or consumer it is the consumer of the Web service. The requestor leverages a ready-made Web service by opening a network connection and sending an XML request.
Next we will also write two Web service requesters: A Web-based consumer (ASP. NET application) and a consumer based on a Windows app.
Next is an example of our first Web service, which exposes two methods as a service provider (AddAndSayHello) as the Web service that will be used by the application. This is a standard template for Web service.. NET Web Service uses the. asmx extension. Note that one way to expose a Web service is to haveWebMethodProperty. Save this file in the IIS virtual directory asFirstservice.asmx。
Firstservice.asmx
<%@ WebService language = "C" class = "FirstService"%>using system;using system.web.services;using System.Xml.Seri Alization; [WebService (namespace= "http://localhost/MyWebServices/")]public class firstservice:webservice{   [WebMethod] public   int Add (int a, int b)   {      return a + b;   }   [WebMethod]   Public String SayHello ()   {   return ' Hello world ';   }}

To test a Web service, you first have to publish it. A Web service can be published either in the intranet or on the Internet. We will publish this Web service on IIS running on an intranet machine. Let's start with configuring IIS first.
    • open → settings → Control Panel → Administrative Tools → Internet Service Manager
    • Expand and Right-click the Default Web site, and select new → virtual directory. The Virtual Directory Creation Wizard will be opened. Click Next.
    • The virtual directory Aliases screen will be opened. Enter the virtual directory name. For example,mywebservices, click Next.
    • The Site Content Directory screen will be opened.
    • Enter the directory path name for the virtual directory. For example,c:\MyWebServices, click Next.
    • The access permission screen will be opened. Make changes to your settings based on your needs. In this example we leave the default setting.
    • Click the Next button. It will complete the IIS configuration.
    • Click Finish to end the configuration.
To verify that IIS is configured correctly, copy an HTML file (for example,x.html) to the virtual directory created above (C:\MyWebServicesIn After that, open IE and enterhttp://localhost/MyWebServices/x.html。 can openx.htmlThe file is normal.
Note: If not openx.html, try tolocalhostReplace with the IP address of your machine. If it still does not, check whether IIS is running or reconfigure IIS and the virtual directory.
Then verify that the Web service willFirstservice.asmxCopy to the IIS virtual directory created above (C:\MyWebServices)。 Open the Web service in IE (Http://localhost/MyWebServices/FirstService.asmx)。 Being able to open your Web service page is normal. The page should be able to link to two methods that you expose as a Web service. Congratulations! You've already written your first web service!.
Test Web service as you just saw, it's easy to write a Web service under the. NET Framework. Writing a Web service consumer under the. NET framework is also straightforward. As mentioned above, we will write two types of service consumers, one web-based and another Windows-based application. Now let's write our first web service consumer.
Web-based service consumers write a web-based consumer as follows. Name it aswebapp.aspx。 Note that it is an ASP. Save it with the Web Service Test virtual directory (C:\MyWebServices\WebApp.axpx)。
The app has two input boxes for getting the user added. It has an Execute button that will get when clickedADDAndSayHelloTwo Web service.
Webapp.axpx
<%@ page language= "C #"%><script runat= "Server" > void Runsrvice_click (Object sender, EventArgs e) {Firs      TService mysvc = new FirstService ();      Label1.Text = Mysvc.sayhello (); Label2.Text = Mysvc.add (Int32.Parse (Txtnum1.text), Int32.Parse (Txtnum2.text)).   ToString (); }</script>FirstSevice.cs file in the current directory. We need to compile it to create a FirstService.dll(proxy) for the Web service. Related commands:c:> WSDL http://localhost/MyWebServices/FirstService.asmx? WSDL;c:> csc/t:library FirstService.cs.
  
  • Place the compiled agent into the bin directory (C:\MyWebServices\bin) in the virtual directory of the Web service. The Internet Information Service IIS looks for agents in this directory.
  • Also create service consumers. Note A Web service proxy object will be instantiated in the consumer. This agent is responsible for interacting with the service.
  • Enter the consumer URL in IE to test (for example,http://localhost/MyWebServices/WebApp.aspx).
  • Windows App-based Web service consumers write a Web service consumer that is based on a Windows app is the same as writing any other Windows app. All you need to do is create the agent (as we described above) and reference the agent when compiling the application. Here are our Windows apps that use Web service. The app creates a Web service object (of course, just a proxy) and calls itsSayHelloAndADDMethod.
    WinApp.cs
    Using system;using system.io;namespace Svcconsumer {   class Svceater {public         static void Main (string[] args) {
       firstservice mysvc = new FirstService ();         Console.WriteLine ("Calling Hello World Service:" + Mysvc.sayhello ());         Console.WriteLine ("Calling Add (2, 3) Service:" + Mysvc.add (2, 3). ToString ());}}}   

    Using commandsC:\>csc/r:firstservice.dll WinApp.csCompile it,WinApp.exewill be created, execute it to test the app and Web service.
    Now, the question arises: how can you ensure that the application is actually invoking a Web service?
    Verification is actually very simple. Turn off your Web server so that the Web service cannot connect. Then, executeWinAppApplication. It will throw a run-time exception. Now, start the Web server again,WinAppThe application will work properly.
    Original link: http://www.tutorialspoint.com/webservices/web_services_examples.htm

    Web Services Guide: Some examples of Web services

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.