Callbacks mechanism for Web Service in .NET (5)

zhaozj2021-02-17  62

3. Web Form

For webservice "a", the browser we use is used as the client. Each Web Method has its own button trigger event, and saves the status of the SESSION class in the stack. The update of the page is based on the following mechanism: The event handler presses the data to the session stack, extracts up and inserts into the Listbox Control when Page_Load. Another issue is to call the DosomeWorka method in asynchronously, which can generate and process Callback, which is why we can send another request to WebService "a". Note that each work is identified by its verification 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 vidinitiRizeComponent ()

{

// ...

}

#ndregion

// Call the Web Service Asynchronously

Private void ButtosomeWorka_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);

}

// clean-up the listbox

Private void ButtonClear_Click (Object Sender, System.EventArgs E)

{

Listboxcallbackstatus.Items.clear ();

}

}

}

OK, we have started testing now. The above photos shows the user interface of the web. First, make sure your network is online and is in an enterprise internal local area network. Click on the DOSMEWORK button and request the status result. The status and abort buttons can click on the easy time. The ListBox control will display a current state of a work (get it by verification bill)

in conclusion:

Web Service using the application model creates a new era of distributed architecture. Using .NET Framework dynamic calling Web methods in an enterprise model hierarchy is simple and straightforward. This article shows how it is implemented using a C # language. This solution is well explained in the relevant concepts and designs. If you really invest in the market, you need to consider many factors, such as safety design, complete URL addressing, password, server agent, etc.

转载请注明原文地址:https://www.9cbs.com/read-29059.html

New Post(0)