Callbacks mechanism for using WEB service in. NET (5)

Source: Internet
Author: User
Tags ticket
Web 3. Web Form
For WebService "A" we use the browser as the client. Each WEB methods has its own button-triggering event, which holds the state of the session class in stacks (Stack). The update of the page is based on the following mechanism: The event handler presses the data onto the session stack, extracts it at Page_Load, and inserts it into the ListBox control. Another issue is the asynchronous invocation of the Dosomeworka method, which generates and processes callback, which is why we can send another request to WebService "A". Note that each job is recognized by its validation ticket ID.
Namespace Webformcallbackws
{
public class WebForm1:System.Web.UI.Page
{
// ...
protected ServiceA sa = new ServiceA ();

Public WebForm1 ()
{
Page.Init + = new System.EventHandler (Page_Init);
}

private void Page_Load (object sender, System.EventArgs e)
{
if (IsPostBack = = False)
{
Initialize controls, one time!
if (session["Status"] = = null)
session["Status"] = stack.synchronized (new Stack ());
}
Else
{
Stack stack = session["Status" as stack;
while (stack. Count > 0)
LISTBOXCALLBACKSTATUS.ITEMS.ADD (Stack. Pop (). ToString ());

int numberofitems = ListBoxCallbackStatus.Items.Count;
if (Numberofitems > 13)
Listboxcallbackstatus.selectedindex = numberOfItems-13;
}
}
private void Page_Init (object sender, EventArgs e)
{
// ...
}

#region Web Form Designer generated code
private void InitializeComponent ()
{
// ...
}
#endregion

Call the Web service asynchronously
private void Buttondosomeworka_click (object sender, System.EventArgs e)
{
int count = Convert.ToInt32 (Textboxcount.text);
String ticket = Textboxticketid.text;
//
AsyncCallback callback = new AsyncCallback (Callbackdosomeworka);
IAsyncResult ar = sa. Begindosomeworka (count, ticket, callback, NULL);
LISTBOXCALLBACKSTATUS.ITEMS.ADD (String. Format ("#{0} start ...",
Ticket));
}
The call callback from the WebService
private void Callbackdosomeworka (IAsyncResult ar)
{
string retval = sa. Enddosomeworka (AR);
Stack stack = session["Status" as stack;
Stack. Push (retval);
}
Call the Web service
private void Buttonabort_click (object sender, System.EventArgs e)
{
Stack stack = session["Status" as stack;
Stack. Push (SA. Abortworka (Textboxticketid.text));

}
Get the status from the Web service
private void Buttonrefresh_click (object sender, System.EventArgs e)
{
Stack stack = session["Status" as stack;
Stack. Push (SA. Getstatusworka (Textboxticketid.text));
}
Clean-up the listbox
private void Buttonclear_click (object sender, System.EventArgs e)
{
ListBoxCallbackStatus.Items.Clear ();
}
}
}
OK, let's start the test now. The above photos show a Web-form user interface. First, make sure your network is online and in an intranet. Click on the Dosomework button, and then request a status result. Status and Abort buttons can be clicked in easy time. The ListBox control will show the current status of a work (obtained by a validation ticket)
Conclusion:
WEB Service using the application model pioneered a new era of distributed architecture. It is simple and straightforward to use the. Net framework dynamic Invoke Web methods in the enterprise model hierarchy. This article shows how it is implemented using the C # language. This solution is a good explanation of the related concepts and design-time problems. If really put into the market, but also need to consider a number of factors, such as security design, complete URL addressing, passwords, server agents and so on.

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.