Use the Callbacks mechanism for Web Service in .NET (4)

zhaozj2021-02-17  67

test

Web Service Callbacks

The concepts and design can be tested using the following three subplices in the HTTP: // localhost virtual directory. The summary of the test is already in the above paragraph (Web Service Callback section)

Description. All projects have already joined the trace breakpoint so that we can clearly see the process of running the program (http://www.systeminternals.com/). Here are their implementation code:

Web service a

Namespace WebServicea

{

Public Class Servicea: System.Web.Services.Webservice

{

Public servicea ()

{

// Codegen: this call is required by the ASP.NET Web Services Designer

InitializationComponent ();

Trace.Writeline (String.Format ("[{0}] servicea.ctor", gethashcode ()));

}

#Region Component Designer Generated Code

Private vidinitiRizeComponent ()

{

}

#ndregion

Protected Override Void Dispose (Bool Disposing)

{

TRACE.WRITELINE (String.Format ("[{0}] servicea.dispose", gethashcode ());

}

[WebMethod]

Public String DosomeWorka (int COUNT, STRING Ticket)

{

Trace.WriteLine (String.Format ("[{0}] servicea.dosomeworka start ...",

GetHashcode ()));

INT startTC = environment.tickcount;

// Current State

Global.State [Ticket] = "The Job Has Been Started";

String state = ticket;

// Location of the source / target web services -

// (Hard Coded for Test Purpose Only!)

String mywsdlurl = "http://localhost/webservicea/servicea.asmx? WSDL";

String TargetWsdlurl = "http://localhost/webserviceb/serviceb.asmx? WSDL";

// Call The Webservice B

WebServiceAccessor WSA = New WebServiceAccessor (TargetWsdlurl);

Object esobj = wsa.createInstance ("serviceb");

Object retval = wsa.invoke (esobj, "begindosomeworkb", count,

MYWSDLURL, State, Null, NULL;

// Wait for the call to completion

WebClientAsyncResult Ar = RetVal as WebClientAsyncResult;

ar.asyncwaithandle.waitone ();

// Retrieve A Result

Object result = wsa.invoke (esobj, "enddosomeworkb", ar); int durationtc = environment.tickcount - starttc;

Trace.writeline (String.Format ("[{0}] servicea.dosomeworka done in {1} ms",

GetHashcode (), DurationTC);

//

Global.State.Remove (Ticket);

Return Result.toString ();

}

[WebMethod]

Public Bool CallbackServicea (String Sender, String Xmleventarg)

{

WebServiceEventArgs EA = (WebServiceEventArgs) xmleventarg;

String msg = String.Format

"[{0}] servicea.callbackServiceA ({1}, [{2}, {3}, {4}]",

GetHashcode (), Sender, Ea.Name, Ea.State, Ea.Param;

IF (Global.State.Containskey (EA.STATE))

{

Global.State [Ea.State] = String.Format ("{0}, [{1}, {2}, {3}]",

Sender, Ea.Name, Ea.State, Ea.Param;

Trace.WriteLine (MSG);

Return True;

}

Return False;

}

[WebMethod]

Public String AbortWorka (String Ticket)

{

Trace.writeline (String.Format ("[{0}] servicea.abortworka",

GetHashcode ()));

IF (Global.State.Containskey (Ticket))

{

Global.State.Remove (Ticket);

Return string.format ("# {0} Aborted.", Ticket);

}

Return string.format ("# {0} doesn't exist.", ticket);

}

[WebMethod]

Public String getStatusWorka (String Ticket)

{

IF (Global.State.Containskey (Ticket))

{

Return string.format ("# {0} status: {1}", ticket, global.state [ticket]);

}

Return string.format ("# {0} doesn't exist.", ticket);

}

}

}

Outline:

Namespace WebServicea

{

Public Class Global: System.Web.httpApplication

{

Static public hashtable statin = null;

Protected void Application_Start (Object Sender, Eventargs E)

{

State = hashtable.synchronized (new hashtable ());

TRACE.WRITELINE ("[{0}] servicea.application_start",

GetHashcode ()));

}

// ...

Protected Void Application_END (Object Sender, Eventargs E)

{

State.clear ();

Trace.writeline ("[{0}] servicea.application_end",

GetHashcode ()));

}

}

}

Requests and Callback Web Methods run in different sessions, which is why Status is saved in a shared resource class (such as a HashTable). Each client's request has a separate ticket credential, which is a communication key between Web Service and the global site (cookie)

.

2.

Web service

B

This web service is very simple. Only one Web Methods simulates some work. During the operation, the service starts to enable Callback Web Methods. Work in synchronization is running some cyclic operations. Callback Web Methods of WebService "A", based on its return value, can be automatically stopped based on its return value. Sustain. You can keep the status in the session.

Namespace WebServiceB

{

Public Class Serviceb: System.Web.Services.WebService

{

Public serviceb ()

{

// Codegen: this call is required by the ASP.NET Web Services Designer

InitializationComponent ();

TRACE.WRITELINE (String.Format ("[{0}] serviceb.ctor, gethashcode ()));

}

#Region Component Designer Generated Code

Private vidinitiRizeComponent ()

{

}

#ndregion

Protected Override Void Dispose (Bool Disposing)

{

Trace.Writeline (String.Format ("[{0}] serviceb.dispose", gethashcode ()))));

}

[WebMethod (EnableSession = true)]

Public String DosomeWorkb (int COUNT, String Callbackws, String Statews)

{

Trace.writeline (String.Format ("[{0}] serviceb.dosomeworkb start ...",

GetHashcode ()));

INT startTC = environment.tickcount;

// async call to the servicea.callbackServicea Method

WebServiceAccessor WSA = New WebServiceAccessor (Callbackws);

Object esobj = wsa.createInstance ("servicea");

// Prepare the Callback Arguments: Sender, Eventargs

String sender = gettype (). fullname;

WebServiceEventArgs EA = New WebServiceEventArgs ();

EA.NAME = "this is a callback";

Ea.state = statews;

For (INT II = 0; II <(count & 0xff); II ) // max. count = 255

{

Ea.Param = II;

String Xmleventargs = EA;

Object retval = wsa.invoke (esobj, "begincallbackservicea",

Sender, Xmleventargs, NULL, NULL;

// Simulate Some Task

Thread.sleep (250);

// Wait for the call to completion

WebClientAsyncResult Ar = RetVal as WebClientAsyncResult;

ar.asyncwaithandle.waitone ();

// Result

Object Result = WSA.Invoke (esobj, "endcallbackservicea", ar);

IF (Bool) Result == FALSE)

{

Trace.WriteLine (String.Format)

"[{0}] serviceb.dosomeworkb Has been Aborted", gethashcode ()))))));

Return string.format ("# {0} Aborted During the program of {1}.",

Statews, II);

}

}

//

Int durationtc = environment.tickcount - starttc;

TRACE.WRITELINE (String.Format ("[{0}] serviceb.dosomeworkb done in {1} ms",

GetHashcode (), DurationTC);

Return string.format ("# {0} done in {1} ms", statews, durationtc);

}

}

}

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

New Post(0)