. NET C # Simple example of creating a WebService service

Source: Internet
Author: User

Web Service is a programmable Web-based application for developing distributed, interoperable applications and a Web service

The characteristics of WebService are as follows:

1. Use XML (Standard Universal Markup Language) as the format for data interaction.

2. Cross-platform, because the use of XML so long as the local application can connect to the network parsing XML can achieve data exchange, such as Android, IOS, WindowsPhone, etc. can achieve the WEB service data interaction.

3. Based on the HTTP protocol, directly across the firewall, general-purpose strong;

The following uses Visual Studio 2013 (which is also the case with other VS versions) to create a simple Web service service.

I. Open the new Web site, visual studio-> file

Second, select the ASP. NET site, then click "OK"

Third, after the new site you will see the solution manager see a Web. config, we can configure this file to implement the browser to WebService remote call. Now, add a WebService project to the project name in the right-click Solution .

After four, you can find the Web service (ASMX) in the project template and add it !

Create a WebService. cs file in the App_Code folder after creation

A webservice.asmx file is created at the root of the Web site

These two files WebService. CS is responsible for the logical part,WebService. asmx provides the service section. Future references to the WebService service will refer directly to the WebService. asmx file to invoke the WebService service, and the execution part of the service is the WebService.cs file

Now open the WebService. cs file to edit the logic section to implement a simple " client invoke Web service implementation of a simple addition operation, and return the results as a string to the client "

1 /*2 Web.Services.cs File3 */4 usingSystem;5 usingSystem.Collections.Generic;6 usingSystem.Linq;7 usingsystem.web;8 usingSystem.Web.Services;9 Ten /// <summary> One ///Summary description of WebService A /// </summary> -[WebService (Namespace ="http://tempuri.org/")] -[WebServiceBinding (ConformsTo =wsiprofiles.basicprofile1_1)] the //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line.  - //[System.Web.Script.Services.ScriptService] -  Public classWebService:System.Web.Services.WebService { -  +      PublicWebService () { -  +         //If you are using a design component, uncomment the following line A         //InitializeComponent (); at     } -  -     /// <summary> -     ///method [WebMethod] is the declaration of a Web service method, if you want to write a method to let the client call and return the results must be labeled on the method [WebMethod] -     ///If you are only responsible for logical operations or private methods and do not intend to give the client results, simply call inside the class method without declaring [WebMethod] -     /// </summary> in     /// <param name= "a" ></param> -     /// <param name= "B" ></param> to     /// <returns>Converts the result of an operation into a string return</returns> + [WebMethod] -      Public stringHelloWorld (intAintb) { the         intresult = A +b; *         returnresult. ToString (); $     }    Panax Notoginseng}

The above code writes a basic method for remote invocation, after saving the CS file, a simple Web service is created!

Let's start this project and use our browser to view the page where Microsoft defines the test for us:

Here we can see that the Hello method we wrote can be called, and we click into this method to go to the Invoke Debug page:

Input parameters, we write a simple addition operation, the parameter is we write the HelloWorld method parameters A and B, we enter two integer click Call, the following results appear:

The following is the result returned by the Web service, where 133 is the result returned by the HelloWorld parameter a+b:

1 <? XML version= "1.0" encoding= "UTF-8" ?> 2 <  xmlns= "http://tempuri.org/">133</string  >

Here, in the future we want to invoke a Web service to get the client from the server in the above way, Access: "Host name/web service name. asmx" can be called.

The resulting data is XML, so the client will need the client to parse the XML file after getting the data.

Note: The above creation of Web services called by the browser is only available for local computer debugging and requires the configuration of Web. config (previously mentioned) for Remote call debugging

If not configured, the remote call appears: The test form can only be used for problems that are resolved by a request from the local computer .

You can refer to the method if you want to make a remote call:

Locate the Web. config below the site's solution

After opening, add the following configuration:

1 <system.web>  2     <webservices>  3       <Protocols>  4         <Addname= "HttpGet"/>  5         <Addname= "HttpPost"/>  6       </Protocols>  7     </webservices>    8 <system.web>  

After the configuration, you can remotely through the browser to invoke the Webservice.asmx service, after the development test, for security, be sure to remove the configuration!

The next article will show you how to use WinForm to reference a Web service and get a return value!

. NET C # Simple example of creating a WebService service

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.