Here's an example. I got it working with help from the following posts.
Up vote 14 down vote favorite 4 share g+ share fb share tw.
I am trying to make a WCF REST method entirely asynchronous (I don't want to block anywhere). Essentially I have a simple service with 3 layers: Service, Business Logic and Data Access Layer. The Data Access Layer is accessing a database and it can take several second to get a response back from that method.
I don't understand very well how to chaining of all those method work. Can someone please help me to complete the sample I am trying to write below? I don't understand well the pattern used by WCF and I didn't find much documentation on the subject.
Can someone help me to complete the following example? In addition, how can I measure that the service will be able to handle more load than a typical synchronous implementation? Using System; using System.Collections.
Generic; using System.Runtime.Remoting. Messaging; using System. ServiceModel; using System.ServiceModel.
Activation; using System.ServiceModel. Web; using System.Threading. Tasks; namespace WcfRestService1 { ServiceContract AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.
Allowed) ServiceBehavior(InstanceContextMode = InstanceContextMode. PerCall) public class Service1 { private BusinessLogic bll = new BusinessLogic(); // Synchronous version WebGet(UriTemplate = "/sync") public string GetSamples() { return bll.ComputeData(); } // Asynchronous version - Begin WebGet(UriTemplate = "/async") OperationContract(AsyncPattern = true) public IAsyncResult BeginGetSampleAsync(AsyncCallback callback, object state) { Task t = bll. ComputeDataAsync(); // What am I suppose to return here // return t.
AsyncState;? } // Asynchronous version - End public List EndGetSampleAsync(IAsyncResult result) { // How do I handle the callback here? } } public class BusinessLogic { public Task ComputeDataAsync() { DataAccessLayer dal = new DataAccessLayer(); return dal.GetData(); } public string ComputeData() { Task t = this.
ComputeDataAsync(); // I am blocking... Waiting for the data t.Wait(); return t. Result; } } public class DataAccessLayer { public Task GetData() { // Read data from disk or network or db } } } c# wcf asynchronous task-parallel-library link|improve this question edited Nov 28 '11 at 3:58 asked Nov 7 '11 at 17:14Martin3,71474284 72% accept rate.
This sounds like you might be trying to solve the problem with the wrong solution. – Lucas B Nov 28 '11 at 22:50 @np-hard: I am not planning to use EF. I'm not trying to make a WCF service "faster", I want it to be able to handle more load (i.e.
More connections with less thread) – Martin Nov 29 '11 at 5:11 please look at updated code using sql command, async pattern goes all the way to data layer. – np-hard Nov 29 '11 at 15:57.
Here's an example. I got it working with help from the following posts: Edit: Added an example of an async client Implement Classic Async Pattern using TPL pfelix.wordpress.com/2008/06/27/wcf-and-... pfelix.wordpress.com/2008/06/28/wcf-and-... Here's a little do-nothing service: namespace WcfAsyncTest { ServiceContract public interface IAsyncTest { OperationContract(AsyncPattern=true) IAsyncResult BeginOperation(AsyncCallback callback, object state); string EndOperation(IAsyncResult ar); } // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.
Allowed) ServiceBehavior(InstanceContextMode = InstanceContextMode. PerCall) public class Service1 : IAsyncTest { public IAsyncResult BeginOperation(AsyncCallback callback, object state) { Task result = Task.Factory. StartNew((x) => { // spin to simulate some work var stop = DateTime.Now.
AddSeconds(10); while (DateTime. Now callback(t)); return result; } public string EndOperation(IAsyncResult ar) { ar.AsyncWaitHandle.WaitOne(); return "Hello! "; } } } And here's the client (command line): using System; using System.Collections.
Generic; using System. Linq; using System. Text; namespace TestClient { class Program { static void Main(string args) { var client = new ServiceReference1.AsyncTestClient(); var result = client.Operation(); Console.
WriteLine(result); Console.ReadLine(); } } } if you put trace points on the service, you can see that WCF really is calling EndOperation for you. Async Client Example First, you will need to generate an async proxy. You can do that by right-clicking on the Service Reference (in the References Folder of your project), and choosing "Configure Service Reference".
Check the "Generate Asynchronous Operations" checkbox. Now your client proxy will have some new members that weren't there before. Here's how to use them: // this is in the command-line test client // no changes to your service required.
Static void AsyncTest() { var client = new ServiceReference1.AsyncTestClient(); client. OperationCompleted += new EventHandler(client_OperationCompleted); client.OperationAsync(); Console. WriteLine("Operation Running"); } static void client_OperationCompleted(object sender, ServiceReference1.
OperationCompletedEventArgs e) { if (e. Error == null) Console. WriteLine("Operation Complete.
Result: " + e. Result); else Console. WriteLine(e.Error.ToString()); }.
Missing a } and); in that TaskFactory.StartNew() – dthorpe Nov 28 '11 at 23:19 1 @dthorpe Thanks! It was actually the less-than sign on the while statement that did it -- I had just copy-pasted from the code editor, and I didn't think to escape it. – JMarsch Nov 28 '11 at 23:22 imo you shouldnt really have to do a WaitOne in the EndXXX method since that would block your WCF thread, I am guessing the main driver for having a async wcf solution is to not block, and improve scalability – np-hard Nov 29 '11 at 3:05 Thanks JMarsh!
However, I'd like to also understand how I make everything asynchronous (including the three layers: Service, BLL, and DAO). – Martin Nov 29 '11 at 5:14 1 @np-hard: Having End block is part of MS's standard for the async model (msdn.microsoft.com/en-us/library/ms228963.aspx ) Since wcf won't call EndOperation until after it finishes, the call will never block in this case. However, it is valid (in a non-wcf use case) to call BeginOperation, do something, and then call EndOperation() with the expectation that you will be blocked until the operation completes (see the link above for a more detailed explanation).
– JMarsch Nov 29 '11 at 17:15.
Here is an implementation of a service that implements Async. In this the callback of wcf is passed all the way to the ado. Net's sql command.
When the command returns, it would invoke the service's EndXXX method, which would invoke Business layer, which would finally invoke EndXXX of SqlCommand. Let me know if you face any issues public class Service { private BusinessLogic businessLayer = new BusinessLogic(); public IAsyncResult BeginAnyOperation(AsyncCallback callback, object userState) { return businessLayer. BeginComputeData(callback, userState); } public string EndAnyOperation(IAsyncResult result) { return businessLayer.
EndComputeDate(result); } } public class MyState : IAsyncResult { public MyState() { } public object AsyncState { get; set; } public WaitHandle AsyncWaitHandle { get; set; } public bool CompletedSynchronously { get { return true; } } public bool IsCompleted { get; set; } public AsyncCallback AsyncCallback { get; set; } public T Result { get; set; } public IAsyncResult InnerResult { get; set; } } public class BusinessLogic { private DataAccessLayer dal = new DataAccessLayer(); public IAsyncResult BeginComputeData(AsyncCallback callback, object state) { return dal. BeginGetData(callback, state); } public string EndComputeDate(IAsyncResult asyncResult) { return dal. EndGetData(asyncResult); } } public class DataAccessLayer { public IAsyncResult BeginGetData(AsyncCallback callback, object state) { var conn = new SqlConnection(""); conn.Open(); SqlCommand cmd = new SqlCommand("myProc", conn); var commandResult = cmd.
BeginExecuteReader(callback, state, System.Data.CommandBehavior. CloseConnection); return new MyState { AsyncState = cmd, InnerResult = commandResult }; } public string EndGetData(IAsyncResult result) { var state = (MyState)result; var command = (SqlCommand)state. AsyncState; var reader = command.
EndExecuteReader(state. InnerResult); if (reader.Read()) return reader. GetString(0); return string.
Empty; } }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.