How to call webservices in a workflow in an asynchronous way  
Author Message
Satya vani





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi,
I am doing R&D on the features of Windows Workflow Foundation which we are going to apply for our project after release of WWF. we are using SOA Architecture. So I need to call services based on the rules. I have succeded in doing them practically using Sequential and State Machine Workflow. So, now I want to achieve Asynchronous way of calling webservices. I don't know whether this can be possible with WWF or not. If it is possible, can any body please help me in doing this.
Thanks in advance.



Software Development for Windows Vista15  
 
 
vishu_gupta





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi Satya,

All workflow instances are asynchronous since their lifetime is managed by the workflow runtime. Hence the caller starts them using the InvokeWebservice Activity and carry on with the other work. Use the WebserviceOutput activity to recieve the response.

Hope this answers your question.

Thanks,

Vishal



 
 
Matt Milner - Pluralsight





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Actually, the webserviceoutput activity is for when you are exposing your workflow as a service and does not work with the InvokeWebService activity.

If you want true asynchronous invocation you'll have to write your own activity. If you have a one way operation, it should return as soon as the message is sent.

What is the scenario that you are trying to enable with asynch calls If your activity is going to accept a response, it will essentially block the current branch of execution waiting for a response anyway. You might be able to write a pair of activities, one to begin the execution and one to get the response, but you'd probably need to write some local services to help you out with the communication and correlation.

Matt



 
 
Satya vani





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi Matt,

Thank you for ur reply. In my scenario, I want to call a webservice, but instead of waiting for the response from that I want to continue with other activities and I want to specify the Method where the webservice response can be put.

As per your reply I need to write 2 code activities first one is for invoking and executing the web service, but is it possible to say that give response to a particular url. Do you have any idea how to implement it, or is there any built in activity to manage asynchronous calls.



 
 
Matt Milner - Pluralsight





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

I'd do something like the following:

  1. Create a local service that identifies a method to call and an event that will get raised when the web service call is done.
  2. In the workflow, use the CallExternalMethod activity to call your local service. In your local service you will start the web service call asynchronously and then return control back to the workflow.
  3. Add the other activities to do some work before the call returns. Then, add a HandleExternalEvent activity that handles the event from your local service.
  4. Finally, in the local service, when the web service request completes, raise the event to send the data into the web service.

This is the simplest way to do this. You want the local service because it can manage the state for your calls even if the workflow were to go idle and get persisted.

Matt



 
 
Satya vani





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi matt,

In ur reply you have mentioned about local service, is it local webservice . If it is so I have created a webservice with one public method and an event, like the following

[Serializable]

public class RegEventArgs : ExternalDataEventArgs{

private string _strMessage;

public RegEventArgs(Guid instanceId, string strMessage)

: base(instanceId){

_strMessage = strMessage;}}

[ExternalDataExchange]

public interface ILocalService{

void ReturnMessage(string retMessage);

event EventHandler<RegEventArgs> RegistrationDone;}

I have added this webservice to a workflow and trying to set properties of CallExternalMethod Activity. I am not able to view this service under refrenced assemblies in the window which is opened for selecting InterfaceType for CallExternalMethod.

Please help me with sample code, If u have any Idea. Thanks in advance.



 
 
Matt Milner - Pluralsight





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

This looks like you are on the right track. I'm not sure why you can't see the interface as you have it declared correctly as far as I can tell.

I've posted a simple example out for you to look at. Let me know if this helps.

http://www.pluralsight.com/matt/downloads/asyncwebserviceworkflowexample.zip

The workflow calls into the local service to start the web service call, then does some other work. When the web service calls returns, the event is raised and the workflow uses the HandleExternalEvent activity to handle the event.

Matt



 
 
Satya vani





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi matt,

Thank you for your response and for sending a code sample. I have done the same thing except raising the webservicecompleted Event after completing the WebService as HelloWorldCompleted in your example and you have done Interface declaration and implementation inside the workflow, where as I tried to create it in a local webservice as your steps says.

This example suits what exactly I am trying to achieve. Thank you once again.



 
 
Satya vani





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi matt,

