فراخوانی سرویس های WCF به صورت Async
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۲/۰۴/۰۵ ۸:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
[DataContract]
public class Book
{
[DataMember]
public int Code { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Author { get; set; }
} [ServiceContract]
public interface IBookService
{
[OperationContract( AsyncPattern = true )]
IAsyncResult BeginGetAllBook( AsyncCallback callback, object state );
IEnumerable<Book> EndGetAllBook( IAsyncResult asyncResult );
}[OperationContract] IEnumerable<Book> GetAllBook(int code , AsyncCallback callback, object state );
public class CompletedAsyncResult<TEntity> : IAsyncResult where TEntity : class , new()
{
public IList<TEntity> Result
{
get
{
return _result;
}
set
{
_result = value;
}
}
private IList<TEntity> _result;
public CompletedAsyncResult( IList<TEntity> data )
{
this.Result = data;
}
public object AsyncState
{
get
{
return ( IList<TEntity> )Result;
}
}
public WaitHandle AsyncWaitHandle
{
get
{
throw new NotImplementedException();
}
}
public bool CompletedSynchronously
{
get
{
return true;
}
}
public bool IsCompleted
{
get
{
return true;
}
}
} public class BookService : IBookService
{
public BookService()
{
ListOfBook = new List<Book>();
}
public List<Book> ListOfBook
{
get;
private set;
}
private List<Book> CreateListOfBook()
{
Parallel.For( 0, 10000, ( int counter ) =>
{
ListOfBook.Add( new Book()
{
Code = counter,
Title = String.Format( "Book {0}", counter ),
Author = "Masoud Pakdel"
} );
} );
return ListOfBook;
}
public IAsyncResult BeginGetAllBook( AsyncCallback callback, object state )
{
var result = CreateListOfBook();
return new CompletedAsyncResult<Book>( result );
}
public IEnumerable<Book> EndGetAllBook( IAsyncResult asyncResult )
{
return ( ( CompletedAsyncResult<Book> )asyncResult ).Result;
}
}[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class BookServiceClient : System.ServiceModel.ClientBase<UI.BookService.IBookService>, UI.BookService.IBookService {
public BookServiceClient() {
}
public BookServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName) {
}
public BookServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public BookServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public BookServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}
public UI.BookService.Book[] BeginGetAllBook() {
return base.Channel.BeginGetAllBook();
}
}System.ServiceModel.ClientBase<UI.BookService.IBookService>
public class ServiceMapper<TChannel>
{
public static TChannel CreateChannel()
{
TChannel proxy;
var endPointAddress = new EndpointAddress( "http://localhost:7000/" + typeof( TChannel ).Name.Remove( 0, 1 ) + ".svc" );
var httpBinding = new BasicHttpBinding();
ChannelFactory<TChannel> factory = new ChannelFactory<TChannel>( httpBinding, endPointAddress );
proxy = factory.CreateChannel();
return proxy;
}
}"http://localhost:7000/" + "BookService.svc"
var channel = ServiceMapper<Contract.IBookService>.CreateChannel();
channel.BeginGetAllBook( new AsyncCallback( ( asyncResult ) =>
{
channel.EndGetAllBook( asyncResult ).ToList().ForEach( _record =>
{
Console.WriteLine( _record.Title );
} );
} ) , null );
Console.WriteLine( "Loading..." );
Console.ReadLine();