Use C # To create a simple Web Service

Source: Internet
Author: User

Next, let's take a look at how to establish and deploy a simple Web service.
Create a Web Service
1. Create a Webservice directory under the wwwroot directory.
2. Create the following file:
<% @ WebService Language = "c #" Class = "AddNumbers" %>
Using System;
Using System. Web. Services;
Public class AddNumbers: WebService
{
[WebMethod]
Public int Add (int a, int B ){
Int sum;
Sum = a + B;
Return sum;
}
}
 
3. Save this file as AddService. asmx (asmx is the extension) and save it to the Webservice directory.
4. Now we have created a Web service and are ready for use by the client.
5. Now, you can use the following URL to access this Web Service:
Http: // ip address/Webservice/Addservice. asmx/Add? A = 10 & B = 5
The result is returned in XML format.
Deploy this service on the client
1. Enter the following in the command line:
WSDL http: // ip address/WebService/MathService. asmx/n: NameSp/out: FileName. cs
This operation creates a file called FileName. cs.
Description: WSDL refers to WebServices Description Language. This Program is in the Program FilesMicrosoft. NETFrameworkSDKBin directory.
NameSp is the name of the namespace we set and will be used in the implementation code of the client that deploys this service later.
2. Compile
CSC/t: library/r: system. web. dll/r: system. xml. dll FileName. cs
The above command will generate a dll file named the name of the public class in the above asmx file. In our example, it is: AddNumbers. dll
3. Place the generated dll file in the wwwrootin directory of the deployment machine.
Call this Web service in asp/aspx of the deployment Machine
<% @ Import Namespace = "NameSp" %>
<Script language = "c #" runat = "server">
Public void Page_Load (object o, EventArgs e ){
Int x = 10;
Int y = 5;
Int sum;
// Instantiating the public class of the webservice
AddNumbers AN = new AddNumbers ();
Sum = AN. Add (x, y );
String str = sum. ToString ();
Response. writeline (str );
}
</Script>

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.