Example #1
0
        static void AddServiceAsyncWithDataContract()
        {
            try
            {
                Console.Title = "Asynchronous Pattern in Windows Communication Foundation";

                Console.WriteLine("Enter first number:");
                int number1 = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Enter second number:");
                int             number2 = Convert.ToInt32(Console.ReadLine());
                AddDataContract input   = new AddDataContract
                {
                    Nbr1 = number1,
                    Nbr2 = number2
                };
                Console.ForegroundColor = ConsoleColor.Yellow;
                _addServiceProxy        = new ProxyFactory <IAddService>("AddService");
                _service = _addServiceProxy.CreateProxy();

                Console.WriteLine("Service Proxy created.");
                Console.WriteLine("Calling BeginAdd of WCF service");
                IAsyncResult res = _service.BeginAddDC(input, new AsyncCallback(AddCallbackDC), _service);

                Console.WriteLine("Sent request BeginAdd of WCF service");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ResetColor();
            }
        }
Example #2
0
        public AddDataContract EndAddDC(IAsyncResult ar)
        {
            AddDataContract result = null;

            try
            {
                if (ar != null)
                {
                    using (AddAsyncResult asyncResult = ar as AddAsyncResult)
                    {
                        if (asyncResult == null)
                        {
                            throw new ArgumentNullException("IAsyncResult parameter is null.");
                        }

                        if (asyncResult.Exception != null)
                        {
                            throw asyncResult.Exception;
                        }

                        asyncResult.AsyncWait.WaitOne();
                        result = asyncResult.AddContract;
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorInfo err = new ErrorInfo(ex.Message, "EndAddDC faills");
                throw new FaultException <ErrorInfo>(err, "reason goes here.");
            }
            return(result);
        }
Example #3
0
        public IAsyncResult BeginAddDC(AddDataContract input, AsyncCallback callback, object state)
        {
            AddAsyncResult asyncResult = null;

            try
            {
                asyncResult = new AddAsyncResult(input, callback, state);

                //Queues a method for execution. The method executes when a thread pool thread becomes available.
                ThreadPool.QueueUserWorkItem(new WaitCallback(CallbackDC), asyncResult);
            }
            catch (Exception ex)
            {
                ErrorInfo err = new ErrorInfo(ex.Message, "BeginAddDC faills");
                throw new FaultException <ErrorInfo>(err, "reason goes here.");
            }

            return(asyncResult);
        }
Example #4
0
 private AddDataContract InternalAdd(AddDataContract input)
 {
     Thread.Sleep(TimeSpan.FromSeconds(20)); //模拟处理复杂运算
     input.Result = input.Nbr1 + input.Nbr2;
     return(input);
 }
Example #5
0
 public AddAsyncResult(AddDataContract input, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.AddContract = input;
 }