Inspired by How to: Implement an Asynchronous Service Operation and Building Task Based WCF Services with Task Parallel Library, I'm trying to make a WCF web service with an operation that is executed asynchronously.
The idea is that I have a method that does work that lasts anywhere from a second to a minute that is called by a button on a web page and I have a timer that calls another method in the same service that eventually will return the asynchronous operation's status (working or not).
So I set up a dummy example and my asynchronous operation actually blocks my Web Serivce.
[ServiceContract(Namespace = "")]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service1
{
[OperationContract(AsyncPattern = true, Action = "TestServiceMethod", Name = "TestServiceMethod", ReplyAction = "TestServiceMethodReply")]
public IAsyncResult BeginTestServiceMethod(string request, AsyncCallback callback, object asyncState)
{
var task = new Task<string>((state) =>
{
SpinWait.SpinUntil(() => { return false; }, 5000);
return request;
}, asyncState);
task.ContinueWith((t) => { callback(t); });
task.Start();
return task;
}
public string EndTestServiceMethod(IAsyncResult result)
{
var task = (Task<string>)result;
return task.Result;
}
[OperationContract]
public string OtherTest()
{
return "OtherTest";
}
}
and this is the javascript on my page (the click function is activated by clicking a button)
function Click() {
var service = new Service1();
service.TestServiceMethod("Dummy", PopWord);
service.OtherTest(PopWord);
}
function PopWord(word) {
alert(word);
}
The result is a 5 seconds wait when I click on the button, followed by "Dummy" and "OtherTest" popping one after the other. Expected behavior would be "OtherTest" popping with "Dummy" 5 seconds later.
Can anyone spot what I am doing wrong or perhaps suggest another approach?