ASP.NET 4.5 and ASP.NET Core 1 Hosting BLOG

Tutorial and Articles about ASP.NET 4.0 and the latest ASP.NET 4.5 Hosting

ASP.NET 4.5 Hosting - ASPHostPortal.com :: Asynchronous WCF service calls using .NET Framework 4.5

clock October 10, 2013 06:22 by author Ben

The Windows Communication Foundation (WCF) is an application programming interface in the .NET Framework for building connected, service-oriented applications. A WCF client has two ways to access the functions provided by WCF services. They are synchronous and asynchronous WCF calls. This article i wanna tell you how to asynchronous WCF service calls using .NET Framework 4.5. To demonstrate examples, I am using Windows 7 and Visual Studio 2010.

1. Open Visual Studio . It will create a Web service called MyAsync.asmx with a sample web method inside.


You can see the code snippet of the web method below: 

[WebMethod]
public string MyTestAsynchronousMethod(string strName, int
waitTime)
{
  System.Threading.Thread.Sleep(waitTime);
  return "Hei..." + strName + "Iam Called Asynchronously"; 
}

2. And also we create a consuming ASP.NET web application that looks like the below screenshot.



Now the only thing we have to do is call the web service in the button click event.

To call the application on the button click event we have to create a proxy. As all of you know we can create a proxy using WSDL.EXE and also by adding a web reference to the consuming application. Here I am going to use the second approach, i.e., using service reference for ease.

Right click the consuming web application, go to the Add Service Reference option, Then go ahead with adding the service reference in your consuming application. I have added a web reference in the consuming application with name MyProxy.

So we are all set to call the service from our button click.

In the asynchronous button click,I am going to create the proxy class object and looking at the intelisense, we can see that there are mainly three things related to our web method which we have created. 1 event and two methods,one with an async augment.

Here we are going to use the event called MyTestAsynchronousMethodCompleated and the web method MyTestAsynchronousMethodAsync.

You might think that from where the web method MyTestAsynchronousMethodAsync does and the event MyTestAsynchronousMethodCompleated came from, right ?
The answer is it will come by default while creating the proxy of the web service.

The web method MyTestAsynchronousMethodAsync is the hero who makes the asynchronous call happen and the event MyTestAsynchronousMethodCompleated is the supporting actor of the hero.

You can see the event and the method in the below snapshot.



3. The next thing we have to do is to plumb the event. As I mentioned earlier, the event is responsible to return the result after the execution of the web service method.  So I am going to set a label in our consuming application in such a way that it will display the returned string from the web service method call.
For that purpose I have added an event handler for that event. Visual Studio will create the event handler stub automatically for us. Just type += next to the event which we have and hit Tab key twice in your keyboard. You have got your event handler stub with the arguments and the parameters set. The only thing you have to do is write the business logic inside the event handler stub. That Visual Studio doesn't know, I mean your business logic.
This is the code :



4. You can see a property called Result in the event argument. That’s the property that gives you the web method executed Result, Error etc., in string format, it’s not the service method itself as before.
So let’s go and implement the logic in the stub by assigning the result to the label text property (you can see this in the below snapshot). We are going to call the second web method which is generated automatically.

namespace AsyncConsumer
{
    public partial class _Default : System.Web.UI.Page
    {
        Stopwatch objSW = new Stopwatch();
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void BtnAsync_Click(object sender, EventArgs e)
        {
            objSW.Reset();
            objSW.Start();
            MyProxy.MyAsync objProxy = new MyProxy.MyAsync();
            objProxy.MyTestAsynchronousMethodCompleted += 
              new MyProxy.MyTestAsynchronousMethodCompletedEventHandler(
              objProxy_MyTestAsynchronousMethodCompleted);
            objProxy.MyTestAsynchronousMethodAsync(txtName.Text, 
              Convert.ToInt32(txtWaitTime.Text));
            DoSomeLongJob();
            lblExecTime.Text = "Total Execution Time :" + 
              objSW.ElapsedMilliseconds.ToString() + " Milliseconds";
        }
        void objProxy_MyTestAsynchronousMethodCompleted(object sender, 
             MyProxy.MyTestAsynchronousMethodCompletedEventArgs e)
        {
            lblResult.Text = e.Result;
        }
        private void DoSomeLongJob()
        {
            System.Threading.Thread.Sleep(5000);
        }
    }
} 


