. Net c # a simple example of creating a WebService,

Source: Internet
Author: User

. Net c # a simple example of creating a WebService,

Web service is a programmable web-based application that is used to develop distributed interoperability applications. It is also a web service.

WebService has the following features:

1. Use XML (standard common Markup Language) as the format of data interaction.

2. cross-platform performance. Because XML is used, data exchange can be realized as long as the local application can connect to the network to parse XML, such as Android, IOS, and WindowsPhone.

3. Based on the HTTP protocol, the firewall is directly crossed and the general type is strong;

The following uses Visual Studio 2013 (the same is true for other VS versions) to create a simple Web service.

 

1. Open Visual Studio-> file-> New-> website

 

2. select ASP. NET empty website and click "OK"

3. After creating a website, you will see a web. config in solution manager. Later, We can configure this file to implement remote WebService calls by the browser. Right-click the project name in the solution to add a WebService project.

4. You can find it in the Project template.Web Service (ASMX), addIt!

5. Create a WebService. cs file in the APP_Code folder.

Create a WebService. asmx file under the root directory of the website.

Among the two files, WebService. cs is responsible for the logic section and WebService. asmx provides the service section. When you reference the WebService in the future, the WebService. asmx file will be directly referenced to call the WebService. The execution part of the service is the WebService. cs file.

Now open the WebService. cs file to edit the logic part and implement a simple "client calls web service to implement a simple addition operation, and return the operation result sent to the client using a string".

 

1/* 2 Web. services. cs file 3 */4 using System; 5 using System. collections. generic; 6 using System. linq; 7 using System. web; 8 using System. web. services; 9 10 /// <summary> 11 /// summary of WebService 12 /// </summary> 13 [WebService (Namespace =" http://tempuri.org/ ")] 14 [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)] 15 // to allow ASP. net ajax to call this Web service from a script, uncomment the following line. 16 // [System. web. script. services. scriptService] 17 public class WebService: System. web. services. webService {18 19 public WebService () {20 21 // If the designed component is used, uncomment the following line 22 // InitializeComponent (); 23} 24 25 /// <summary> 26 // The [WebMethod] header on the method declares a web service method, if you want to write a method that allows the client to call and return results, you must first mark [WebMethod] 27 // if you are only responsible for logical operations or private methods, it is not intended to give the client results, no need to declare [WebMethod] 28 /// </summary> 29 /// <param name = "a"> </param> 30 /// <param name = "B"> </param> 31 // <returns> convert the operation result to a string and return the result </returns> 32 [WebMethod] 33 public string HelloWorld (int a, int B) {34 int result = a + B; 35 return result. toString (); 36} 37}

The above code writes a basic method for remote calling. After saving the cs file, a simple web Service is created!

Next we will start this project and use a browser to view the call page for the test defined by Microsoft:

Here we can see that the Hello method we wrote can be called, and we click to enter this method to enter the call debugging page:

The input parameter is a simple addition operation. The parameter is the parameters a and B of the HelloWorld method we wrote. We enter two integers and click call. The following result is displayed:

The following is the result returned by the web service. 133 is the result returned by HelloWorld parameter a + B:

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

By now, if you want to call a web service, you can obtain the client data from the server in the preceding way. You can access "host name/web Service name. asmx" to call the service.

The obtained data is XML, so the client needs to parse the XML file after obtaining the data.

Note: The web Service created in the browser is only used for debugging on the local computer. You must configure web. config (previously mentioned) to debug remote calls.

If this parameter is not configured, a remote call may result in a problem where the test form can only be used for requests from local computers.

For remote calls, refer to the following method:

Find web. config at the bottom of the website Solution

Add the following Configuration:

1 <system.web>  2     <webServices>  3       <protocols>  4         <add name="HttpGet"/>  5         <add name="HttpPost"/>  6       </protocols>  7     </webServices>    8 <system.web>

After configuration, You can remotely call the webservice. asmx service through a browser. After development and testing, you must remove this configuration for security purposes!

Related Article

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.