Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to host the Windows Workflow as a Web service(.svc)?

I am trying to host the windows workflow as a web service, below is the sample workflow that I built and would like to host as a web service(.svc), can you please suggest the required steps?

using System;
using System.ServiceModel.Activities;
using System.Activities;
using System.ServiceModel;
using System.Activities.Statements;

namespace DemoWF
{
    public class _25_LeaveRequest
    {
        public WorkflowService GetInstance()
        {
            WorkflowService service;
            Variable<int> empID = new Variable<int> { Name = "empID" };
            Variable<int> requestID = new Variable<int> { Name = "requestID" };

        Receive receiveLeaveRequest = new Receive
        {
            ServiceContractName = "ILeaveRequestService",
            OperationName = "ApplyLeave",
            CanCreateInstance = true,
            Content = new ReceiveParametersContent
            {
                Parameters ={
                    {"empID",new OutArgument<int>(empID)}
                }
            }
        };

        SendReply replyLeaveRequestID = new SendReply
        {
            Request = receiveLeaveRequest,
            Content = new SendParametersContent
            {
                Parameters ={
                            {"requestID",new InArgument<int>(requestID)},
                        },
            },
        };


        Sequence workflow = new Sequence()
        {
            Variables = { empID, requestID },
            Activities = {
                new WriteLine{Text="WF service is starting..."},
                receiveLeaveRequest,
                new WriteLine{
                    Text=new InArgument<string>(aec=> "Emp ID="+empID.Get(aec).ToString())
                },
                new Assign<int>{
                    Value=new InArgument<int>(5),
                    To=new OutArgument<int>(requestID)
                },
                new WriteLine{
                    Text=new InArgument<string>(aec=> "Request ID="+requestID.Get(aec).ToString())
                },
                replyLeaveRequestID
            },
        };

        service = new WorkflowService
        {
            Name = "AddService",
            Body = workflow
        };
        return service;
    }
}

Right now, it is self hosted as highlighted below

namespace DemoWF
{
    class Program
    {
        static void Main(string[] args)
        {
            LeaveRequest();
        }

        private static void LeaveRequest()
        {
            _25_LeaveRequest receiveAndReplyWorkflow = new _25_LeaveRequest();
            WorkflowService wfService = receiveAndReplyWorkflow.GetInstance();
            Uri address = new Uri("http://localhost:8000/WFServices");
            WorkflowServiceHost host = new WorkflowServiceHost(wfService, address);

            try
            {
                Console.WriteLine("Opening Service...");
                host.Open();

                Console.WriteLine("WF service is listening on " + address.ToString() + ", press any key to close");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("some thing bad happened" + e.StackTrace);
            }
            finally
            {
                host.Close();
            }
        }
    }
}
like image 835
Karthikeyan Vijayakumar Avatar asked Oct 18 '22 12:10

Karthikeyan Vijayakumar


1 Answers

The quickest way would be to create a WCF Workflow Service Application.

Creating a WCF Workflow Service Application...

You'll get a workflow designer where you can drag and drop the activities you need:

Boilerplate workflow from project template...

And if you run the project in Visual Studio, you'll get an auto-generated WSDL with your service operation(s):

The running workflow-as-a-WCF-service...

And also it will bring up Visual Studio's WCF Test Client tool:

enter image description here

You can create a workflow-based service that handles multiple methods by using the Pick Branch activity. Each branch would then have a Receive and Send Reply activity, with the receive activity moved to the trigger section and the send reply activity in the action part.

Each trigger would be for a specific operation on the service. In the following example, I define two operations: MyFirstOperation and MySecondOperation.

enter image description here

Below is what the WCF test client tool will show with multiple operations exposed from the workflow:

enter image description here

Hopefully that gets you started. The topic of standing up workflow-based WCF services can get quite involved. :)

like image 108
ajawad987 Avatar answered Oct 21 '22 02:10

ajawad987