5. I have added a method called DoSomeLongJob(). Which makes the thread to wait for 5 seconds for the consuming application and a Label to display the execution time.The above is the complete code snippet which we have done.
Let’s see the execution of our application.



What would be the result and execution time?
In a normal case this will take approximately 7 seconds delay, that is 2 seconds which we have given, and the 5 seconds of the DoSomeLongJob();
right?

Let’s see how long will it take. 

Ooopsss!!! Unfortunately or fortunately I got an exception like below while clicking the button:



6. Did you notice the marked portion in the exception?
Yes, we have to add and set the Async attribute to true in the Page directive of the page in which we are planning to have any asynchronous web service method call. Otherwise it’s not possible. Let’s go and set it.

After setting the Async attribute the directive will be like:



7. And the last, reload your page.



Free ASP.NET hosting - ASPHostPortal.com :: ASPHostPortal.com Proudly Announces Free Trial Windows ASP.NET Hosting

clock October 3, 2013 10:11 by author Ben

ASPHostPortal.com is a premier Windows and ASP.NET Web hosting company that specializes in Windows and ASP.NET-based hosting. We proudly announces 7 Day Free Trial Windows and ASP.NET Hosting to all new customers. The intention of this FREE TRIAL service is to give our customers a "feel and touch" of our system. This free trial is offered for the next 7 days and at anytime, our customers can always cancel the service.

The 7 Day Free Trial is available with the following features:

- Unlimited Domains
- 5 GB Disk Space
- 60 GB of Bandwidth
- 2 MS SQL Database
- Unlimited Email Account
- Support ASP.NET 4.5
- Support MVC 4.0
- Support SQL Server 2012
- Free Installations of ASP.NET And PHP Applications

ASPHostPortal.com believes that all customers should be given a free trial before buying into a service and with such approach, customers are confident that the product / service that they choose is not faulty or wrong. Even we provide free trial service for 7 days, we always provide superior 24/7 customer service, 99,9% uptime guarantee on our world class data center. On this free trial service, our customer still can choose from our three different data centre locations, namely Singapore, United States and Amsterdam (The Netherlands)

Anyone is welcome to come and try us before they decide whether or not they want to buy. If the service does not meet your expectations, our customer can simply cancel before the end of the free trial period.

For all the details of packages available visit ASPHostPortal.com

About ASPHostPortal.com:

ASPHostPortal.com is a hosting company that best support in Windows and ASP.NET-based hosting. Services include shared hosting, reseller hosting, and sharepoint hosting, with specialty in ASP.NET, SQL Server, and architecting highly scalable solutions. As a leading small to mid-sized business web hosting provider, ASPHostPortal strive to offer the most technologically advanced hosting solutions available to all customers across the world. Security, reliability, and performance are at the core of hosting operations to ensure each site and/or application hosted is highly secured and performs at optimum level.



Cheap ASP.NET 4.5 Hosting

We’re a company that works differently to most. Value is what we output and help our customers achieve, not how much money we put in the bank. It’s not because we are altruistic. It’s based on an even simpler principle. "Do good things, and good things will come to you".

Success for us is something that is continually experienced, not something that is reached. For us it is all about the experience – more than the journey. Life is a continual experience. We see the Internet as being an incredible amplifier to the experience of life for all of us. It can help humanity come together to explode in knowledge exploration and discussion. It is continual enlightenment of new ideas, experiences, and passions


Author Link


 

Corporate Address (Location)

ASPHostPortal
170 W 56th Street, Suite 121
New York, NY 10019
United States

Tag cloud

Sign in