WCF异步调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    /**
     * 1.与下列各项之间的服务协定接口:
     *      a.同步 SampleMethod 操作。
     *      b.异步 BeginSampleMethod 操作。
     *      c.异步 BeginServiceAsyncMethod/EndServiceAsyncMethod 操作对。
     * 2.使用 System.IAsyncResult 对象的服务实现。
     **/
    [ServiceContractAttribute( Namespace = "http://microsoft.wcf.documentation" )]

    public interface IService1
    {
        [OperationContractAttribute]
        string SampleMethod( string msg );

        [OperationContractAttribute( AsyncPattern = true )]
        IAsyncResult BeginSampleMethod( string msg, AsyncCallback callback, object asyncState );


         string EndSampleMethod( IAsyncResult result );

         [OperationContractAttribute( AsyncPattern = true )]
         IAsyncResult BeginServiceAsyncMethod( string msg, AsyncCallback callback, object asyncState );

         string EndServiceAsyncMethod( IAsyncResult result );

    }

}

 

 public class Service1 : IService1
    {
        public string SampleMethod( string msg )
        {
            Console.WriteLine( "Called synchronous sample method with \"{0}\"", msg );
            return "The sychronous service greets you: " + msg;
        }

        public IAsyncResult BeginSampleMethod( string msg, AsyncCallback callback, object asyncState )
        {
            Console.WriteLine( "BeginSampleMethod called with: " + msg );
            return new CompletedAsyncResult<string>( msg );
        }

        public string EndSampleMethod( IAsyncResult result )
        {
            throw new NotImplementedException( );
        }

        public IAsyncResult BeginServiceAsyncMethod( string msg, AsyncCallback callback, object asyncState )
        {
            throw new NotImplementedException( );
        }

        public string EndServiceAsyncMethod( IAsyncResult result )
        {
            throw new NotImplementedException( );
        }
    }

    class CompletedAsyncResult<T> : IAsyncResult
    {
        T data;
        public CompletedAsyncResult(T data )
        {
            this.data = data;
        }

        public T Data
        { get { return data; } }


        public object AsyncState
        {
            get { return (object)data; }
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get { throw new Exception( "The method or operation is not implemented." ); }
        }

        public bool CompletedSynchronously
        {
            get {return true; }
        }

        public bool IsCompleted
        {
            get { return true; }
        }
    }

//客户端

namespace wcf异步调用2
{
    class Program
    {
        static void Main( string[ ] args )
        {
            Console.WriteLine( "begin asyCall {0}", DateTime.Now );
            Service1Client work = new Service1Client( );
            work.SampleMethodCompleted += new EventHandler<SampleMethodCompletedEventArgs>
                ( work_SampleMethodCompleted );
            work.SampleMethodAsync( "连接成功" );
            Console.WriteLine( "end asyCall {0}", DateTime.Now );
            Console.Read( );
        }

        static void work_SampleMethodCompleted( object sender, SampleMethodCompletedEventArgs e )
        {
            Console.WriteLine( "result={0},{1}",e.Result,DateTime.Now);
        }
    }
}

posted @ 2012-09-05 22:00  blog_yuan  阅读(239)  评论(0编辑  收藏  举报