Create Webservice+flash simple calculator with vs.net

Source: Internet
Author: User
web| Create ' =====================
' Reprint please keep the following information
' Original: Ouyangdongjie
' Http://www.cnsxml.com
'=====================
After reading this tutorial, you will find that WebService is really simple on. NET!
We're going to use vs.net to create a simple webservice, and his function is to do simple arithmetic.
Then create a flash to call this webservice


First, create WebService
1. Open Visual Studio. NET.
2. Create a new Active Server Pages (ASP). NET Web Service project. Name the Web service Oydjservice and, if necessary, designate its location as an appropriate

When the WEB server that is running asp.net.
3. Change the name of the Solution file to Oydjservice to maintain consistency.
4. Change the name of the default Web service created with Service1.asmx to Oydjservice.asmx.
5. Click here in the design environment to switch to Code view to switch to Code view.
6. Define the methods used to encapsulate your service functionality. Each method provided by this service must be marked with a WebMethod attribute before it. If this property is not available, the service will not expose this

Method.

Note: Not every method requires a WebMethod attribute. It is useful to hide some of the implementation details that are called by the public Web service method, which is also used in local applications

In the case of the WebService class, you also need to hide some implementation details. Local applications can use any common class, but only methods with WebMethod properties can act as Web

Service remote access.

Add the following code to the Oydjservices class you created earlier:

Program code:
<webmethod () > Public Function Add (A As Integer, B as Integer) As Integer
return (A + b)
End Function

<webmethod () > Public Function Subtract (A as System.Single, B as System.Single) as System.Single
Return A-b
End Function

<webmethod () > Public Function Multiply (A as System.Single, B as System.Single) as System.Single
Return A * B
End Function

<webmethod () > Public Function Divide (A as System.Single, B as System.Single) as System.Single
If B = 0
Return-1
End If
Return Convert.tosingle (A/b)
End Function




Build Web Service: From the Build menu, select Build.
You can test this Web service by going to the Oydjservice.asmx Web service page. If your page is placed on the local computer, the URL will be

Http://localhost/OydjService/OydjService.asmx.

Runtime asp.net will return a Web service help page to illustrate this Web service. This page also allows you to test different Web service methods.


Second, create Flash to invoke this webservice

1. Create a new Flash
2. Drag into 2 text boxes (textarea), respectively, named: T1, T2
3. Drag into a drop-down menu (ComboBox), named: YSF, and fill in the "Data" property: "[1,2,3,4]", "Labels" property: "[+,-, *,÷]"
4. Drag into a dynamic text box, named: Convert
5. Drag into a button, named: Search
6. Drag into a Webserviceconnector component
7. Edit the "action" at the first frame and add the following code to the

Program code:
Import mx.services.*;

Set WSDL Address
var Wsdluri = "Http://localhost/OydjService/OydjService.asmx?WSDL";
WS = New WebService (Wsdluri);

Click the button and start connecting WS and COMPUTE
Search.onpress=function () {
For calculation
Switch (ysf.selectedItem.data) {
Case 1:
callback = ws. ADD (T1.text,t2.text);
Break

Case 2:
callback = ws. Subtract (T1.text,t2.text);
Break

Case 3:
callback = ws. Multiply (T1.text,t2.text);
Break

Case 4:
callback = ws. Divide (T2.text,t1.text);
Break

}
Web Service successfully passed back information
Callback.onresult = function (Result) {
Convert.text=result;
}
Web Service error message return
Callback.onfault = function (fault) {
For (i in fault) {
Trace (i+ ":" +fault[i]);
}
}
}





Third, demonstration

(Because my webservice server is not very stable, it may not be displayed correctly)

Third, flash original file download
Http://cnsxml.com/demo/webs_flash/jj.fla



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.