Introduction
Though Performance blocking and sluggishness is the tailbacks for any application, we can easily Overco Me these bottlenecks by using asynchronous programming. But Old-style practice for asynchronous programming was not the easy-to-do enough to write, debug and maintain. So what's the contemporary approach??
Well, in my view, this is task based asynchronous programming, which are updated in. NET 4.5 through the use of KE Ywords await and async . But what does and do async await ? async And is the await controlling continuation. When a method uses the async keyword, it means it's an asynchronous method, which might has an await keyword inside, and I F It has an await keyword, would async activate it. So, simply async activates await the, from which point, the asynchronous have been started. There is a nice explanation that have been given here.
In WCF, we can also consider an asynchronous operation when the service operation creates a delaying call. There is three ways to implement asynchronous operations:
- task-based Asynchronous
- event-based Asynchronous
IAsyncResult Asynchronous
In this article, I am going to use task-based asynchronous operation since this is the most contemporary strategy .
Okay. Let's move to the code.
Define and Implement Service
A very simple Service contract such as:
Hide Copy Code
[ServiceContract] interface imessage{ [OperationContract] task<string> getmessages (string msg);
With this simple contract, the implementation is just straight forward.
Hide Copy Code
public class messageservice:imessage{async task<string> imessage.getmessages (string msg) {var task = Task.Factory.StartNew (() = = { Thread.Sleep (10000); return "return from Server:" + msg;}); return await task. Configureawait (false);}
Here, the method async was marked with the keyword, which means it might use await keyword inside. It also means that the method would be able to suspend and then resume asynchronously at await points. Moreover, it points the compiler to boost the outcome of the method or any exceptions that could happen into the return type .
Service HostingHide Copy Code
Class program{ void Main (string[] args) { new ServiceHost (typeof (Messageservice)); Console.WriteLine ("Available endpoints: \ n"); SvcHost.Description.Endpoints.ToList (). ForEach (endpoints=> Console.WriteLine (Endpoints. Address.tostring ())); Svchost.open (); Console.ReadLine (); }}
Service ConfigurationHide Shrink Copy Code
<?XmlVersion="1.0 "?><Configuration><System.ServiceModel><Services><ServiceName="Rashim.RND.WCF.Asynchronous.ServiceImplementation.MessageService "><Host><Baseaddresses><AddBaseAddress="Net. tcp://localhost:8732/"/><AddBaseAddress="http://localhost:8889/"/></baseaddresses></host><EndpointAddress="TCP "Binding="NetTcpBinding "Contract="Rashim.RND.WCF.Asynchronous.Services.IMessage "/><EndpointAddress="Http "Binding="BasicHttpBinding "Contract="Rashim.RND.WCF.Asynchronous.Services.IMessage "><Identity><DnsValue="localhost/></identity></endpoint><EndpointAddress="MexBinding="Mextcpbinding "Contract="IMetadataExchange "/><EndpointAddress="MexBinding="mexHttpBinding "Contract="IMetadataExchange "/></service></services><Behaviors><Servicebehaviors><Behavior><servicemetadata/></behavior></servicebehaviors></behaviors> </ System.servicemodel> < startup><supportedruntime version= " Span class= "Code-keyword" >v4.0 "sku=" /> </startup></configuration>
After configuring the service, we need to configure the client app to consume the service.
Define Client
A Simple Console Application (Client):
Hide Copy Code
Class Program {Staticvoid Main (string[] args) {GetResult (); Console.ReadLine (); } private async static void GetResult () { var client = New Proxy ("basichttpbinding_imes Sage "); var task = Task.Factory.StartNew (() = = client. Getmessages ("Hello")); var str = await task; Str. ContinueWith (E = = { if (e.iscompleted) {Console.WriteLine (str. Result); } }); Console.WriteLine ("waiting for the result");}} Client ConfigurationHide Copy Code
<?XmlVersion="1.0 "?><Configuration><System.ServiceModel><Bindings><BasicHttpBinding><BindingName="Basichttpbinding_imessage "/></basichttpbinding></bindings><Client><EndpointAddress="Http://localhost:8889/Http "Binding="basichttpbinding "bindingconfiguration = "basichttpbinding_imessage" contract= " Rashim.RND.WCF.Asynchronous.Services.IMessage "name=" Span class= "Code-keyword" >basichttpbinding_imessage "/> </client> </system.servicemodel> Span class= "Code-keyword" ></configuration>
Finally, proxy class is given below through which the client would consume the services.
Hide Copy Code
Class Proxy:clientbase<imessage>, IMessage {public proxy () { } public proxy ( String endpointconfigurationname): Public task<string> getmessages (return channel.getmessages (msg); } }
That ' s it. Very easy stuff though.
task-based asynchronous operation in WCF Z