How To Create Asynchronous Web Service In Net
In this article I will show how to build and use asynchronous Web services in your application and how you can use the callback method to make your application work in asynchronous state. I made this application to calculate the speedup for the case when you want to run long process in different machines versus single machine. This application shows what jobs are running, what jobs have been finished and some other data related to the jobs such as time and data size.
BeginSortMachine1(parameter,CallBackFunction,0);
This is a call back function, when an asynchronous web service completed this function get called.public void OnCBMachine1(IAsyncResult ar){
try
{
if ( false == bIsBtn400Clicked)
{
mylist1[0] = st.EndSortMachine1(ar);
bMachineDone[0] = true ;
eTime[0] = System.DateTime.Now;
timeDiff[0] = eTime[0].Subtract(sTime[0]);
lblMachine1Time.Text = timeDiff[0].TotalMilliseconds.ToString();
lblMsg1.Text = "Finished Machine 1 Job.";
if ( bMachineDone[1] == true &&
bMachineDone[2] == true &&
bMachineDone[3] == true )
{
timeDiff[4] = eTime[0].Subtract(sTime[0]);
lblTimeTaken.Text = timeDiff[4].TotalMilliseconds.ToString();
double dTime = timeDiff[4].TotalMilliseconds / Int32.Parse(datasize4.Text);
avg4.Text = dTime.ToString();
task4.Text = (timeDiff[4].TotalMilliseconds / 4).ToString();
lblInfo.Text = "Job Completed";
}
}
else
{
mylist400[nCounterMachine1] = st.EndSortMachine1(ar);
bMachineDone[0] = true ;
eTime[0] = System.DateTime.Now;
timeDiff[0] = eTime[0].Subtract(sTime[0]);
TotalTimeFor400Tasks += timeDiff[0].TotalMilliseconds;
lblMachine1Time.Text = timeDiff[0].TotalMilliseconds.ToString();
lblMsg1.Text = "Finished Machine 1 Job # " + (nCounterMachine1+1).ToString();
lblTimeTaken.Text = TotalTimeFor400Tasks.ToString();
double dTime = TotalTimeFor400Tasks / Int32.Parse(datasize400.Text);
avg400.Text = dTime.ToString();
task400.Text = (TotalTimeFor400Tasks / nCurrentJobIndex).ToString();
if ( nCurrentJobIndex == 400)
{
lblInfo.Text = "Job Completed";
dTime = TotalTimeFor400Tasks / Int32.Parse(datasize400.Text);
task400.Text = (TotalTimeFor400Tasks / nCurrentJobIndex).ToString();
}
else
ServiceJobs();
}
}
catch (Exception ex)
{
lblInfo.Text = ex.Message.ToString();
}
} The attached project has two parts - the Web service and the client application. The Web Service This is an XML web service, which has four web methods, that sorts the data using (O)n2 algorithm. Creating and XML web service is a very easy task in .NET. Tricky part is I want to use these method asynchronously. When you create a web service in .NET, it creates that method asynchronous and synchronous both. All you need to add is a Begin keyword, if you want to use that method as asynchronous ,also you need to assign a callback function, which is called by web method, when web method is completed. I create XML web services that are running on four different machine, and these web services contains the function for the sorting of data ( On2) algorithm. This web service is called DataProcess and four functions.
2. Send first four tasks to four machines, and keep the counter of current job.
3. We call all jobs asynchronously, so as soon as I get a callback function from machine, I send another job on line and increment the counter.
4. Keep doing this until counter reaches 400 ( means all jobs are dispatched).
5. Now wait for last job to complete and as soon as you get that callback, mark that time.
6. Take a difference of this time from the start time, that is your total time taken from this method. The test analysis report of single machine looks like following All time are in milliseconds.
How To Create Asynchronous Web Service In Net
Source: https://www.c-sharpcorner.com/article/building-and-using-asynchronous-web-services/
Posted by: hulingslithend.blogspot.com
0 Response to "How To Create Asynchronous Web Service In Net"
Post a Comment