In some blog posts, IsOneWay is called "Asynchronous Method" directly ". Although most of the time there will be no problems with development, the two are different. Next, let's do a test. The same service contract is implemented using IsOneWay and asynchronous respectively. The client simulates concurrent calls using multiple threads and uses ServiceThrottlingBehavior (InstanceContextMode. Single can also be used) for concurrency control. Pay attention to the comparison of the output results, and we will find their differences.
IsOneWay version
[ServiceContract]
Public interface IService
{
[OperationContract (IsOneWay = true)]
Void Test ();
}
Public class MyService: IService
{
Public void Test ()
{
Console. WriteLine ("Service BeginAdd: {0}", DateTime. Now );
Thread. Sleep (5000 );
Console. WriteLine ("Service EndAdd: {0}", DateTime. Now );
}
}
Public class WcfTest
{
Public static void Test ()
{
AppDomain. CreateDomain ("Server"). DoCallBack (delegate
{
ServiceHost host = new ServiceHost (typeof (MyService ));
Host. AddServiceEndpoint (typeof (IService), new WSHttpBinding (), "http: // localhost: 8080/myservice ");
ServiceThrottlingBehavior throttling = new ServiceThrottlingBehavior ();
Throttling. MaxConcurrentInstances = 1;
Host. Description. Behaviors. Add (throttling );
Host. Open ();
});
For (int I = 0; I <3; I ++)
{
New Thread (delegate ()
{
IService channel = ChannelFactory <IService>. CreateChannel (new WSHttpBinding (),
New EndpointAddress ("http: // localhost: 8080/myservice "));
Using (channel as IDisposable)
{
Console. WriteLine ("Client {0} BeginAdd: {1 }",
Thread. CurrentThread. ManagedThreadId, DateTime. Now );
Channel. Test ();
Console. WriteLine ("Client {0} BeginAdd End: {1 }",
Thread. CurrentThread. ManagedThreadId, DateTime. Now );
}
}). Start ();
}
}
}
Output:
Client 15 BeginAdd: 2007-4-19 10:51:39
Client 14 BeginAdd: 2007-4-19 10:51:39
Client 16 BeginAdd: 2007-4-19 10:51:39
Client 16 BeginAdd End: 2007-4-19 10:51:40
Service BeginAdd: 2007-4-19 10:51:40
Service EndAdd: 2007-4-19 10:51:45
Client 14 BeginAdd End: 2007-4-19 10:51:46
Service BeginAdd: 2007-4-19 10:51:46
Service EndAdd: 2007-4-19 10:51:51
Service BeginAdd: 2007-4-19 10:51:51
Client 15 BeginAdd End: 2007-4-19 10:51:51
Service EndAdd: 2007-4-19 10:51:56
Asynchronous version
[ServiceContract]
Public interface IService
{
[OperationContract]
Void Test ();
[OperationContract (AsyncPattern = true)]
IAsyncResult BeginTest (AsyncCallback callBack, object state );
Void EndTest (IAsyncResult ar );
}
Public class MyService: IService
{
Public void Test ()
{
Console. WriteLine ("Service BeginAdd: {0}", DateTime. Now );
Thread. Sleep (5000 );
Console. WriteLine ("Service EndAdd: {0}", DateTime. Now );
}
Public IAsyncResult BeginTest (AsyncCallback callBack, object state)
{
Throw new Exception ("The method or operation is not implemented .");
}
Public void EndTest (IAsyncResult ar)
{
Throw new Exception ("The method or operation is not implemented .");
}
}
Public class WcfTest
{
Public static void Test ()
{
AppDomain. CreateDomain ("Server"). DoCallBack (delegate
{
ServiceHost host = new ServiceHost (typeof (MyService ));
Host. AddServiceEndpoint (typeof (IService), new WSHttpBinding (), "http: // localhost: 8080/myservice ");
ServiceThrottlingBehavior throttling = new ServiceThrottlingBehavior ();
Throttling. MaxConcurrentInstances = 1;
Host. Description. Behaviors. Add (throttling );
Host. Open ();
});
For (int I = 0; I <3; I ++)
{
New Thread (delegate ()
{
IService channel = ChannelFactory <IService>. CreateChannel (new WSHttpBinding (),
New EndpointAddress ("http: // localhost: 8080/myservice "));
Using (channel as IDisposable)
{
Console. WriteLine ("Client {0} BeginAdd: {1 }",
Thread. CurrentThread. ManagedThreadId, DateTime. Now );
IAsyncResult ar = channel. BeginTest (null, null );
Console. WriteLine ("Client {0} BeginAdd End: {1 }",
Thread. CurrentThread. ManagedThreadId, DateTime. Now );
Channel. EndTest (ar );
}
}). Start ();
}
}
}
Output:
Client 14 BeginAdd: 2007-4-19 10:53:56
Client 16 BeginAdd: 2007-4-19 10:53:56
Client 15 BeginAdd: 2007-4-19 10:53:56
Client 16 BeginAdd End: 2007-4-19 10:53:56
Client 14 BeginAdd End: 2007-4-19 10:53:56
Client 15 BeginAdd End: 2007-4-19 10:53:56
Service BeginAdd: 2007-4-19 10:53:59
Service EndAdd: 2007-4-19 10:54:04
Service BeginAdd: 2007-4-19 10:54:04
Service EndAdd: 2007-4-19 10:54:09
Service BeginAdd: 2007-4-19 10:54:09
Service EndAdd: 2007-4-19 10:54:14
Through comparison, we found that the Asynchronous Method BeginXXX call is not affected by concurrency control, and directly returns control after the call; while the IsOneWay is different, it is blocked until the service method is executed (of course, does not wait until the service method execution is completed ). This difference may bring different effects in terms of processing concurrency. Understanding the differences between IsOneWay and Asynchronization can avoid unexpected problems.
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/zengjibing/archive/2009/01/17/3813601.aspx