The Asynchronous way of calling webservice in workflow is working fine for one Request. Will it works fine for hundreds of requests and will it give responses to the corresponding requests made. How to handle no of requests . Is it required to put the requests in a queue and process it later. I am just wondering how it can process all the requests and how it identifies a particular response is for a particular request.

Could you please explain me how to handle the situation for number of requests. Your response will be more precious for me.



 
 
Matt Milner - Pluralsight





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

The use of the workflow instance id as user state for the asynchronous callback allows the handling of many requests from many different workflows. If you need to correlate responses within workflow, then you might need to use a class or struct for the user state with more data in it. See the lab on local communications in the HOL for WF for more examples/info on correlation.

As it stands, because the sample uses the workflow instance id, you should be able to run many instances of the workflow and get the correct response back to the correct workflow.

Any issues with handling a number of requests will likely be related to .NET web services or .NET networking configuration no WF related code.

Matt



 
 
Satya vani





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi matt,

I got one more scenario. In local class of your example we are calling webservice asynchronously and getting the webservice response using the delegate "service_HelloWorldCompleted" which is returned as string in the webmethod.

service.HelloWorldCompleted += new AsyncWebServiceWorkflow.localhost.HelloWorldCompletedEventHandler(service_HelloWorldCompleted);

But In my webservice webmethod return type is void. Response is not available in that webmethod.

There is one more project which is adding webreference of this webservice and response will be generated in one class of that project.

Now I need to get the response which is generated in that class to our workflow. How could I get the response for a particular request from that class. Please help me in the above scenario.

Thank you

Satya



 
 
Matt Milner - Pluralsight





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

It sounds like you have a class library that is wrapping your web service call. If that is the case, then just raise an event from your library when the event is raised from the web service. That gives you the same pattern in the local service of the workflow.

Matt



 
 
Satya vani





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi matt,

I am not clear on ur response. can u please explain the phrase

"when the event is raised from the web service".

In the previous case the webmethod is returning string value so that we are able to get the response by using HelloWorldCompletedEventArgs like e.Result like following

service.HelloWorldCompleted += new AsyncWebServiceWorkflow.localhost.HelloWorldCompletedEventHandler(service_HelloWorldCompleted);

Inside service_HelloWorldCompleted

WebServiceResponseEventArgs args = new WebServiceResponseEventArgs((Guid)e.UserState, e.Result);

But now the webmethod which I am calling returns nothing. I have to call it asynchronously and get the corresponding response from the class which adds webreference of this webservice. When calling the event in class, how to corelate the response. Please give me the suggestion on this. Than you in advance.

Satya



 
 
Matt Milner - Pluralsight





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

I'm not sure I fully understand what you are doing that would cause you to both have a web service that returns nothing and need to get the response, so clarify that if you can. But, I'll give it a shot:

I am assuming you have a class library that wraps your web service call. If I am wrong in that assumption, please let me know.

In your class library, have a method like StartCall(parameters) which begins the asynchrnous call to the web service and sets up the delegate to be called when the service ends. In that same class, define an event that you can raise and a custom event arguments class that you can use to include your web service response if you have one.

In the workflow local service, instead of calling the web service directly, register a handler for the event in your class, then call hte start method.

When the event is handled in your local service, you should be able to handle it the same way I did in my example and raise an event into the workflow.

Matt



 
 
Satya vani





PostPosted: Windows Workflow Foundation, How to call webservices in a workflow in an asynchronous way Top

Hi matt,

How are u . According to your response "In your class library, have a method like StartCall(parameters) which begins the asynchrnous call to the web service and sets up the delegate to be called when the service ends." .

1). I can make an asynchronous call but I can't get the SomeMethodCompleted event to call the delegate as you says when the service ends, bcoz the webmethod I am calling is with void returntype, and the response will be generated in one class after receiving the request from AsyncEngine where the requests are formed in to a Queue and processed one by one.

2). Now I exposed my workflow as a webservice so that they will add a reference of that and access webmethod which is with a parameter, so that I can receive response back to workflow (Is it correct way ).

3). If receiving can be done as above, how can I send response back to user, and how to corelate the responses to requests.

If u understand my problem please help me in giving the samples or links for giving the response back to user after asynchronous call.

This is my great problem, so your reply will be more precious for me.

Thank you

satya