juil

01

.NET WCF async

Posted by : admin | On : 1 juillet 2014

docs

http://jaliyaudagedara.blogspot.fr/2013/03/asynchronous-operations-in-wcf.html

http://www.codeproject.com/Articles/646239/NET-Asynchronous-Patterns

resources

https://onedrive.live.com/?cid=5f84eceeec6f3f8e&id=5F84ECEEEC6F3F8E!237&authkey=!ADqvp2nWpn6QtSw

 

 

 

sample code illustration

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsyncWcfClient
{
    class Program
    {
        static wsService1.Service1Client client1 = new wsService1.Service1Client();
        static wsService2.Service2Client client2 = new wsService2.Service2Client();
        static wsService3.Service3Client client3 = new wsService3.Service3Client();

        static void Main(string[] args)
        {
            client1.GetDataCompleted += client_GetDataCompleted;
            client1.GetDataAsync("event-based asynchronous pattern");
            Console.WriteLine("Waiting for async operation...");

            //client1.BeginGetData("IAsyncResult asynchronous pattern (Client-Side)", new AsyncCallback(GetDataCallBack), null);
            //Console.WriteLine("Waiting for async operation...");

            //client2.BeginWorkerMethod("IAsyncResult asynchronous pattern (Server-Side)", new AsyncCallback(GetDataCallBack), null);
            //Console.WriteLine("Waiting for async operation...");

            //InvokeAsyncMethod("task-based asynchronous pattern");
            //Console.WriteLine("Waiting for async operation...");

            Console.ReadLine();
        }

        static void client_GetDataCompleted(object sender, wsService1.GetDataCompletedEventArgs e)
        {
            Console.WriteLine(e.Result.ToString());
        }

        ////IAsyncResult asynchronous pattern (Client-Side) callback
        //static void GetDataCallBack(IAsyncResult result)
        //{
        //    Console.WriteLine(client1.EndGetData(result).ToString());
        //}

        ////IAsyncResult asynchronous pattern (Server-Side) callback
        //static void GetDataCallBack(IAsyncResult result)
        //{
        //    Console.WriteLine(client2.EndWorkerMethod(result).ToString());
        //}

        //static async void InvokeAsyncMethod(string message)
        //{
        //    Console.WriteLine(await client3.MyWorkerMethodAsync(message));
        //}
    }
}

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

namespace AsyncWcfService
{
 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
 [ServiceContract]
 public interface IService1
 {
 //event-based asynchronous pattern
 [OperationContract]
 string GetData(string message);
 }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncWcfService
{
 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
 // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
 public class Service1 : IService1
 {
 public string GetData(string message)
 {
 Thread.Sleep(5000);
 return message;
 }
 }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace AsyncWcfService
{
 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService2" in both code and config file together.
 [ServiceContract]
 public interface IService2
 {
 //IAsyncResult asynchronous pattern
 [OperationContractAttribute(AsyncPattern = true)]
 IAsyncResult BeginWorkerMethod(string message, AsyncCallback callback, object asyncState);

 string EndWorkerMethod(IAsyncResult result);
 }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncWcfService
{
 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service2" in code, svc and config file together.
 // NOTE: In order to launch WCF Test Client for testing this service, please select Service2.svc or Service2.svc.cs at the Solution Explorer and start debugging.
 public class Service2 : IService2
 {
 public IAsyncResult BeginWorkerMethod(string message, AsyncCallback callback, object asyncState)
 {
 var task = Task<string>.Factory.StartNew((res) => MyMethod(asyncState,message), asyncState);
 return task.ContinueWith(res => callback(task));
 }

 public string EndWorkerMethod(IAsyncResult result)
 {
 return ((Task<string>)result).Result;
 }

 private string MyMethod(object asyncState,string message)
 {
 Thread.Sleep(5000);
 return message;
 }
 }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace AsyncWcfService
{
 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService3" in both code and config file together.
 [ServiceContract]
 public interface IService3
 {
 //task-based asynchronous pattern
 [OperationContract]
 Task<string> MyWorkerMethodAsync(string message);
 }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AsyncWcfService
{
 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service3" in code, svc and config file together.
 // NOTE: In order to launch WCF Test Client for testing this service, please select Service3.svc or Service3.svc.cs at the Solution Explorer and start debugging.
 public class Service3 : IService3
 {
 public async Task<string> MyWorkerMethodAsync(string message)
 {
 return await Task.Factory.StartNew(() => MyMethod(message));
 }

 private string MyMethod(string message)
 {
 Thread.Sleep(5000);
 return message;
 }
 }
}