Click here to Skip to main content
15,892,059 members
Articles / Web Development / ASP.NET

Responsive WCF service With ICallbackEventHandler Interface

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
8 Feb 2013CPOL5 min read 31.8K   762   33  
Responsive WCF service with ICallbackEventHandler Interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebClient.DemoService;
using System.ServiceModel;
using System.Threading;

namespace WebClient
{

    public class CallBack : WebClient.DemoService.IDemoServiceCallback
    {

        public string Message
        {
            get;
            set;
        }
        public void ISOrerPlaceSuccessfully(bool issuccess, float total)
        {
            Thread.Sleep(5000);
            if (issuccess)
                this.Message = "Order with total of : " + total + " placed successfully";
            else
                this.Message = "Order with total of : " + total + " failed to place";
        }
    }

    public partial class Default : System.Web.UI.Page, ICallbackEventHandler
    {
        static CallBack callback;
        DemoServiceClient client;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    //setupClientSideCallback();

                    Response.Write("abc");
                    Server.MapPath("abc.aspx");
                }
                catch
                {
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            setupClientSideCallback();
            callback = new CallBack();
            InstanceContext ic = new InstanceContext(callback);
            client = new DemoServiceClient(ic);
            OrderItem item = new OrderItem();
            item.Name = "Test";
            item.Price = 12;
            item.Quantity = Convert.ToInt32(txtQunatity.Text);
            lblMsg.Text = "Placing Order...";
            client.PlaceOrder(item);
        }


        protected void setupClientSideCallback()
        {
            string ScriptRef = this.ClientScript.GetCallbackEventReference(this, "'" + 0 +

        "'", "OnCallback", "'" + lblMsg.ClientID + "'");

            formBody.Attributes.Add("onload", ScriptRef);
            string script = "<script language='javascript' type='text/javascript'> " +

                         " function getServerTime() " +

                         " { " + ScriptRef + " } " +

                         " function OnCallback(Result,Context) " +

                         " { " +

                         " var lbl=document.getElementById(Context); " +

                          " lbl.innerText=Result ; " +

                          " setTimeout(getServerTime, 1000); " +

                          " } " +

                         " </script> ";

            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "ClientCallback", script);

        }

        string ICallbackEventHandler.GetCallbackResult()
        {
            if (callback!=null && callback.Message != null)
            {
                return  callback.Message;
            }

            return "Placing Order..." ;
        }

        string eventArgument;

        void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
        {
            this.eventArgument = eventArgument;
        } 
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions