Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 365, comments - 174, trackbacks - 34

My Links

News


Shawn Weisfeld's Facebook profile

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

Using the CCR for web service calls

So I figured I would see how the CCR compared the traditional .NET 2.0 Web Service model so I threw together this demo application. The interesting thing is that while I wired up the async call back events first, most of the CCR results arrived first.

Web Service

    1         [WebMethod]

    2         public string Echo(string message, int sleep)

    3         {

    4             System.Threading.Thread.Sleep(sleep);

    5             return message;

    6         }

Test Harness

    1         static void Main(string[] args)

    2         {

    3             int sleeptime = 100;

    4 

    5             //Call using traditional sequential model

    6             Console.WriteLine("traditional sequential test");

    7             for (int i = 0; i < 5; i++)

    8             {

    9                 using (CCRService.Service1 ws = new CCRServiceTest.CCRService.Service1())

   10                 {

   11                     ws.Credentials = System.Net.CredentialCache.DefaultCredentials;

   12                     Console.WriteLine(ws.Echo(string.Format("Seq Message {0}", i), sleeptime));

   13                 }

   14             }

   15 

   16             //Call using traditional Async Model

   17             Console.WriteLine("traditional Async Model test");

   18             for (int i = 0; i < 5; i++)

   19             {

   20                 using (CCRService.Service1 ws = new CCRServiceTest.CCRService.Service1())

   21                 {

   22                     ws.Credentials = System.Net.CredentialCache.DefaultCredentials;

   23                     ws.EchoCompleted += new CCRServiceTest.CCRService.EchoCompletedEventHandler(

   24                         delegate(object sender, CCRServiceTest.CCRService.EchoCompletedEventArgs e)

   25                         {

   26                             Console.WriteLine(e.Result);

   27                         });

   28 

   29                     ws.EchoAsync(string.Format("Async Message {0}", i), sleeptime);

   30                 }

   31             }

   32 

   33             //Call using the CCR

   34             Console.WriteLine("CCR test");

   35             DispatcherQueue dq = new DispatcherQueue();

   36             Port<int> work = new Port<int>();

   37 

   38             for (int i = 0; i < 5; i++)

   39             {

   40                 work.Post(i);

   41             }

   42 

   43             Arbiter.Activate(dq,

   44                 Arbiter.Receive(true, work,

   45                 delegate(int i)

   46                 {

   47                     using (CCRService.Service1 ws = new CCRServiceTest.CCRService.Service1())

   48                     {

   49                         ws.Credentials = System.Net.CredentialCache.DefaultCredentials;

   50                         Console.WriteLine(ws.Echo(string.Format("CCR Message {0}", i), sleeptime));

   51                     }

   52                 }));

   53 

   54             Console.WriteLine("Done!");

   55             Console.ReadKey();

   56 

   57 

   58         }

Results:

traditional sequential test
Seq Message 0
Seq Message 1
Seq Message 2
Seq Message 3
Seq Message 4
traditional Async Model test
CCR test
Done!
CCR Message 0
CCR Message 1
CCR Message 2
CCR Message 3
Async Message 1
Async Message 2
Async Message 0
Async Message 3
Async Message 4
CCR Message 4

Print | posted on Tuesday, January 8, 2008 12:59 PM | Filed Under [ C# ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 5 and type the answer here:

Powered by: