C # simple example of WebService

Source: Internet
Author: User

Is WebService. in terms of concept, it may be complicated, but we can have a macro understanding that WebService is an external interface with functions that can be called by external customers (note: there are also functions that cannot be called by the customer ). If we are a server, we have written a WebService and then gave it to the customer (and we also gave them the call rules ), the customer can be in a relatively transparent State when obtaining information from the server. Even if the customer does not understand (and does not need to) the process, they only obtain data.

The data transmitted by WebService can only be serialized data, typically XML data.

The following is a simple example:

(1) Create --- project --- Visual C # --- web --- ASP. NET web service application, named testwebservice

The directory structure is as follows:

We changed service1.asmx to myservice. asmx.

Modify the class name in the file at the same time.

Public class myservice: system. Web. Services. WebService
{

[Webmethod]
Public String helloworld ()
{
Return "Hello World ";
}
}

Modify the file myservice. asmx at the same time (right-click to view the tag; if you double-click to open the. CS file in the vs environment)

<% @ WebService Language = "C #" codebehind = "myservice. asmx. cs" class = "testwebservice. service1" %> changed

<% @ WebService Language = "C #" codebehind = "myservice. asmx. cs" class = "testwebservice. myservice" %>

 

(2) regenerate the project, right-click myservice. asmx, and select view in the browser to check whether the project has syntax errors.

(3) Add the specified method in myservice. asmx

Using system;
Using system. Data;
Using system. Web;
Using system. collections;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. componentmodel;

Namespace testwebservice
{
/// <Summary>
/// Summary of service1
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Toolboxitem (false)]
Public class myservice: system. Web. Services. WebService
{

[Webmethod] // required. In order to describe the following method, each method must be prefixed.
Public String getname ()
{
Return "hope ";
}

[Webmethod]
Public String getage ()
{
Return "25 ";
}

}
}

Regenerate the project, right-click myservice. asmx, and select View in browser. The effect is as follows:

 

 

(4) publish on the Internet

Here I tested it on the local machine, so there is no need to publish it. If you want to publish it to the Internet, we can use:

Upload the files in the binfile and the asmx files in the same directory as the Bin (including. dll and. PDB files) to the Internet.

(5) use the web service interface.

Create a common Windows application, right-click, and add web reference ----

Change the Web reference name to hopewebservice.

Now, we can use the WebService method. Using hopewebservice, we can access two methods.

(6) usage:

Hopewebservice. myservice OBJ = new hopewebservice. myservice ();
MessageBox. Show ("name is:" + obj. getname () + "; age is:" + obj. getage ());

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.